git clone <repo_url> [folder_name]
git branch <branch_name>
git checkout <branch_name>
# Between working area and default branch (usually master)
git diff
# Between staged changes and default branch (usually master)
git diff --staged
# Between working area and some other branch
git diff <remote>/<branch>
# Between working area and stashed changes
git stash show -p
git stash show -p stash@{1}
git checkout -b <branch_name>
git remote add <remote_name> <repo_url>
git branch -t <branch_name> <remote_name>/<remote_branch>
git checkout <branch_name>
git branch -u <remote_name>/<remote_branch>
# Delete merged branch
git branch -d <branch>
# Delete non-merged branch (or force deletion)
git branch -D <branch>
git push origin :<branch_name>
git branch --merged
git tag -a <tag_name> -m "<comment>"
git push origin <tag_name>
git tag -d <tag_name>
git push origin :<tag_name>
git checkout -b <branch_name> <tag_name>
git tag --contains <commit_id>
# Or
git describe --contains <commit_id>
git gc --prune
git remote prune <remote>
git reset HEAD <file>
git checkout <file>
Use this instruction with caution.
git push -f <remote> <commit_sha1>:<branch_name>
# Unstage changes on a tracked file
git reset HEAD <file>
# Unstage changes on a currently-untracked file
git rm --cached <file>
# Unstage changes on the working copy
# (use this instruction with caution, you could lose data)
git checkout -f
git reset --hard
git reset --soft HEAD^
# HEAD@{1} gives you the commit that HEAD pointed at before
git reset --soft HEAD@{1}
git blame -L <line_from>,<line_to> <file>
git add <other files>
git commit --amend
git commit --amend --author "Your name <your.email@address.com>"
git commit --amend --date="$(date -R)"
git reset HEAD <file>
# To a specific commit
git checkout <commit_sha1> <file>
# To a specific branch
git reset <remote>/<branch> <file>
# Resolve accepting the remote changes
git checkout --theirs <file>
# Resolve accepting the local changes
git checkout --ours <file>
git revert -m 1 <commit_sha1>
git clean -f [-i]
# Get the base commit for your branch
$ git merge-base your-branch master
# Use the hash returned by the previous command to rebase
$ git rebase -i <HASH>
# Previous command will open up your text editor with something like this:
# pick 1fc6c95 do something
# pick 6b2481b do something else
# pick dd1475d changed some things
# pick c619268 fixing typos
#
# Replace `pick` for `squash` in all lines but first, like this:
# pick 1fc6c95 do something
# squash 6b2481b do something else
# squash dd1475d changed some things
# squash c619268 fixing typos
# Save and close the file, wait for a new instance of your text editor,
# edit your commit message and enjoy your brand new commit!
(Available since Git 2.35)
git stash push -S
If you have commands, tips, suggestions or improvements that could make the cheatsheet richer or if you see something that is not accurate or right, please, feel free to propose the changes either on GitHub or in the comments below.