Git Command Cheatsheet

Essential Git commands for version control workflows.

The Git commands you'll use most, each with a one-line explanation: starting and cloning repositories, staging and committing, viewing history and differences, creating and switching branches, merging and rebasing, working with remotes, stashing work and undoing mistakes. It uses the newer git switch and git restore commands (added in Git 2.23 in 2019) alongside the older git checkout, which still works. Search the table for a word such as "undo" or "branch". Pair it with the .gitignore Generator when starting a new project.

Git commands you'll use most

CommandWhat it does
git initCreate a new repository in the current folder
git clone <url>Copy a remote repository
git statusShow changed, staged and untracked files
git add <file> / git add -AStage a file / stage all changes
git commit -m "message"Commit the staged changes
git commit --amendChange the last commit (message or content)
git log --oneline --graphCompact history with branches
git diff / git diff --stagedUnstaged / staged changes
git branchList branches
git switch -c <branch>Create and switch to a branch
git switch <branch>Switch branches
git merge <branch>Merge a branch into the current one
git rebase <branch>Replay your commits on top of another branch
git fetchDownload remote changes without merging
git pullFetch and merge (or rebase) the remote branch
git push -u origin <branch>Push a branch and set its upstream
git stash / git stash popSet changes aside / bring them back
git restore <file>Discard unstaged changes to a file
git restore --staged <file>Unstage a file
git reset --soft HEAD~1Undo the last commit, keep its changes staged
git revert <commit>Make a new commit that undoes one
git cherry-pick <commit>Apply one commit from elsewhere
git tag v1.0Tag the current commit
git remote -vList remotes
git blame <file>Who last changed each line

Common use cases

  • Look up a command you don't use every day
  • Learn Git basics as a beginner
  • Find the safe way to undo a change
  • Keep a quick reference open while you work

How to use

  1. Search for what you want to do, such as "undo" or "remote".
  2. Copy the command and replace the parts in angle brackets.
  3. Run git status before and after to see what changed.

FAQ

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

git fetch downloads new commits from the remote without changing your files. git pull fetches and then merges (or rebases) them into your current branch in one step.

How do I undo my last commit?

If you haven't pushed it, git reset --soft HEAD~1 removes the commit but keeps the changes staged. If it's already pushed, use git revert, which adds a new commit that reverses it without rewriting shared history.

Should I merge or rebase?

Merging keeps history exactly as it happened, with a merge commit. Rebasing replays your commits on top of the other branch for a straight-line history. Don't rebase commits other people have already pulled.

What's the difference between git switch and git checkout?

git checkout did two jobs: switching branches and restoring files. Git 2.23 split them into git switch and git restore, which are clearer. checkout still works.