Git Cheat Sheet: Essential Git Commands (with PDF)
Contents
- Install and set up Git
- Install Git
- Set your name and email
- Initialize a new repository
- Clone a repository
- Stage and commit changes
- Check the status of your files
- Add a file to the staging area
- Add all changes to the staging area
- See unstaged changes
- See staged changes
- Commit the staged changes
- Create an empty commit
- Remove a file from the repository and the working directory
- View the commit history
- Show a commit's changes
- Branches
- List branches
- Show the current branch
- Create a new branch
- Switch to another branch
- Create a branch and switch to it
- Rename the current branch
- Delete a branch
- Merge a branch into the current branch
- Copy a file from another branch
- Remote repositories
- Add a remote
- Push commits to a remote
- Pull changes from a remote
- Undo changes
- Unstage a file (the opposite of git add)
- Unstage everything
- Discard changes in a file
- Change the last commit
- Remove a file from the last commit
- Revert a commit
- Go back to a specific commit
- Reset a branch to match the remote
- Stash and clean up
- Save uncommitted changes for later
- Reapply stashed changes
- Clean up and optimize the repository
The Git commands you’ll actually use, grouped by task: setting up, saving changes, branches, remotes, and undoing mistakes. Every command is ready to copy. Replace the parts in <angle brackets> with your own values.
Install and set up Git
Install Git
On Windows you can also download the installer from git-scm.com. On macOS, running git for the first time offers to install it with the Xcode Command Line Tools.
# Windows
winget install --id Git.Git -e
# macOS
brew install git
# Ubuntu / Debian
sudo apt install git
# Check that it worked
git --version
Set your name and email
Every commit records who made it. --global sets it once for all your repositories.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Initialize a new repository
The git init command creates a new, empty Git repository in the current directory, with a .git folder that tracks changes to the files in it.
git init
Clone a repository
git clone <repository-url>
Stage and commit changes
Check the status of your files
git status
Add a file to the staging area
git add <file>
Add all changes to the staging area
git add .
See unstaged changes
git diff
See staged changes
Shows what will go into the next commit.
git diff --staged
Commit the staged changes
git commit -m "Your message"
Create an empty commit
A commit with no changes. Handy for re-running a CI pipeline or deploy.
git commit --allow-empty -m "Trigger deploy"
Remove a file from the repository and the working directory
git rm <file>
View the commit history
git log
# One line per commit
git log --oneline
Show a commit's changes
git show <commit-hash>
Branches
List branches
# Local branches
git branch
# Local and remote branches
git branch -a
# Most recently used first
git branch --sort=-committerdate
Show the current branch
git branch --show-current
Create a new branch
git branch <branch-name>
Switch to another branch
git switch <branch-name>
# Older syntax, same result
git checkout <branch-name>
Create a branch and switch to it
git switch -c <branch-name>
# Older syntax
git checkout -b <branch-name>
Rename the current branch
git branch -m <new-branch-name>
Delete a branch
git branch -d <branch-name>
# Force-delete a branch that isn't merged
git branch -D <branch-name>
Merge a branch into the current branch
git merge <branch-name>
Copy a file from another branch
Takes the file as it is on the other branch, without switching branches.
git restore --source <branch-name> -- <file>
# Older syntax
git checkout <branch-name> -- <file>
Remote repositories
Add a remote
git remote add <name> <repository-url>
Push commits to a remote
git push <remote> <branch>
# First push of a new branch: also set the upstream
git push -u origin <branch-name>
Pull changes from a remote
git pull <remote> <branch>
Undo changes
The commands people search for most. Commands marked destructive throw away work that isn’t committed, so run git status first.
Unstage a file (the opposite of git add)
The file stays changed, it’s just no longer staged for the next commit.
git restore --staged <file>
# Older syntax
git reset <file>
Unstage everything
Resets the staging area to the last commit. Your changes stay in the files.
git reset
Discard changes in a file
Destructive: the file goes back to how it was in the last commit.
git restore <file>
Change the last commit
Only amend commits you haven’t pushed yet.
# Fix the message
git commit --amend -m "New message"
# Add a forgotten file to it
git add <file>
git commit --amend --no-edit
Remove a file from the last commit
Undoes the commit but keeps all its changes staged, unstages the file, and commits again without it. The file’s changes stay in your working directory.
git reset --soft HEAD~1
git restore --staged <file>
git commit -m "Your message"
Revert a commit
Creates a new commit that undoes the changes from <commit-hash>. History isn’t rewritten, so this is the safe way to undo something that’s already pushed.
git revert <commit-hash>
Go back to a specific commit
Moves the current branch back to an earlier commit. Find the hash with git log --oneline. If you’ve already pushed, use git revert instead, or you’ll need to force-push.
# Keep the changes since then, unstaged
git reset <commit-hash>
# Destructive: throw away everything after that commit
git reset --hard <commit-hash>
Reset a branch to match the remote
Destructive: your local branch becomes an exact copy of the remote one, and local commits and changes are lost.
git fetch origin
git reset --hard origin/<branch-name>
Stash and clean up
Save uncommitted changes for later
git stash
# Include untracked files
git stash -u
Reapply stashed changes
# Apply and keep the stash
git stash apply
# Apply and remove it from the stash list
git stash pop
Clean up and optimize the repository
git gc
You can download the basics as a PDF here: Download Git Cheat Sheet. New to open source? See how to make your first open source contribution.
Happy coding!