Git Cheat Sheet: Essential Git Commands (with PDF)

By Marko Denic
Contents
  1. Install and set up Git
  2. Install Git
  3. Set your name and email
  4. Initialize a new repository
  5. Clone a repository
  6. Stage and commit changes
  7. Check the status of your files
  8. Add a file to the staging area
  9. Add all changes to the staging area
  10. See unstaged changes
  11. See staged changes
  12. Commit the staged changes
  13. Create an empty commit
  14. Remove a file from the repository and the working directory
  15. View the commit history
  16. Show a commit's changes
  17. Branches
  18. List branches
  19. Show the current branch
  20. Create a new branch
  21. Switch to another branch
  22. Create a branch and switch to it
  23. Rename the current branch
  24. Delete a branch
  25. Merge a branch into the current branch
  26. Copy a file from another branch
  27. Remote repositories
  28. Add a remote
  29. Push commits to a remote
  30. Pull changes from a remote
  31. Undo changes
  32. Unstage a file (the opposite of git add)
  33. Unstage everything
  34. Discard changes in a file
  35. Change the last commit
  36. Remove a file from the last commit
  37. Revert a commit
  38. Go back to a specific commit
  39. Reset a branch to match the remote
  40. Stash and clean up
  41. Save uncommitted changes for later
  42. Reapply stashed changes
  43. 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.

bash
# 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.

bash
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.

bash
git init

Clone a repository

bash
git clone <repository-url>

Stage and commit changes

Check the status of your files

bash
git status

Add a file to the staging area

bash
git add <file>

Add all changes to the staging area

bash
git add .

See unstaged changes

bash
git diff

See staged changes

Shows what will go into the next commit.

bash
git diff --staged

Commit the staged changes

bash
git commit -m "Your message"

Create an empty commit

A commit with no changes. Handy for re-running a CI pipeline or deploy.

bash
git commit --allow-empty -m "Trigger deploy"

Remove a file from the repository and the working directory

bash
git rm <file>

View the commit history

bash
git log

# One line per commit
git log --oneline

Show a commit's changes

bash
git show <commit-hash>

Branches

List branches

bash
# Local branches
git branch

# Local and remote branches
git branch -a

# Most recently used first
git branch --sort=-committerdate

Show the current branch

bash
git branch --show-current

Create a new branch

bash
git branch <branch-name>

Switch to another branch

bash
git switch <branch-name>

# Older syntax, same result
git checkout <branch-name>

Create a branch and switch to it

bash
git switch -c <branch-name>

# Older syntax
git checkout -b <branch-name>

Rename the current branch

bash
git branch -m <new-branch-name>

Delete a branch

bash
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

bash
git merge <branch-name>

Copy a file from another branch

Takes the file as it is on the other branch, without switching branches.

bash
git restore --source <branch-name> -- <file>

# Older syntax
git checkout <branch-name> -- <file>

Remote repositories

Add a remote

bash
git remote add <name> <repository-url>

Push commits to a remote

bash
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

bash
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.

bash
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.

bash
git reset

Discard changes in a file

Destructive: the file goes back to how it was in the last commit.

bash
git restore <file>

Change the last commit

Only amend commits you haven’t pushed yet.

bash
# 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.

bash
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.

bash
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.

bash
# 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.

bash
git fetch origin
git reset --hard origin/<branch-name>

Stash and clean up

Save uncommitted changes for later

bash
git stash

# Include untracked files
git stash -u

Reapply stashed changes

bash
# 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

bash
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!