Like it!

Join me on Facebook!

Like it!

A list of useful git commands

Carefully crafted for the absent-minded.

I type git in the command line on a daily basis, but I keep on forgetting most of the core commands available. Especially if I don't use them so frequently. The following list contains what I need to remember about Git. I will update it as needed.

Rename branches

action command
rename the current branch git branch -m <new-name>
rename a different branch git branch -m <old-branch-name> <new-branch-name>

Delete branches

action command
delete a local branch git branch -d <branch-name>
force delete a local branch (if not fully merged) git branch -D <branch-name>
delete a remote branch git push origin --delete <branch-name>

Track/untrack branches

action command
unset upstream from the current branch git branch --unset-upstream
set upstream to another local branch git branch --set-upstream <local-branch> origin/<remote-branch>

Push commits

action command
push to a new remote branch git push <remote-name> <local-branch-name>:<remote-branch-name>
(as above, shorter) git push <remote-name> <branch-name>


action command
temporarily switch to a different commit1 git checkout <commit-hash>
switch to a different commit and delete history git checkout --hard <commit-hash>

1: This command generates a detached head status. git checkout <branch-name> will get you back on the previous branch, while git checkout -b <new-branch-name> will move the current state to a new branch.

Undo things

action command
undo all unstaged changes git checkout -- .
undo specific unstaged file git checkout -- <file-name>
reset to specific commit, throwing away uncommitted changes git reset --hard <commit-hash>
reset to specific commit, leaving your current files untouched1 git reset --soft <commit-hash>
change the most recent commit message (locally) git commit --amend

1: Useful to undo accidental commits.

Stash things

action command
stash all unstaged files git stash
unstash all stashed files git stash pop
delete all stashes git stash clear

Diff, show

action command
show code changes between two branches git diff <branch-1>..<branch-2>
show what files have changed between two branches git diff --stat <branch-1>..<branch-2>
compare file between two commits (works also in different branches) git diff <commit-hash-1>..<commit-hash-2> <file>
show a file in another branch git show <branch>:<file>
show files changed between two commits git diff --name-only <commit-hash-1> <commit-hash-2>
(as above, using HEAD) git diff --name-only HEAD~[M] HEAD~[N]
show diff on staged files git diff --staged


action command
show all tags git tag --list
create a new annotated* tag git tag -a <tag-title> -m '<tag-message>'
create a new lightweight* tag git tag <tag-title>
push all tags to a remote repository git push --tags
push a specific tag to a remote repository git push origin <tag-name>
checkout to a specific tag git checkout tags/<tag-name>
delete local tag git tag --delete <tag-name>
delete remote tag git push --delete origin <tag-name>

*: Annotated tags are meant for release while lightweight tags are meant for private or temporary object labels (source).


action command
show origin git remote show origin