Git Cheats


Initialize the local directory as a Git repository:

git init

Provide your user information, which will get attached to commit messages:

git config --global "<your_email>"
git config --global "<your_name>"

Create an empty Git repository in a hosted service such as GitHub, GitLab or Bitbucket. Set the URL of the remote repository, where local changes will be pushed to:

git remote add <remote_name> <remote_url>


git remote add origin

Verify the remote repository details:

git remote -v


Clone an existing repository located on a remote host:

git clone <remote_url>


git clone

Alternatively, clone a local repository:

git clone /path/to/repository


Stage all local files that have changed since the last commit:

git add .

Or stage a specific file:

git add <file_name>

Or stage all of the contents of a specific directory:

git add <dir_name>/*

Abort changes to a file, removing it from the staging area and reverting back to the version of the file from the previous commit:

git checkout -- <file_name>


Commit all staged files with a comment:

git commit -m "<comment>"

Modify the previous commit's message:

git commit --amend

Undo the previous commit:

git revert HEAD^

Revert back three commits:

git reset --hard HEAD~3

Drop all local changes and commits and point your local branch at the latest history from a remote repository:

git fetch <remote_name>
git reset --hard <remote_name>/<branch_name>


git fetch origin
git reset --hard origin/master


View all tags:

git tag

Create a tag:

git tag <tag_name>

Tag a release:

git tag v<major>.<minor>.<patch>

Apply a tag to a previous commit, where <commit_id> is the first 10 characters of the full commit id:

git tag <tag_name> <commit_id>

Delete a tag locally:

git tag -d <tag_name>


Push all local commits to the corresponding ("tracked") branch in the default remote repository:

git push

Specify the name of the remote repository and, optionally, the branch to push to:

git push <remote_name> <branch_name>

For example, to push to the master branch in the remote "origin" repository:

git push origin master

Push changes to local tags to the default remote repository:

git push --tags


Pull in the latest changes from the corresponding ("tracked") branch in the default remote repository:

git pull

The git pull operation is a combination of fetch and merge commands. Git tries to automate the process of merging changes, but this is not always possible and results in merge conflicts. Fix the conflicted files then mark them as merged with git add <file_name>.

You can specify the name of the remote repository to pull from:

git pull <remote_name>

And you can specify the branch:

git pull <remote_name> <branch_name>


git pull origin master


See all local branches:

git branch

Create a new branch locally:

git checkout -b <branch_name>

Switch back to an existing branch:

git checkout <branch_name>


git checkout master

Checkout a branch that exists in a remote repository but not the local repository:

git checkout -b <local_branch_name> <remote_name>/<remote_branch_name>


git checkout -b master origin/master

Checkout a specific tag or release:

git checkout <tag_name>

Delete a local branch:

git branch -d <branch_name>

Use -D to force deletion of a local branch.

Delete a remote branch:

git push origin :<branch_name>


git branch -d feature/issue-301
git push origin :feature/issue-301

You might be able to restore a branch that was deleted in the last 30 days. Find it in git reflog, re-create the branch from the reflog HEAD point or the commit id:

git reflog
git checkout -b <branch_name> HEAD@{25}


git reflog
git checkout -b <branch_name> <commit_id>


To temporarily stash away your recent changes, then restore them after doing some other stuff such as switching to a new working branch:

git stash
git checkout -b <branch>
git stash pop

Other useful commands

Assign blame for errors or merge conflicts on a specific file:

git blame <file_name>

To find out what you can do with git log:

git log --help

Use the built-in Git GUI:


Make Git output more colourful:

git config color.ui true