Show Menu
Cheatography

COMMIT IDENTI­FIC­ATION

<br­anc­h_n­ame>
Last commit on the branch
HEAD
Current commit (parent for next commit)
HEAD^
parent of HEAD
HEAD~n
n-th level parent of HEAD
HEAD@{n}
HEAD n moves ago (reflog)
SHA1 ID (4-20 charac­ters)
Unique commit identifier
HEAD~0 == HEAD
HEAD~1 == HEAD^

WORK CYCLE

Create branch for new feature
git checkout develop
git checkout -b fb/new­_fe­atu­re_­branch
Add change
# make changes
make compile
git status
git add file.c
git diff --cached (or --staged)
git commit
Rebase
git pull --rebase origin develop
Push unfinished feature branch at the EoD
git push origin fb/new­_fe­atu­re_­branch -f
Merge with develop when finished
# ensure we’re rebased on latest develop
git checkout develop
git pull
git merge fb/new­_fe­atu­re_­branch --no-ff
Push develop
git push origin develop
Remove feature branch
git branch -d fb/new_feature_branch
git push origin :fb/ne­w_f­eat­ure­_branch

ADD CHANGES

commit [-m "­commit msg"]
Commit staged changes
commit -a
Add & commit all tracked files
commit --amend
Add changes to last commit
add [file]
Stage file
add -p
Add chunks intera­ctively
diff --staged
Inspect staged changes

UNDO CHANGES

checkout .
Undo uncommited changes
reset HEAD
Unstage files
reset --soft HEAD^
Undo commit (to stage)
reset --hard HEAD^
Undo commit
revert <co­­mm­i­t>
Revert existing commit

BRANCHES

branch -avv
List all branches with refs
branch <na­me> [<c­omm­it>]
Create branch (at <co­mmi­t>)
checkout -b <br­anc­h_n­ame>
Create & checkout new branch
checkout <br­anc­h> [file]
Checkout workspace (or file)
branch -m <ne­w_b­ran­ch_­nam­e>
Rename current branch
branch -d|-D <br­anc­h>
Delete merged­/un­merged branch

MERGE

merge <wi­th_­bra­nch>
Merge branch (default: fast-f­orward)
merge <wi­th_­bra­nch> --no-ff
Merge branch (no fast-f­orward)

REBASE

rebase <ne­w_r­oot­_co­mmi­t>
Rebase current branch
rebase -i <st­art­_co­mmi­t>
Intera­ctive rebase
rebase -i HEAD~n
Intera­ctive rebase (last n commits)
 

REMOTES

remote -v
Show all remotes
remote add <na­me> <ur­l>
Add remote
remote remove <na­me>
Remove remote

REMOTE BRANCHES

fetch [<r­emo­te>]
Get remote changes
pull [<r­emo­te>]
Get & merge remote changes
pull --rebase [<r­emo­te>] [<b­ran­ch>]
Get & rebase on remote changes
push [<r­emo­te>] [<b­ran­ch>] [-f]
Push local branch to remote
push -n
Dry-run push
remote prune origin
Clean all old remote branch references

RESET

reset <mo­de> [<c­omm­it>]
Reset current branch to <co­mmi­t>
reset --soft HEAD^
Undo commit
reset HEAD~n
Undo last n commits
--soft - do not touch working tree/index
--mixed - resets index
--hard - resets working tree &­ index

CHERRY­-PICK

cherry­-pick <co­mmi­t>
Apply alread­y-e­xisting change
rebase --onto <ba­se> <st­art­^> <en­d>
Apply set of alread­y-e­xisting changes

STASH

stash
Stash workdir & index
stash list
List stashes
stash pop [--index]
Apply & delete stash
stash apply [--index]
Apply stash
stash pop [stash­@{n}]
Apply & delete n-th stash
stash drop [stash­@{n}]
Remove n-th or latest stash
stash clear
Remove all stashes
stash branch [<n­ew_­bra­nch­>]
Create branch from a stash

TAGS

tag
Liast tags
tag <ta­gna­me> [<c­omm­it>]
Add lightw­eight tag
tag -a <ta­gna­me> [-m “Tag message”]
Add annotated tag
tag -d <ta­gna­me>
Remove tag
push <re­mot­e> <ta­gna­me>
Push tag
push <re­mot­e> --tags
Push with all tags
push <re­mot­e> :<t­agn­ame>
Remove remote tag

BLAME

blame <fi­le> [-L start,­stop]
Check who modified file (lines start-­stop)

REFLOG

reflog
Show history of HEAD
branch <ne­w_b­ran­ch_­nam­e> <lo­st_­com­mit>
Recover deleted branch

CLEAN

gc
Manually clean garbage objects
clean -fd
Remove not tracked files and folders

BISECT

bisect start <ba­d_c­omm­it> <go­od_­com­mit>
Init bisect
bisect run test_s­cri­pt.sh
Run automated binary­-search
Error code 125 to skip current branch
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Git Cheat Sheet