No matter whether you’ve just taken a first look at a Git tutorial or already have some ex­per­i­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­stall­a­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­ur­a­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 "sample­ad­dress@sample.com"

Create a re­pos­it­ory

Create a new re­pos­it­ory or download an existing re­pos­it­ory.

Create and name a new local re­pos­it­ory git init sam­ple­name
Copy an existing re­pos­it­ory and its history with Git clone git clone "http://samples­ite.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 re­pos­it­ory git add *
Show unstaged changes to files with Git diff git diff
Show any changes between the staging area and the re­pos­it­ory git diff --staged
Track changes after commit git diff HEAD
Remove a file from the staging area (changes are kept) git reset sample­file
Add currently indexed files per­man­ently to the version history with Git commit git commit -m "Ex­plan­a­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 sampl­eb­ranch
Remove a remote branch git push origin --delete sampl­eb­ranch
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 re­pos­it­ory git fetch "http://www.samples­ite.com"
Fetch all branches git fetch --all
Download changes from the remote re­pos­it­ory ‘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­plan­a­tion git stash save "insert ex­plan­a­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 sampl­eb­ranch

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 re­pos­it­ory git push "http://www.samples­ite.com" local-sampl­eb­ranch
Transfer data to the remote server git push origin master
Force push git push -f
Remove remote branch via push git push origin --delete sampl­eb­ranch
Pull history from external re­pos­it­ory 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 -- 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 sample­file

or

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

or

git restore sample­file
Tip

Whether a static website or a single-page ap­plic­a­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 scalab­il­ity.

Reviewers

Go to Main Menu