Skip to content

Git cheatsheet

Below you will find examples of the most used commands of the Git version control system.

How to create a Git repository

git init

How to create a branch

git branch <branch_name>

or

git checkout -b <branch_name>

or with the newer, more readable command:

git switch -c <branch_name>

How to list branches

git branch -l

How to delete a local branch

git branch -d <branch_name>

How to delete a remote branch

git push -d origin <branch_name>

Make sure you really want to. It is best to make a copy or rename the branch first.

How to rename a local branch

git branch -m <old_name> <new_name>

How to rename a remote branch

Rename the local branch:

git branch -m <old_name> <new_name>

Push it to the server:

git push -u origin <new_name>

Delete the remote branch:

git push -d origin <branch_name>

How to switch to a branch

git checkout <branch_name>

or:

git switch <branch_name>

How to create a commit

Add all files to the commit:

git add .

Create the commit:

git commit -m "A short description of what this commit does"

How to switch to a commit

git checkout <commit_hash>

How to start a new branch from a commit

Go to the commit:

git checkout <commit_hash>

Create the new branch:

git checkout -b <new_branch_name>

How to push a branch to the server

While on the branch you want to push:

git push -u origin <branch_name>

How to push more commits to a branch already on the server

git push

How to fetch changes from the server

git fetch

How to fetch and merge changes from the server

Fetches all changes and merges the ones for the current branch, if it is linked to the server (origin):

git pull

How to check the state of the repository

Which files have changed and which are already staged for the commit:

git status

How to browse the history

Every commit on one line, with the branches drawn alongside:

git log --oneline --graph

How to put changes aside

When you have to switch to another branch quickly and your current work is not ready to commit yet:

git stash

When you come back, bring the changes back:

git stash pop

How to undo changes

git revert, git reset, git restore and the life-saving git reflog are covered in a separate article: Git - undoing changes.

Advertisement