Git Cheatsheet
Quick reference for the most common Git commands, plus the gotchas that bite people.
Set the global config
git config --global user.name "[name]" git config --global user.email "[email]"
Set the default editor
git config --global core.editor "code --wait"
Show effective config and where each value comes from
git config --list --show-origin
.git/config before blaming the global settings.Create a git repository
git init
Clone an existing git repository
git clone [url]
Clone a single branch only
git clone --branch [branch] --single-branch [url]
Shallow clone β skip history for a faster checkout
git clone --depth 1 [url]
git log, git blame, and most rebases will fail or behave oddly until you run git fetch --unshallow.See what's changed
git status
Stage a file
git add [file]
Stage everything in and below the current directory
git add -A
git add . only stages the current directory and below β run it from the repo root, or files elsewhere get silently skipped.See unstaged changes
git diff
See staged changes
git diff --staged
Commit all tracked changes
git commit -am "[commit message]"
-am skips new, untracked files entirely β git add them first or they won't be in the commit.Add new modifications to the last commit
git commit --amend --no-edit
Change the last commit message
git commit --amend
Undo the most recent commit and keep the changes
git reset HEAD~1
Undo the most recent commit and discard the changes
git reset --hard HEAD~1
--hard throws away working-directory changes permanently. git reflog can't bring back uncommitted work β only commits.Unstage a file without losing its changes
git restore --staged [file]
Discard uncommitted changes to a file
git restore [file]
git checkout -- [file]) permanently discards local edits. There is no trash bin for uncommitted changes.Undo a commit that's already been pushed or shared
git revert [commit]
revert over reset/amend once a commit is shared. Revert adds a new commit instead of rewriting history, so it can't break anyone else's clone.Recover a commit you think you lost
git reflog
git reflog keeps a local record of everywhere HEAD has pointed for about 90 days β even after a hard reset or a deleted branch.List branches
git branch
Create a branch
git branch [name]
Create and switch to a new branch
git switch -c [name]
Switch branches
git switch [name]
Rename the current branch
git branch -m [new-name]
Delete a branch that's been merged
git branch -d [name]
Force-delete a branch, merged or not
git branch -D [name]
git reflog for a while, but only before garbage collection runs.Delete a remote branch
git push origin --delete [name]
Merge a branch into the current one
git merge [branch]
Rebase the current branch onto another
git rebase [branch]
Interactively rewrite the last 3 commits
git rebase -i HEAD~3
Continue after resolving a rebase conflict
git rebase --continue
Abort a merge or rebase in progress
git merge --abort git rebase --abort
List remotes
git remote -v
Add a remote
git remote add origin [url]
Fetch without merging
git fetch
Pull (fetch + merge)
git pull
Pull with rebase instead of merge
git pull --rebase
git pull merges by default, adding an extra merge commit whenever your branch has diverged. --rebase keeps history linear, but rewrites local commits β don't use it on a branch others are also pushing to.Push and set the upstream branch
git push -u origin [branch]
Force-push safely
git push --force-with-lease
--force-with-lease fails if the remote has commits you haven't fetched yet β it protects you from silently overwriting a teammate's work the way plain --force would.Stash current changes
git stash
Include untracked files in the stash
git stash -u
git stash skips new, untracked files β they'll still be sitting in your working directory afterward, which looks like the stash "didn't work."List stashes
git stash list
Reapply the most recent stash and remove it
git stash pop
Reapply a stash without removing it
git stash apply
Delete a stash
git stash drop
View commit history
git log
Compact, graphical history of every branch
git log --oneline --graph --all
Show a specific commit
git show [commit]
See who last changed each line of a file
git blame [file]
Compare two commits
git diff [commit1] [commit2]
List tags
git tag
Create an annotated tag
git tag -a v1.0 -m "[message]"
Push tags to the remote
git push --tags
Delete a tag
git tag -d [tag]
Preview what untracked cleanup would remove (dry run)
git clean -fdn
Remove untracked files and directories
git clean -fd
git clean -fdn first to see exactly what would be deleted.git rm -r --cached . followed by a fresh git add . to apply new ignore rules.git switch -c [new-branch] first if you want to keep them.git config --global core.autocrlf true; on macOS/Linux, set it to input. Mismatched settings across a team show up as "every line changed" diffs.Fix "fatal: refusing to merge unrelated histories"
git merge [branch] --allow-unrelated-histories
--allow-unrelated-histories when that is actually what you intend.