CHRISBMN WEBTOOLS
← All Tools
Reference

Git Cheatsheet

Quick reference for the most common Git commands, plus the gotchas that bite people.

Configuration

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
⚠️Gotcha: Repo-level config always wins over global config. If a commit shows the wrong name or email, check that repo's .git/config before blaming the global settings.
Get started

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]
⚠️Gotcha: A shallow clone has no history. git log, git blame, and most rebases will fail or behave oddly until you run git fetch --unshallow.
Stage & inspect

See what's changed

git status

Stage a file

git add [file]

Stage everything in and below the current directory

git add -A
⚠️Gotcha: 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

Commit all tracked changes

git commit -am "[commit message]"
⚠️Gotcha: -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
I’ve made a mistake

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
⚠️Gotcha: --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]
⚠️Gotcha: This (and the older 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]
⚠️Gotcha: Prefer 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
⚠️Gotcha: Almost nothing in Git is truly gone. git reflog keeps a local record of everywhere HEAD has pointed for about 90 days β€” even after a hard reset or a deleted branch.
Branches

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]
⚠️Gotcha: This deletes commits that exist only on that branch. They're recoverable via git reflog for a while, but only before garbage collection runs.

Delete a remote branch

git push origin --delete [name]
Merging & rebasing

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
⚠️Gotcha: Never rebase (or force-push) a branch other people have already pulled. Rebasing rewrites commit hashes, so everyone else's history silently diverges from yours.

Continue after resolving a rebase conflict

git rebase --continue

Abort a merge or rebase in progress

git merge --abort
git rebase --abort
Remote

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
⚠️Gotcha: Plain 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
⚠️Gotcha: --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

Stash current changes

git stash

Include untracked files in the stash

git stash -u
⚠️Gotcha: Plain 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
History & inspection

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]
Tags

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]
Cleanup & danger zone

Preview what untracked cleanup would remove (dry run)

git clean -fdn

Remove untracked files and directories

git clean -fd
⚠️Gotcha: This is permanent and unrecoverable. Always run git clean -fdn first to see exactly what would be deleted.
⚠️Gotcha: .gitignore isn't retroactive. Adding a pattern doesn't untrack files Git is already tracking. Run git rm -r --cached . followed by a fresh git add . to apply new ignore rules.
⚠️Gotcha: Detached HEAD. Checking out a commit, tag, or remote branch directly (not a local branch) leaves you in "detached HEAD" state. Commits made there belong to no branch and can be lost when you switch away β€” run git switch -c [new-branch] first if you want to keep them.
⚠️Gotcha: Line endings (CRLF vs LF). On Windows, set 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
⚠️Gotcha: This happens when merging two repos or branches with no common ancestor commit β€” often after re-initializing a repo. Only use --allow-unrelated-histories when that is actually what you intend.