Skip to content

Support us

Authors: fire1ce, Adriaan Molendijk | Created: 2022-03-23 | Last update: 2022-08-12

Git Cli Cheat Sheet

Git is a free and open source distributed version control system designed to quickly and efficiently manage everything from small to very large projects.

Create Repositories

A new repository can either be created locally, or an existing repository can be cloned. When a repository was initialized locally, you have to push it to GitHub afterwards.

The git init command turns an existing directory into a new Git repository inside the folder you are running this command. After using the git init command, link the local repository to an empty GitHub repository using the following command:

git init

Specifies the remote repository for your local repository. The url points to a repository on GitHub.

git remote add origin [url]

Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits

git clone [url]

Git Configuration

Configure user information for all local repositories

Sets the name you want attached to your commit transactions

git config --global "[name]"

Sets the email you want attached to your commit transactions

git config --global "[email address]"

Enables helpful colorization of command line output

git config --global color.ui auto

Synchronize Changes

Synchronize your local repository with the remote repository on

Downloads all history from the remote tracking branches

git fetch

Combines remote tracking branches into current local branch

git merge

Uploads all local branch commits to GitHub

git push

Updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull is a combination of git fetch and git merge

git pull

Redo Commits

Erase mistakes and craft replacement history

Undoes all commits after [commit], preserving changes locally

git reset [commit]

If you don't want to reset absolutely, but relatively that is also possible using

git reset HEAD~2
which undoes the last 2 commits.

Discards all history and changes back to the specified commit

git reset --hard [commit]


Branches are an important part of working with Git. Any commits you make will be made on the branch you’re currently “checked out” to. Use git status to see which branch that is.

Creates a new branch

git branch [branch-name]

Switches to the specified branch and updates the working directory

git switch -c [branch-name]
or you can use

git checkout -b [branch-name]
to both create and switch to the branch simultaneously.

Combines the specified branch’s history into the current branch. This is usually done in pull requests, but is an important Git operation.

git merge [branch]

Deletes the specified branch

git branch -d [branch-name]

Make Changes

Browse and inspect the evolution of project files

Lists version history for the current branch

git log

Lists version history for a file, beyond renames (works only for a single file)

git log --follow [file]

Shows content differences between two branches

git diff [first-branch]...[second-branch]

Outputs metadata and content changes of the specified commit

git show [commit]

Snapshots the file in preparation for versioning

git add [file]

Records file snapshots permanently in version history

git commit -m "[descriptive message]"

The .gitignore file

Sometimes it may be a good idea to exclude files from being tracked with Git. This is typically done in a special file named .gitignore. You can find helpful templates for .gitignore files at If there are certain files (like .vscode or .ide) that should be discluded from all projects, you can create a global .gitignore file to do so.

Untrack Files Already Added to git Repository Based on .gitignore

Commit all your changes. Before proceeding, make sure all your changes are committed, including your .gitignore file. Remove everything from the repository. To clear your repo, use:

git rm -r --cached .

Re add everything.

git add .


git commit -m ".gitignore fix"

Use Gist as Repository

It's probably easiest if you just start by cloning the gist, so that origin (a "remote" that refers to the original repository) is set up for you. Then you can just do git push origin master. For example:

git clone mygist
cd mygist

Add you changes to the repository.

git add .
git commit -m "Better comments"
git push origin master

However, if you don't want to redo your changes, you can do:

cd mygist
git remote add origin
git fetch origin
# Push your changes, also setting the upstream for master:
git push -u origin master

Strictly speaking, the git fetch origin and -u argument to git push origin master are optional, but they will helpfully associate the upstream branch master in origin with your local branch master.