Git Cheatsheet

Clone repository

git clone <repo_url> [folder_name]

Create branch

git branch <branch_name>

Checkout branch

git checkout <branch_name>

View code differences

# 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>

Create branch and checkout

git checkout -b <branch_name>

Add new remote

git remote add <remote_name> <repo_url>

Track remote branch

git branch -t <branch_name> <remote_name>/<remote_branch>

Delete local branch

# Delete merged branch
git branch -d <branch>

# Delete non-merged branch (or force deletion)
git branch -D <branch>

Delete remote branch

git push origin :<branch_name>

Create tag

git tag -a <tag_name> -m "<comment>"

Push tag to origin

git push origin <tag_name>

Delete local tag

git tag -d <tag_name>

Delete remote tag

git push origin :<tag_name>

Checkout tag

git checkout -b <branch_name> <tag_name>

Clean local repo

git gc --prune

Clean remote repo

git remote prune <remote>

Restore deleted file

git reset HEAD <file>
git checkout <file>

Undo last push

Use this instruction with caution.

git push -f <remote> <commit_sha1>:<branch_name>

Undo staged changes

# 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

Undo last pull

git reset --hard

Undo last commit

git reset --soft HEAD^

Blame

git blame -L <line_from>,<line_to> <file>

Amend last commit changes

git add <other files>
git commit --amend

Amend last commit author

git commit --amend --author "Your name <your.email@address.com>"

Unstage files

git reset HEAD <file>

Reset file

# To a specific commit
git checkout <commit_sha1> <file>

# To a specific branch
git reset <remote>/<branch> <file>

Resolve conflicts with checkout

# Resolve accepting the remote changes
git checkout --theirs <file>

# Resolve accepting the local changes
git checkout --ours <file>

Revert commit or merge

git revert -m 1 <commit_sha1>

Clean untracked files from working directory

git clean -f [-i]

Squash commits

# 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!

Contribute!

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.

Share!