Git
Git Cheat Sheet
Section titled “Git Cheat Sheet”A quick reference for everyday Git commands.
Getting Started
Section titled “Getting Started”Start a new repo:
git initClone an existing repo:
git clone <url>Prepare to Commit
Section titled “Prepare to Commit”Add an untracked file or unstaged changes:
git add <file>Add all untracked files and unstaged changes:
git add .Choose which parts of a file to stage:
git add -pMove a file:
git mv <old> <new>Delete a file:
git rm <file>Tell Git to forget about a file without deleting it:
git rm --cached <file>Unstage one file:
git reset <file># ORgit restore --staged <file>Unstage everything:
git reset# ORgit restore --staged .Check what you added:
git statusMake Commits
Section titled “Make Commits”Make a commit (and open a text editor to write the message):
git commitMake a commit:
git commit -m 'message'Commit all unstaged changes:
git commit -am 'message'Move Between Branches
Section titled “Move Between Branches”Switch branches:
git switch <name># ORgit checkout <name>Create a branch:
git switch -c <name># ORgit checkout -b <name>List branches:
git branchList branches by most recently committed to:
git branch --sort=-committerdateDelete a branch:
git branch -d <name>Force delete a branch:
git branch -D <name>Diff Staged/Unstaged Changes
Section titled “Diff Staged/Unstaged Changes”Diff all staged and unstaged changes:
git diff HEADDiff just staged changes:
git diff --stagedDiff just unstaged changes:
git diffDiff Commits
Section titled “Diff Commits”Show the diff between a commit and its parent:
git show <commit>Diff two commits:
git diff <commit> <commit>Diff one file since a commit:
git diff <commit> <file>Show a summary of a diff:
git diff <commit> --statgit show <commit> --statWays to Refer to a Commit
Section titled “Ways to Refer to a Commit”Every time we say <commit>, you can use any of these:
| Reference | Example |
|---|---|
| ★ a branch | main |
| ★ a tag | v0.1 |
| ★ a commit ID | 3e887ab |
| ★ a remote branch | origin/main |
| ★ current commit | HEAD |
| ★ 3 commits ago | HEAD^^^ or HEAD~3 |
Discard Your Changes
Section titled “Discard Your Changes”Delete unstaged changes to one file:
git restore <file># ORgit checkout <file>Delete all staged and unstaged changes to one file:
git restore --staged --worktree <file># ORgit checkout HEAD <file>Delete all staged and unstaged changes:
git reset --hard# ORgit restore --staged --worktree .Delete untracked files:
git clean‘Stash’ all staged and unstaged changes:
git stashEdit History
Section titled “Edit History”“Undo” the most recent commit (keep your working directory the same):
git reset HEAD^Squash the last 5 commits into one:
git rebase -i HEAD~5Then change pick to fixup for any commit you want to combine with the previous one.
Undo a failed rebase:
git reflog BRANCHNAMEThen manually find the right commit ID in the reflog, then run:
git reset --hard <commit>Change a commit message (or add a file you forgot):
git commit --amendCode Archaeology
Section titled “Code Archaeology”Look at a branch’s history:
git log maingit log --graph maingit log --onelineShow every commit that modified a file:
git log <file>Show every commit that modified a file, including before it was renamed:
git log --follow <file>Find every commit that added or removed some text:
git log -G bananaShow who last changed each line of a file:
git blame <file>Combine Diverged Branches
Section titled “Combine Diverged Branches”Every example below starts from the same history: main has commits A B C, and banana branched off after B with commits D E.
Combine with rebase
Section titled “Combine with rebase”git switch bananagit rebase mainBefore: D---E banana / A---B---C main
After: D'---E' banana / A---B---C---+ main \ D---E (original commits are "lost")Combine with merge
Section titled “Combine with merge”git switch maingit merge bananaBefore: D---E banana / A---B---C main
After: D---E banana / \ A---B---C---◇ mainCombine with squash merge
Section titled “Combine with squash merge”git switch maingit merge --squash bananagit commitBefore: D---E banana / A---B---C main
After: D---E banana / A---B---C---DE main (D and E squashed into one commit)Bring a branch up to date with another branch (a.k.a. “fast-forward merge”)
Section titled “Bring a branch up to date with another branch (a.k.a. “fast-forward merge”)”git switch maingit merge bananaBefore: A---B---C main \ D---E banana
After: A---B---C---D---E main, bananaCopy One Commit onto the Current Branch
Section titled “Copy One Commit onto the Current Branch”git cherry-pick <commit>Before: D---E / A---B---C main
After: D---E / A---B---C---D' mainRestore an Old File
Section titled “Restore an Old File”Get the version of a file from another commit:
git checkout <commit> <file># ORgit restore <file> --source <commit>Add a Remote
Section titled “Add a Remote”git remote add <name> <url>Push Your Changes
Section titled “Push Your Changes”Push the main branch to the remote origin:
git push origin mainPush the current branch to its remote “tracking branch”:
git pushPush a branch that you’ve never pushed before:
git push -u origin <name>Force push:
git push --force-with-leasePush tags:
git push --tagsPull Changes
Section titled “Pull Changes”Fetch changes (but don’t change any of your local branches):
git fetch origin mainFetch changes and then rebase your current branch:
git pull --rebaseFetch changes and then merge them into your current branch:
git pull origin main# ORgit pullConfigure Git
Section titled “Configure Git”Set a config option:
git config user.name 'Your Name'Set an option globally:
git config --global ...Add an alias:
git config alias.st statusSee all possible config options:
man git-configImportant Files
Section titled “Important Files”| Purpose | Location |
|---|---|
| Local git config | .git/config |
| Global git config | ~/.gitconfig |
| List of files to ignore | .gitignore |