Cheatsheet: Git

This cheatsheet is inspired by Git Book I’ve read a few days ago. Getting some useful stuff from other sources, I’ve built a list of commands which seem quite useful for my workflow.

Won’t chit-chat about version control importance and Git domination. You know it all. Just take a look what’s here.

Versions logfile

git log –pretty=format:”%h – %an, %ar : %s”

One of git log patterns to dive into the project history

git log –grep INFRA

Show commits with “INFRA” word in messages

git log -Smain

Show commits with “main” function in code diff

git log –oneline –graph

Check commit history with branching

git log –since=1.month

Show all commits for the last month only

git config –global alias.last ‘log -1 HEAD’

Useful alias to get the last commit message

git log –oneline –decorator

Show the pointers to current branches

git log mybranch –not master

Show all commits on mybranch out of master

git shortlog –no-merges

Contributors summary for all project history

git shortlog –no-merges master –not v1.1

Contributors summary since version 1.1

git reflog

Get the history of branch HEAD pointer movings

git log master…mybranch

“Three dots” means “show me all commits that exists at master and mybranch, but not at both branches”

git log –oneline –left-right –merge

Get commits conflicting with each other

git log –all –graph –pretty=format:’%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)%Creset’ –abbrev-commit –date=relative

One more log pretty format

Versions reading

git diff –staged

Explain the difference between “staged” and “commited” state

git describe master

Get the project version (by tag)

git ls-remote origin

Show remote repository refs such as branches HEAD and PRs

git show mybranch

Show the content of last commit on branch

git rev-parse mybranch

Get the last commit of branch

git show HEAD~5

Show the content of last 5 commits

git grep –count myfunction

Get number of myfunction matches at the codebase

git grep -n –break –heading myfunction

Myfunction grep ath the codebase with pretty format

git blame -L 11,20 utils/

Check who’s put the specific line of code, when (time) and where (commit)

git show :/typo

Get the latest commit matching a typo pattern

git rebase -i HEAD~10

Main instrument to refactor commit history

Three-trees workflow

git mv file_from file_to

Proper way to rename files under git

git commit –amend

Append staged state-files to the last commit

git reset HEAD file1

Rollback the file from “staged” to “modified” state

git checkout — file1

Drop all changes in the file

git add -i

Interactive files staging

git reset [–soft|–mized|–hard] HEAD

Rollback your [branch|index|working directory] to the previous commit state


git fetch –prune

Get the last state from remote repository, i.e. cleanup absent branches

git branch –no-merged

Get all not-yet-merged branches

git rebase –onto master branch2 branch1

Rebase to master changes from branch1 that hasn’t existed at branch2

git rerere

Reslove merge conflicts by one of saved patterns

git merge –abort

Abort conflicting merge

git checkout –conflict=diff3 conflict_file

Get the full merge conflict context

git merge -Xours mybranch

Automatically resolve merge conflicts to the branch you’re merging in


git read-tree –prefix=subproject/ -u subproject_remote

Merge the different git repository (fetched from remote) as a sub-tree

git diff-tree subproject_remote

Get the diff for your sub-tree

git merge –squash -s=recursive -Xsubtree=subproject subproject_remote

Merge the changes from remote subtree project

git submodule foreach ‘git stash’

Execute one command for each submodule

git push –recurse-submodules=on-demand

Push submodules to remote repositories

git submodule update –remote –rebase

Update submodule && rebase with local changes


git stash

Stash your files from staging/modification areas

git stash -u

Stash everything, even untracked files

git stash –patch

Stash only specific hunks


git config –global help.autocorrect 1

Configure git autocorrection

git config alias.sdiff ‘!'”git diff && git submodule foreach ‘git diff'”

git config alias.spush ‘push –recurse-submodules=on-demand’

git config alias.supdate ‘submodule update –remote –merge’

Useful aliases for work with submodules

git config –global commit.template ~/.gitmessage.txt

Set up commit template from text file

git config –global help.autocorrect 20

Autocorrect typos in command after 2.0 seconds

git config –global core.autocrlf true

Switch on linefeeds conversion for Windows systems

git config –global core.whitespace \


Configure whitespace alarms

echo ‘*.png diff=exif’ >> .gitattributes

echo “*.docx diff=word” > .gitattributes

Setup diffs for images and MS Word files

git config –global merge.ours.driver true

Always have an “ours” strategy with files marked with “merge=ours” in .gitattributes

export GIT_AUTHOR_NAME=”John Doe”


export GIT_COMMITTER_NAME=”John Doe”


Dynamically change author/committer of the next commit

git config –global help.autocorrect 1

Configure git autocorrection


git svn clone [svn_repo_url] -T trunk -b branches -t tags

Clone SVN repository like a Git repository

git svn dcommit

Push to remote SVN repository from a Git client

git svn info

Svn info analog

git clone hg::[mercurial_repo] /tmp/hello-git

Clone Mercurial repository like a Git repository (git-remote-hg tool required)

git svn clone [svn_repo_url] -T trunk -b branches -t tags

Clone SVN repository like a Git repository


git clean -d

Recursive cleanup of unstaged and non-ignored files.
Use -n flag for dry-run.

git rm $(git ls-files -d)

Clean up removed files

git stripspace

Strips trailing whitespace, collapses newlines, adds newline to end of file

Remote repository workflow

git push -u branchA:branchB

Push the local branchA to remote as a branchB

git push origin master:refs/heads/qa/master

Push local master branch to remote repository as a different topic

git push origin :topic

Trick to remove topic from remote repository


git archive –format zip

Archive your project

git bundle create repo.bundle HEAD master

Create a bundle of all commits

git clone repo.bundle repo

Deploy the bundle into new repository

git fetch ../commits.bundle master:other-master

Fetch the bundle into separate branch


git format-patch -M origin/master

Collect branch changes to one patch

git commit -m “Big-ass commit” –allow-empty

Commit with no changes


View all commits on a repo by author…master

Compare branches across forked repositories


Focus search bar

G + i

Go to Issues

G + p

Go to Pull Requests

G + c

Go to code

git commit -m “Fix screwup, fixes #12”

Any of the keywords fix/fixes/fixed, close/closes/closed or resolve/resolves/resolved, followed by the issue number, will close the issue once it is committed to the master branch.

[Link to a header](#awesome-section)

Insert relative link to header in Github Markdown



Create templates for Github issues and pull requests


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s