Git Cheatsheet

Find the right git command by describing what you want to do — not by memorizing command names.

Tired of memorizing Git commands? Use Tower — the Git client built for pros.

Visual Git client · Mac & Windows · Used by 100,000+ developers

Try Tower

Affiliate link — we may earn a commission at no extra cost to you.

Basics

Init, clone, commit, push, pull

Branching

Create, merge, rebase, switch

Undoing

Reset, revert, discard changes

Remote

Fetch, push, remotes, tracking

Stashing

Save, apply, list, drop stashes

History

Log, diff, blame, search

Tags

Create, push, delete tags

Advanced

Cherry-pick, bisect, worktrees

Git Cheatsheet: Essential Commands & Examples

Git is the world's most widely used version control system, letting developers track every change to every file across the full history of a project. Whether you're working solo or collaborating across teams, understanding core git commands is a non-negotiable skill.

This git cheat sheet is organized for two use cases: quick lookup when you know what you need, and a learning path when you're building fluency from scratch. Start at the top for a fast reference, or read through each section for deeper context and copy-paste examples.

Quick Reference: Most-Used Git Commands

A scannable cheat sheet of everyday git operations grouped by purpose.

Setup & Config

  • git config --global user.nameSet your identity name
  • git initInitialize a new repository
  • git clone <url>Copy a remote repository locally

Stage & Commit

  • git statusCheck working directory state
  • git add <file>Stage a file for commit
  • git commit -m "msg"Commit staged changes
  • git diffShow unstaged diff

Branch & Merge

  • git branchList all branches
  • git switch -c <name>Create and switch to new branch
  • git merge <branch>Merge branch into current branch

Remote Repositories

  • git remote add origin <url>Link a remote repository
  • git push origin <branch>Push commits to remote
  • git pull --rebaseFetch and rebase from remote
  • git fetch originDownload without merging

Undo & Recover

  • git restore <file>Discard local file changes
  • git reset --soft HEAD~1Undo last commit, keep staged
  • git revert HEADCreate new commit that undoes HEAD
  • git reflogBrowse full history log

Inspect & Log

  • git log --oneline --graphVisual branch log
  • git show <hash>Inspect a commit by hash
  • git blame <file>See who changed each line
  • git stashTemporarily shelve changes

Getting Started: Setup & Repositories

Before your first git commit, configure your identity. Git attaches your name and email to every commit you make, so every change in the repository is traceable back to its author.

Use git init to turn any local directory into a git repository, or git clone to copy an existing remote repository into a new local directory.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Initialize a new repository
git init
echo "# Project" > README.md
git add README.md
git commit -m "Initial commit"

Working with Changes

The everyday loop: check status, review the diff, stage files, and commit.

Before staging anything, use git status to check which files are modified, staged, or untracked files git doesn't yet know about. Then run git diff to see the exact line-level diff of every unstaged change.

Stage individual files with git add <file> rather than git add . when you want precise control over what enters the next commit. Use git commit --amend to fix the last commit message or add a forgotten file without creating a new commit.

git status
git diff

# Stage a specific file
git add src/app.js
git commit -m "Add app logic"

# Fix the last commit
git commit --amend --no-edit

Branching & Merging

Creating a new branch is one of git's most powerful features — it lets you isolate work without touching the current branch. Use git merge to combine branches and preserve full history, or git rebase to replay commits for a cleaner, linear log.

Merge workflow

git branch feature/login
git switch feature/login
# work, commit...
git switch main
git merge feature/login

Best for shared branches — preserves full merge context.

Rebase workflow

git switch feature/login
git rebase main
# resolve any conflict
git add .
git rebase --continue

Best for local feature branches — produces a linear log.

Working with Remote Repositories

Remote repositories are hosted versions of your project. Push to share your work, fetch to inspect changes, pull to integrate them.

Add a remote with git remote add origin <url> then push your branch upstream. Use git fetch origin to download changes without modifying your working directory — useful when you want to check what's changed on the remote before integrating.

git pull --rebase is often preferable to a plain pull — it avoids unnecessary merge commits when syncing your current branch with the remote.

git remote add origin git@github.com:user/repo.git
git push -u origin main

# Inspect before merging
git fetch origin
git log HEAD..origin/main --oneline

git pull --rebase

