Git Cheat Sheet
Free Git cheat sheet: the most-used Git syntax and methods at a glance — searchable and beginner-friendly.
Getting Started
| Concept | Syntax | Example |
|---|---|---|
| Initialize a repo Turns the current folder into a Git repository. | git init | git init |
| Clone a repo Downloads a repository and its full history. | git clone <url> | git clone https://github.com/user/repo.git |
| Check status Shows changed files, staged files, and your current branch. | git status | git status |
| View history Compact commit history; add -10 for the last ten. | git log --oneline | git log --oneline -10 |
| Configure identity Set once per machine — it's stamped on your commits. | git config --global user.name / user.email | git config --global user.name "Ada" |
Everyday Workflow
| Concept | Syntax | Example |
|---|---|---|
| Stage changes Marks changes to include in the next commit. `.` stages everything. | git add <file> | git add . | git add src/app.js |
| Commit Saves a snapshot of staged changes with a message. | git commit -m "message" | git commit -m "Fix login bug" |
| Push Uploads your commits to the remote repository. | git push origin <branch> | git push origin main |
| Pull Fetches remote commits and merges them into your branch. | git pull origin <branch> | git pull origin main |
| See what changed Line-by-line changes. Plain `git diff` = unstaged changes. | git diff | git diff HEAD~1 |
Branches
| Concept | Syntax | Example |
|---|---|---|
| Create + switch Makes a new branch and moves to it (modern form of checkout -b). | git switch -c <name> | git switch -c feature/login |
| Switch branch Move between branches. | git switch <name> | git switch main |
| List branches -a includes remote branches. | git branch | git branch -a |
| Merge Brings another branch's commits into the current one. | git merge <branch> | git merge feature/login |
| Delete branch -d is safe (refuses unmerged); -D forces. | git branch -d <name> | git branch -d feature/login |
Undo & Rescue
| Concept | Syntax | Example |
|---|---|---|
| Unstage a file Takes a file back out of the staging area (keeps your edits). | git restore --staged <file> | git restore --staged app.js |
| Discard local edits ⚠️ Permanently throws away uncommitted changes to that file. | git restore <file> | git restore app.js |
| Amend last commit Fix the last commit's message or add forgotten files (before pushing). | git commit --amend | git commit --amend -m "Better message" |
| Revert a commit Safely undoes a commit by creating a new opposite commit. | git revert <hash> | git revert a1b2c3d |
| Stash work Shelve uncommitted changes, switch branches, then pop them back. | git stash / git stash pop | git stash |
Setup & Config
| Concept | Syntax | Example |
|---|---|---|
| List config Show all current settings (global, system, and local combined). | git config --list | git config --list |
| Set default branch New repos start on `main` instead of `master`. | git config --global init.defaultBranch main | git config --global init.defaultBranch main |
| Set default editor Choose which editor Git opens for commit messages and rebases. | git config --global core.editor <ed> | git config --global core.editor "code --wait" |
| Create an alias Make shortcuts, e.g. `git co` runs `git checkout`. | git config --global alias.<name> <cmd> | git config --global alias.co checkout |
| Cache credentials Avoid retyping your password/token on every push. | git config --global credential.helper | git config --global credential.helper cache |
Staging & Committing
| Concept | Syntax | Example |
|---|---|---|
| Stage everything Stage all changes including deletions across the whole repo. | git add -A | git add -A |
| Interactive / patch add Review and stage changes hunk by hunk for clean commits. | git add -p | git add -p |
| Unstage Remove a file from the staging area but keep your edits. | git restore --staged <file> | git restore --staged app.js |
| Commit all tracked Stage all tracked (modified) files and commit in one step. | git commit -am "msg" | git commit -am "Update README" |
| Empty / WIP commit Record a commit with no changes — handy to kick off pipelines. | git commit --allow-empty | git commit --allow-empty -m "trigger CI" |
Inspecting
| Concept | Syntax | Example |
|---|---|---|
| Graph log Visualize branches and merges as an ASCII commit graph. | git log --oneline --graph --all | git log --oneline --graph --all |
| Staged diff See exactly what will be included in your next commit. | git diff --staged | git diff --staged |
| Show a commit Display a commit's message and full diff. | git show <hash> | git show a1b2c3d |
| Who changed a line Show the commit and author behind each line of a file. | git blame <file> | git blame -L 10,20 app.js |
| Search history Find commits that added or removed a specific string of code. | git log -S"text" | git log -S"loginUser" |
| File history List only the commits that touched a particular file. | git log -- <file> | git log --oneline -- src/app.js |
Undoing
| Concept | Syntax | Example |
|---|---|---|
| Soft reset Undo the last commit but keep its changes staged. | git reset --soft HEAD~1 | git reset --soft HEAD~1 |
| Mixed reset Undo the last commit; changes stay in your working tree, unstaged. | git reset HEAD~1 | git reset HEAD~1 |
| Hard reset ⚠️ Discard the last commit AND its changes permanently. | git reset --hard HEAD~1 | git reset --hard HEAD~1 |
| Restore from commit Bring back an old version of a single file. | git restore --source=<hash> <file> | git restore --source=HEAD~2 app.js |
| Recover lost commits Lists where HEAD has been — your safety net for recovering resets. | git reflog | git reflog |
| Clean untracked ⚠️ Delete untracked files and folders. Use -n first to preview. | git clean -fd | git clean -fd |
Remotes
| Concept | Syntax | Example |
|---|---|---|
| Add a remote Link your local repo to a remote, conventionally named `origin`. | git remote add <name> <url> | git remote add origin https://github.com/u/repo.git |
| List remotes Show remote names and their fetch/push URLs. | git remote -v | git remote -v |
| Fetch Download remote changes without merging them into your branch. | git fetch <remote> | git fetch origin |
| Push & set upstream Push and remember the upstream so future `git push` needs no args. | git push -u <remote> <branch> | git push -u origin feature/login |
| Delete remote branch Remove a branch from the remote repository. | git push <remote> --delete <branch> | git push origin --delete old-feature |
| Prune stale branches Clean up local refs to remote branches that no longer exist. | git fetch --prune | git fetch --prune |
Rebase & Cherry-pick
| Concept | Syntax | Example |
|---|---|---|
| Rebase onto branch Replay your commits on top of another branch for a linear history. | git rebase <branch> | git rebase main |
| Interactive rebase Reword, squash, reorder, or drop the last n commits. | git rebase -i HEAD~n | git rebase -i HEAD~3 |
| Continue rebase Resume a rebase after you've resolved a conflict. | git rebase --continue | git rebase --continue |
| Abort rebase Bail out and return to the state before the rebase started. | git rebase --abort | git rebase --abort |
| Cherry-pick Copy a single commit from another branch onto the current one. | git cherry-pick <hash> | git cherry-pick a1b2c3d |
Stashing
| Concept | Syntax | Example |
|---|---|---|
| Stash with message Shelve changes with a label so you remember what they were. | git stash push -m "msg" | git stash push -m "wip navbar" |
| List stashes Show all stashed change sets, newest first. | git stash list | git stash list |
| Apply (keep stash) Reapply a stash but leave it on the stash stack. | git stash apply | git stash apply stash@{1} |
| Pop (apply + remove) Reapply the most recent stash and delete it from the stack. | git stash pop | git stash pop |
| Drop / clear Delete one stash, or wipe them all. | git stash drop / git stash clear | git stash clear |
Tags & Releases
| Concept | Syntax | Example |
|---|---|---|
| Annotated tag Create a tagged, documented point in history — used for releases. | git tag -a <name> -m "msg" | git tag -a v1.0.0 -m "First release" |
| Lightweight tag A simple named pointer to a commit, no extra metadata. | git tag <name> | git tag v1.0.1 |
| List tags Show all tags, optionally filtered by a pattern. | git tag | git tag -l "v1.*" |
| Push tags Upload your tags — they aren't pushed by default. | git push <remote> --tags | git push origin --tags |
| Delete a tag Remove a tag locally (add a remote push to delete it there). | git tag -d <name> | git tag -d v1.0.1 |
.gitignore & Misc
| Concept | Syntax | Example |
|---|---|---|
| Ignore files Tell Git which files to never track (deps, secrets, build output). | add patterns to .gitignore | node_modules/
*.log
.env |
| Untrack a tracked file Stop tracking a file without deleting it from disk. | git rm --cached <file> | git rm --cached .env |
| Check if ignored Find out which .gitignore rule is excluding a file. | git check-ignore <file> | git check-ignore -v build/ |
| Rename / move Rename or move a tracked file and stage the change at once. | git mv <old> <new> | git mv old.js new.js |
| Add a submodule Embed another Git repo inside yours at a fixed commit. | git submodule add <url> | git submodule add https://github.com/u/lib.git |
| Init submodules Fetch and check out all submodules after cloning. | git submodule update --init --recursive | git submodule update --init --recursive |