6/29/2011 - 2:41 PM

Git cheat sheet

Git cheat sheet

Git cheat sheet

Common commands


Create repository:

git init

Add file:

git add <file>

Remove file:

git rm <file>

Move or rename file:

git mv <from> <to>

Commit changes:

git commit

Show changes:

git status

Show log:

git log

Show log with tags:

git log --decorate

Search thru commit messages:

git log --grep="<search>"


Show branches:

git branch

Create branch:

git branch <branch>

Create and checkout branch:

git checkout -b <branch>

Checkout branch:

git checkout <branch>

Checkout remote branch:

git checkout -b <branch> origin/<branch>

Rename branch:

git branch -m <from> <to>

Delete branch:

git branch -d <branch>

Delete remote branch:

git push origin :<branch>

Review branch changes:

git diff <branch>

Merge branch into current:

git merge <branch>

Resolve merge conflicts:

mate <file>
git add <file>
git commit

Discard branch changes:

git checkout -f master


Show tags:

git tag

Create tag:

git tag -a <tag>

Create tag for specific commit:

git tag -a <tag> <commit>

Show tag data:

git show <tag>

Update tag:

git tag -f <tag> <tag> -m "<message>"

Delete tag:

git tag -d <tag>

Delete remote tag:

git push origin :refs/tags/<tag>


Push to master:

git push origin master

Push with tags:

git push origin master --tags


Fetch from remote repository:

git fetch origin

Merge remote branch into current:

git merge origin/master

Fetch and merge into current branch:

git pull


Clone repository:

git clone <url>

Clone repository into directory:

git clone <url> <directory>

Clone specific branch:

git clone <url> -b <branch> <directory>

Clone with submodules:

git clone --recursive <url>


Add submodule to repository:

git submodule add <url> <path>

Update submodule:

git submodule update


Stash changes:

git stash

Show stashes:

git stash list

Show stash diff:

git stash show -p <stash>

Restore stash:

git stash apply

Restore stash and restage files:

git stash apply --index

Restore specific stash:

git stash apply <stash>

Remove stash:

git stash drop <stash>

Restore and remove stash:

git stash pop

Create branch from stash:

git stash branch <branch>


Show remotes:

git remote -v

Add remote:

git remote add origin <url>

Remove remote:

git remote rm origin


Clear the working tree:

git reset --hard HEAD

Undo a commit:

git reset --hard HEAD~1

Rollback to a specific commit:

git reset --hard <commit>


Get the number of commits in the current branch:

git log --pretty=oneline | wc -l


Set name:

git config --global "<name>"

Set email:

git config --global "<email>"

Set editor (e.g. TextMate or Sublime Text):

git config --global core.editor "mate -w"
git config --global core.editor "subl -w"

Use colors:

git config --global color.ui true