Git Cheat Sheet

There are already some great Git Cheat Sheets available online but I still find myself referring back to my own notes when I want to check something that I don’t do everyday. So for future reference here is the subset of Git commands that I tend to use most often:

Getting Started

Create new repository

$ git init     (in project working directory)

Adding files and committing

$ git add .    (adds all files under current directory)
$ git commit -m 'initial commit'

Checking status

$ git status
$ git log    [--pretty=oneline]

Cloning a repository from github

Cloning a public repository

$ git clone git://github.com/path-to-repository/project.git

Cloning a private repository (to which we can later push changes):

$ git clone git@github.com:username/project.git

Synchronising changes

Pull changes from original source of clone (no need to specify remote repository name as it is stored when the repository was first cloned):

$ git pull

Make changes, add and commit then push updates to remote repository

$ git push

Branches

$ git branch mybranch     (create the branch named 'mybranch')
$ git checkout mybranch   (switch to the branch)
$ git checkout master     (switch back to the master branch)
$ git branch              (list all branches)

Pushing a branch

$ git push origin mybranch

Merging a branch

$ git checkout master   (first switch to the master branch)
$ git merge mybranch    (merge all changes from mybranch into the master branch)
$ gid add <file>        (fix and add any conflicts)
$ git commit -m 'merged changes'

Delete a branch

$ git branch -d mybranch

Deleting a remote branch

$ git push origin :mybranch

Tags

Showing tags

$ git tag [-l v1.*]

Create annotated tag

$ git tag -a v1.5 -m 'version 1.5'

Tagging an existing commit

$ git tag -a v1.1 -m 'version 1.1' <*checksum*>  (checksum from git log)

Pushing tags

$ git push --tags

Logs

View changes (patches)

$ git log -p

View Stats

$ git log --stat

Use one line output

$ git log --pretty=oneline

View branches

$ git log --pretty=oneline --graph

Search commit messages

$ git log --grep='pattern' --pretty=oneline

File History

$ git log --pretty=oneline -- <file>

Directory History

$ git log --pretty=oneline --all -- <dir>

Do not show merges

$ git log --pretty=oneline --no-merges

Show last 5 commits

$ git log --pretty=oneline -5

Commits between two other commits

$ git log master..mybranch --pretty=oneline
$ git log

Diff

Show modifications not yet staged

$ git diff

Show modifications not yet committed

$ git diff --staged

Show all changes since last commit

$ git diff HEAD

Show changes to a file since a specific (tagged) commit

$ git diff v1.0 -- <file>

Show stats for differences between two (tagged) commits

$ git diff v1.0 v1.1 --stat

Undo Changes

Unstage a file

$ git reset HEAD <file>

If you cannot remember this create an alias as follows:

$ git config --global alias.unstage 'reset HEAD'
$ git unstage <file>

Discard changes to a file

$ git checkout -- <file>

Breaking Existing Commits to a Branch

Make a note of the SHAR for the commit that you want to rollback to:

$ git log --pretty=oneline

Then switch to the branch and reset the head to the required commit

$ git branch mybranch
$ git reset --hard <SHAR>

If the previous head was pushed to a repository

$ git push --force     (deletes all later changes!)