No matter whether you’ve just taken a first look at a Git tutorial or already have some ex­pe­ri­ence in using the version control system, with a clear Git cheat sheet, you’ll always have all the Git commands and codes at a glance.

Git Cheat Sheet

Version and in­stal­la­tion

To check which version of Git is currently on your computer, use the following command:

git --version

If you don’t find a version, you can download Git using the following links:

Git is open source and free of charge.

Con­fig­u­ra­tion

You need a username and a valid email address to work with Git. Here’s how to configure both:

Specify a name git config --global user.name "your name"
Enter your email address git config --global user.email "sam­plead­dress@sample.com"

Create a repos­i­to­ry

Create a new repos­i­to­ry or download an existing repos­i­to­ry.

Create and name a new local repos­i­to­ry git init sam­ple­name
Copy an existing repos­i­to­ry and its history with Git clone git clone "http://sam­ple­site.com"

Make changes

You can make, track and add changes.

Show the status of the directory git status
Show objects git show
Add a file git add sam­ple­file
Add all files from a repos­i­to­ry git add *
Show unstaged changes to files with Git diff git diff
Show any changes between the staging area and the repos­i­to­ry git diff --staged
Track changes after commit git diff HEAD
Remove a file from the staging area (changes are kept) git reset sam­ple­file
Add currently indexed files per­ma­nent­ly to the version history with Git commit git commit -m "Ex­pla­na­tions for the changes"

Branches

Group changes into branches and integrate new features.

Create and edit

Create a new branch git branch sam­ple­name
List all branches git branch --list
Delete a branch git branch -d sam­ple­branch
Remove a remote branch git push origin --delete sam­ple­branch
Rename a branch git branch -m new-name

Switch branch

Switch to another branch with Git checkout git checkout other-branch
Create a new branch and switch to it git checkout -b new-branch

Merge and fetch

Merge history of a branch with the current branch git merge sam­ple­name
Download the history of an external repos­i­to­ry git fetch "http://www.sam­ple­site.com"
Fetch all branches git fetch --all
Download changes from the remote repos­i­to­ry “origin” git fetch origin

Cache

Put current changes in your working directory into stash for later use with Git stash git stash
Save changes with an ex­pla­na­tion git stash save "insert ex­pla­na­tion here"
List stashes git stash list
Make new changes to a stash git stash apply
Track changes to the stash git stash show
Apply and delete stash directly git stash pop
Discard in­ter­me­di­ate stashes git stash drop
Delete all available stashes git stash clear
Save to a separate branch git stash branch sam­ple­branch

Push and pull

Use Git push and Git pull to commit to or retrieve from the remote server.

Push a local branch to an external repos­i­to­ry git push "http://www.sam­ple­site.com" local-sam­ple­branch
Transfer data to the remote server git push origin master
Force push git push -f
Remove remote branch via push git push origin --delete sam­ple­branch
Pull history from external repos­i­to­ry git pull
Pull data from server git pull origin master

Log

You check the history of a commit with Git log.

Show all commits for a branch git log
Limit the number of commits (in this example to three) git log -3
Search commits from a specific author git log --author="sample name"
Show commits for a specific time period git log --since=<since> --until=<until>
Show commits for a specific file git log -- sam­ple­file

Revert changes

You have two options to revert changes — Git revert or Git reset. For the latter, you can choose between the options “soft”, “hard” and “mixed”.

Modify existing commit git commit --amend
Remove file from staging area git reset HEAD sam­ple­file

or

git restore --staged sam­ple­file
Discard local unstaged changes to a file git checkout -- sam­ple­file

or

git restore sam­ple­file
Tip

Whether a static website or a single-page ap­pli­ca­tion, with Deploy Now from IONOS, you deploy your project in just three steps. During the process, you’ll benefit from useful features such as automatic framework detection and high scal­a­bil­i­ty.

Reviewers

Go to Main Menu