Cheat sheets

Cheat sheet: Git

The Git commands you use daily, organized by task.

Getting started

Create a new repository git init
Clone a repository git clone https://github.com/user/project.git
Set your name git config --global user.name "Your Name"
Set your email address git config --global user.email "you@example.com"
Set the default branch to main git config --global init.defaultBranch main

Daily work

What has changed? git status
View the changes git diff
View staged (what will be committed) git diff --staged
Stage all changes git add -A
Choose piece by piece what to stage git add -p
Commit with a message git commit -m "Short description"
Amend the last commit git commit --amend
Show history in short form git log --oneline --graph --decorate
History of a single file git log -p file

Branches

Show branches git branch
Create a new branch and switch to it git switch -c new-branch
Switch to a branch git switch main
Merge a branch into the current one git merge new-branch
Rebase the current branch onto main git rebase main
Delete a branch (only if merged) git branch -d new-branch
Cherry-pick a single commit git cherry-pick COMMIT

Collaboration

Show remote repositories git remote -v
Fetch and update git pull --rebase
Push your work git push
Push a new branch for the first time git push -u origin new-branch
Only fetch without merging git fetch --all --prune

Undoing

Restore a file to the last commit git restore file
Unstage a file git restore --staged file
Undo the last commit, keep changes git reset --soft HEAD~1
Undo a commit with a new commit git revert COMMIT
Reset everything to the last commit (warning, deletes work!) git reset --hard HEAD
Show untracked files that would be removed git clean -nd

Stashing and searching

Stash changes temporarily git stash
Retrieve stashed changes git stash pop
Who changed this line? git blame file
Create a tag git tag v1.0.0
Find lost commits git reflog

Good to know

  • Better create a new commit (git revert) than rewrite history you’ve already shared.
  • Never put passwords and keys in a repository, not even “for a moment”; they stay in the history. Use a .gitignore.

Sources and attribution

This overview is written in our own words. Commands and options change per version: when in doubt, check the official documentation.