Git basics quick reference

A quick reference for some basic git commands, with a few links to more detailed explanations.  If you need to learn git, the git documentation itself is excellent.


branch – a moveable pointer to commits
HEAD – a special pointer that keeps track of which branch you’re on
tracking branch – tracks an upstream branch

What changed?

Show changes for files that are modified but not staged:
$ git diff

Show changes for files that are staged but not committed:
$ git diff --staged
$ git diff --cached

(staged is a synonym of cached)

What happened?

Show the git commit history
$ git log

What’s what?

$ git status


Create new branch:
$ git branch mybranch

Switch to a branch:
$ git checkout mybranch

Create a new branch and switch to it:
$ git checkout -b mybranch

Delete branch:
$ git branch -d mybranch

Merge back into your master branch:
$ git checkout master
$ git merge mybranch

List local branches:
$ git branch

List branches with last commit message:
$ git branch -v

You’ve created a local branch ‘mybranch’, and you want to push it to a new remote branch ‘origin/mybranch’ (and set the local one to track the remote one).
Push the local branch to a remote branch and set the remote branch as being the local branch’s upstream branch:
$ git push -u origin mybranch

Get remote branch from remote and switch to it:
$ git checkout -b otherbranch origin/otherbranch
(may need to do a git pull first)

Remote repositories

Copy a remote repository:
$ git clone repo directory
repo – the remote repository
directory – the local directory to copy to

List all remote repositories:
$ git remote

List all remote repositories, with more information (verbose):
$ git remote -v

Show even more details about a remote repository:
$ git remote show [remote-name]
$ git remote show origin

(origin – default name for remote repository)

Uh-oh, undo!

Undo last commit:
$ git reset HEAD^
(The caret (^) after HEAD moves head back through commits. HEAD^ is short for HEAD^1 and in the same way you can apply HEAD^2 to go back two commits ago.)

Undo uncommitted change:
$ git checkout <filename>

Revert committed and pushed changes:
$ git revert f4d8d2c2ca851c73176641109172780487da9c1d
(grab the commit identifier of the commit you want to revert from $ git log)