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
| Command | What it does |
|---|---|
| git init | Create a new repository in the current folder |
| git clone <url> | Copy a remote repository |
| git status | Show changed, staged and untracked files |
| git add <file> / git add -A | Stage a file / stage all changes |
| git commit -m "message" | Commit the staged changes |
| git commit --amend | Change the last commit (message or content) |
| git log --oneline --graph | Compact history with branches |
| git diff / git diff --staged | Unstaged / staged changes |
| git branch | List 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 fetch | Download remote changes without merging |
| git pull | Fetch and merge (or rebase) the remote branch |
| git push -u origin <branch> | Push a branch and set its upstream |
| git stash / git stash pop | Set 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~1 | Undo 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.0 | Tag the current commit |
| git remote -v | List 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
- Search for what you want to do, such as "undo" or "remote".
- Copy the command and replace the parts in angle brackets.
- 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.
Related tools
- Linux Command CheatsheetCommon Linux terminal commands for file management and system administration.
- Mac Keyboard ShortcutsmacOS keyboard shortcuts for productivity and navigation.
- MIME Type LookupMIME type reference for content type headers and file handling.
- File Extension LookupFile extension database with associated programs and MIME types.
- Port Number LookupCommon network port numbers and their associated services.
- DNS Record TypesReference for DNS record types including A, AAAA, CNAME, MX, TXT, and more.
- .gitignore GeneratorIn Utilities