Git Cheatsheet

This site is a reference for Git

Last updated on 09 March, 2021 at 13:59:41 Optimized for

Git is a popular VCS (Versioning Control System) developed by Linus Torvalds and released in 2005. It is a distributed version-control system for tracking changes in any set of files. It was developed to allow programmers coordinating their work on source code during software development. Imagine where we would be without! Other VCS exists but Git has gained an enormous adoption since its release.

Website logo
For the full experience we recommend viewing this website on a desktop or tablet.


How to use Branches

Branches are unique versions of your repository which are used to work on the codebase independently from other changes, e.g. a feature branch, a hotfix branch, experimental feature branch.

Command Description
git branch

List all local branches

git branch -a

List remote and local branches

git checkout -b branch_name

Create a local branch and switch to it

git checkout branch_name

Switch to an existing branch

git push origin branch_name

Push branch to remote

git branch -m new_name

Rename current branch

git branch -d branch_name

Delete a local branch

git push origin :branch_name

Delete a remote branch


Using Git Logs to Your Advantage

Show commit history, changes done to a file, who changed what and when and more.

Command Description
git log --oneline

Show commit history in single lines

git log -2

Show commit history for last N commits

git log -p -2

Show commit history for last N commits with diff

git diff

Show all local file changes in the working tree

git diff myfile

Show changes made to a file

git blame myfile

Show who changed what & when in a file

git remote show origin

Show remote branches and their mapping to local


How To Cleanup with Git

Delete untracked files, undo local changes and unstaging files.

Command Description
git clean -f

Delete all untracked files

git clean -df

Delete all untracked files and directories

git checkout -- .

Undo local modifications to all files

git reset HEAD myfile

Unstage a file


Tagging is used to capture a point in the commit history, such as a realease version.

Command Description
git pull --tags

Get remote tags

git checkout tag_name

Switch to an existing tag

git tag

List all tags

git tag -a tag_name -m "tag message"

Create a new tag

git push --tags

Push all tags to remote repo


Stash your changes temporarily

Stashes take your uncomitted (staged or unstaged) changes and saves them away for later use.

Command Description
git stash save "stash name" && git stash

Save changes to a stash

git stash list

List all stashes

git stash pop

Apply a stash and delete it from stash list

Online Resources & Books