Git Cheat Sheet

No matter whether you’ve just taken a first look at a Git tutorial or already have some experience 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 installation

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.

Configuration

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 "sampleaddress@sample.com"

Create a repository

Create a new repository or download an existing repository.

Create and name a new local repository git init sample name
Copy an existing repository and its history with Git clone git clone "http://samplesite.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 sample file
Add all files from a repository git add*
Show all new or changed files with Git diff git diff
Show any changes between the staging area and the repository git diff --staged
Track changes after commit git diff HEAD
Show differences between index and current stage git reset sample file
Add currently indexed files permanently to the version history with Git commit git commit -m "Explanations for the changes"

Branches

Group changes into branches and integrate new features.

Create and edit

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

Switch branch

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

Merge and fetch

Merge history of a branch with the current branch git merge sample name
Register external repository and swap history git fetch "http://www.samplesite.com"
Fetch all branches git fetch -all
Register a local repository 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 explanation git stash save "insert explanation 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 intermediate stashes git stash drop
Delete all available stashes git stash clear
Save to a separate branch git stash branch sample branch

Push and pull

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

Integrate external branch with current local branch git push "http://www.samplesite.com" local sample 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 samplebranch
Pull history from external repository 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>…<until>
Show commits for a specific file git log sample 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 samplefile or git restore --staged samplefile
Discard local changes to a file in the staging area git checkout --samplefile oder git restore samplefile
Tip

Whether a static website or a single-page application, 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 scalability.

Was this article helpful?
Page top