Phil's Git Cheatsheet

Getting Started

Do this first!!!

Setup .gitattributes correctly
Setup .gitignore correctly
Setup ~/.gitconfig correctly
config [--global] --unset user.email
config [--global] --edit
config [--global] user.email Phil@foo.com
config --list

Help

verb --help
man git-verb

Repository Creation and Cloning

init
clone url [mydir]

Using a Bare Repo as a Central Repo

1a. init --bare newrepo.git
1b. clone --bare url
2a. remote add origin user@server:path/to/newrepo.git
2b. remote set-url origin user@server:path/to/newrepo.git
2c. clone user@server:path/to/newrepo.git
3. push --all origin

Graphical Tools

gui
gitk [--all] [file]
gui blame filespec
mergetool
difftool

Other Docs

Scott Chacon's site
Andrew Peterson's cheat sheet
How to escape a git mess
Stack Overflow git intro
Main git doc page at kernel.org.
Git Flow

Daily Work

Staging

add pathspec
add -A pathspec
add -i pathspec
mv oldfile newfile

Un-Staging

reset [sha] pathspec
checkout [sha] -- pathspec
rm --cached pathspec
rm pathspec
rm 'foo/*.log'

Committing

commit [-m "message"]
commit -a
commit --amend [-m "message"]
commit --amend --no-edit
commit pathspecs

Undoing

show HEAD~4:File > OldFile
reset --soft sha
reset --soft HEAD^
reset [--mixed] sha
reset --hard sha
reset --hard HEAD^
reset --hard HEAD
reset --hard ORIG_HEAD

Reverting

revert [shas]
revert --continue
revert --quit
revert --abort

Cleaning

clean [-f] [-d]
clean -i
clean -n
clean -x
filter-branch

Branching

Managing

branch -r | -a
branch -v | -vv
branch -merged
branch -no-merged
branch newbrname [sha]
branch --track newbr short/brname
branch -d brname
show-branch [-r] [-a] [brnames]
push -u shortname brname

Switching To

checkout [-f | -m] brname
checkout -b newbrname [sha]
checkout --track shortname/brname
checkout -b newname shortname/brname

Merging and Rebasing

merge [--no-commit] sourcebr
mergetool
manual resolution
merge --abort
rebase [--onto newbase] brname
rebase -i brname
rebase --continue
rebase --abort
checkout --ours | --theirs
cherry-pick sha
merge-base sha1 sha2...

Branch/Rebase/Fast-Forward Workflow

1. checkout master
2. pull
3. checkout -b f-myfeature
4. work on your feature
5. fetch origin (when done)
6. diff origin/master (Optional)
7. rebase -i origin/master
8. checkout master
9. pull
10. merge [--no-ff] f-myfeature
11. branch -d f-myfeature
12. push

Git Flow

Introduction

The master branch
The develop branch
Feature branches
Release branches
Hotfix branches

Working on a feature

1. checkout develop; git pull
2. checkout -b f-myfeature
3. work on your feature
4. rebase -i develop (Optional)
5. checkout develop; git pull
6. merge --no-ff f-myfeature
7. branch -d f-myfeature
8. push origin develop

Creating a release

1. checkout develop; git pull
2. checkout -b release-1.2
3. ./bump-version.sh 1.2
4. commit -am 'Bump version to 1.2'
5. time passes ... apply hotfixes ...
6. checkout master; git pull
7. merge --no-ff release-1.2
8. tag -a 1.2
9. checkout develop; git pull
10. merge --no-ff release-1.2
11. branch -d release-1.2
12. push

Making hotfixes

1a. checkout -b hotfix-1.2.1 master
1b. checkout -b hotfix-0.9.1 0.9
2. ./bump-version.sh 1.2.1
3. commit -am 'Bumped version to 1.2.1'
4. work on the hotfix
5. commit -am 'Fixed production problem.'
6. checkout master; git pull
7. merge --no-ff hotfix-1.2.1
8. tag -a 1.2.1
9. checkout develop; git pull
10. merge --no-ff hotfix-1.2.1
11. branch -d hotfix-1.2.1

Remotes

Setup

remote [-v]
remote add shortname url
remote set-url shortname url
remote rm shortname
remote rename oldname newname
remote show shortname

Fetching and Pulling

fetch [shortname]
fetch --tags [shortname]
pull [--rebase] [shortname]

Manual Fetch and Merge

fetch shortname
diff master shortname/master
merge shortname/master
rebase [-i] shortname/master

Pushing

push
push -u shortname brname
push --all -u shortname
push shortname brname:brname2
push shortname :brname
push shortname v1.5
push shortname --tags

Subtrees (Sharing code)

Introduction
subtree add ...
subtree pull ...
subtree push ...
Terminating the link

Reviewing

Comparing

diff [-- path] [--stat]
diff --cached [sha] [-- path]
diff sha [-- path]
diff sha1 sha2 [-- path]
diff HEAD~ -- filespec
diff sha1\...sha2 [-- path]
diff --name-status --oneline sha1 sha2
diff :1:filespec :3:filespec
difftool

Status and History

status [--short] [--ignored]
gitk [--all] [file]
log [options] revisionrange
log [options] brname
log [options] -- pathspec
log [-N]
log --[after|before]=datespec
log -p
log --stat | --shortstat
log --author=somebody
log --committer=somebody
log --grep=pattern
log -S'string'
log -G'pattern'
log -L start,end:file
log -i
log --all-match
log origin/master..HEAD
log --pretty=...
blame filespec
gui blame filespec

Commit Ranges

log sha
log ^sha
log sha1..sha2
log master..f-myfeature
log f-myfeature..master
log origin/master..HEAD
log ^exc1 ^exc2 inc1 inc2

Misc 1

Showing Objects

revparse ref
reflog [log options]
show [options] [things]
show sha1
show ref
show ref:pathspec
show :pathspec
show @
show ref@{date}
show ref@{N}
show rev~N
show rev^N
show HEAD~4:File > OldFile

Stashing

stash list
stash save [--untracked | --all] [msg]
stash show [stash]
stash pop [stash]
stash apply [stash]
stash branch brname [stash]
stash drop [stash]
stash clear

Tagging

tags vs branches
tag
tag -l v1.4*
show v1.4
tag -a v1.4 -m "message"
tag -a v1.4 sha
push shortname v1.5
push origin --tags
tag -f v1.4 sha
tag -d v1.4

Bisection

bisect start
bisect run my_script args

Plumbing Commands

ls-files -s | -u
cat-file -p sha

Inspired by Oscar Otero's wonderful jQuery Cheatsheet and much reading of Scott Chacon's git-scm.com from where I borrowed the favicon (Licence). Andrew Peterson's funky Git Cheatsheet supplied the inspiration for the SWILR boxes.

Built with Lithogen and HTML 5 Boilerplate with popups by jQuery Colorbox.

All errors and omissions are my own. If you like this, Email me.

Theme:

comments powered by Disqus