Undoing Changes & History

Knowing how to safely undo mistakes is what separates confident git users from anxious ones. Use git reset for local history rewrites and revert for shared branches where force-pushing would disrupt other users.

git restore

Discard edits to a file in your working directory without touching staged changes.

git reset

Move HEAD backward. --soft keeps changes staged; --hard discards them entirely.

git revert

Create a new commit that inverts a previous commit. Safe for shared branches.

# Discard local file edits
git restore src/config.js

# Unstage without losing edits
git restore --staged src/config.js

# Undo last commit, keep changes staged
git reset --soft HEAD~1

# Safe undo on a shared branch
git revert HEAD

# Recover a lost commit by hash
git reflog
git checkout HEAD@{2}

Stashing & Temporary Work

Git stash lets you shelve uncommitted work so you can switch context without losing progress.

Run git stash push -m "WIP login" to save your in-progress changes with a descriptive label. Use git stash list to track multiple stashes and git stash pop to restore the most recent one back onto your current branch.

Tip: You can apply a git stash to a different branch than where it was created — useful when you realise you started work on the wrong branch.

git stash push -m "WIP login"
git stash list
# stash@{0}: WIP login

git stash pop

Advanced Commands & Tips

Once you're comfortable with daily git, these commands let you advance your workflow significantly.

Interactive Rebase

Use git rebase -i HEAD~n to rewrite the last n commits — squash, reorder, or edit commit messages before opening a pull request.

git rebase -i HEAD~3

Recover with git reflog

git reflog logs every HEAD movement, including on deleted branches. Find the commit hash and restore it to recover work that looks lost.

git reflog git branch recovered <hash>

Pretty Log & Blame

Use git log with flags to visualise branching history. git blame annotates every line of a file with the commit hash and author who last changed it.

git log --oneline --graph --decorate --all git blame src/app.js

GUI Clients

If you prefer a visual interface, a git GUI like GitTower gives you a visual branch graph, one-click staging, and safe interactive rebase without the command line.

# See GitTower, GitKraken, or GitHub Desktop

Best Practices & Common Mistakes

Best Practices

  • Write clear commit messages: short summary line, optional body for context.
  • Use feature branches to isolate work from main.
  • Keep commits small and frequent — easier to review and revert.
  • Use pull requests and code reviews before merging.
  • Keep .gitignore current and never commit secrets or credentials.

Common Mistakes

  • Committed a secretRemove the file, rotate the credential, then use git filter-repo to scrub history.
  • Pushed to the wrong branchUse git reset and revert the branch, then force-push only with team agreement.
  • Merge conflict panicOpen the conflicting file, resolve the markers, git add, then git merge --continue.

Frequently Asked Questions

What's the difference between git fetch and git pull?+

git fetch downloads changes from the remote repository into your local tracking branches but does not modify your working directory or current branch. git pull runs a fetch followed immediately by a merge (or rebase if configured), integrating remote changes into your branch.

When should I use rebase vs merge?+

Use rebase on local feature branches to keep a clean, linear log before merging into main. Use merge on shared branches to preserve the full history of how and when branches diverged — important for audit trails and context.

How do I undo a commit that's already been pushed?+

Prefer git revert HEAD, which creates a new commit that undoes the previous one without rewriting history. This is safe for shared branches. Only force-push a reset if every collaborator agrees and understands the impact.

What is git reflog and when do I need it?+

git reflog is a local log of every HEAD movement — including commits on branches you've since deleted. It's your safety net for recovering seemingly lost work. Find the commit hash in the reflog output, then use git checkout or git branch to restore it.

What's the difference between git reset --soft, --mixed, and --hard?+

--soft moves HEAD back but keeps all changes staged. --mixed (the default) moves HEAD back and unstages the changes, leaving them in your working directory. --hard moves HEAD back and discards all changes permanently — use it with caution.

Key Takeaways

1

Master init, clone, add, commit, push, and pull first — these cover 80% of daily work.

2

Use branches for every feature or fix to keep main stable and deployable.

3

Learn git reset, revert, and git reflog before attempting advanced history edits.

4

Fetch before you pull to inspect what's changed on remote repositories.

5

Write meaningful commit messages — your future self will thank you.

6

When in doubt, git stash your work and check git status before proceeding.