Skip to content

Git Cheat Sheet

Git tracks project history as snapshots linked in a DAG; daily work flows through status, stage, commit, and sync with remotes via push and pull.

How to use this Git cheat sheet

The working directory, staging area (index), and repository (commits) are the three trees. Branching creates lightweight pointers; merge combines histories while rebase replays commits for a linear timeline.

Undo safely with restore for working tree, reset for moving HEAD, and revert for public history. Stash shelves WIP; bisect binary-searches regressions; remotes track shared copies on GitHub or GitLab.

Quick Git example

# daily workflow
git status
git add -p                  # stage hunks interactively
git commit -m "feat: add login form"
git pull --rebase origin main
git push origin feature/login

# undo local edits to one file
git restore src/app.js

# undo last commit, keep changes staged
git reset --soft HEAD~1

Daily Commands

Command Example Purpose
status git status -sb Short branch status with tracking info
add git add -p Stage changes interactively by hunk
commit git commit -m "fix: typo" Snapshot staged changes locally
diff git diff --staged Review what will be committed
log git log --oneline -10 Compact recent history
show git show HEAD Patch and metadata for a commit
switch git switch -c feature/x Create and switch branch (modern)
pull git pull --rebase Fetch then replay local commits on top

Branching & Merging

Command Example Notes
branch list git branch -a Local and remote-tracking branches
merge git merge feature/x Creates merge commit if histories diverged
fast-forward git merge --ff-only main Refuse merge if non-FF required
rebase git rebase main Replay commits onto updated base
interactive rebase git rebase -i HEAD~3 Squash, reword, drop commits
cherry-pick git cherry-pick abc1234 Apply single commit to current branch
delete branch git branch -d feature/x Delete merged local branch
remote branch git push -u origin feature/x Publish and set upstream

Undo & Restore

Command Example Effect
restore file git restore file.js Discard unstaged working tree changes
restore staged git restore --staged file.js Unstage without losing edits
reset soft git reset --soft HEAD~1 Move HEAD; keep staging and working tree
reset mixed git reset HEAD~1 Move HEAD; unstage; keep working tree
reset hard git reset --hard HEAD~1 Move HEAD; discard all local changes (destructive)
revert git revert HEAD New commit that undoes a prior commit
reflog git reflog Recover "lost" commits after reset
clean git clean -fd Remove untracked files and directories

Remotes, Stash & Bisect

Command Example Purpose
clone git clone git@host:repo.git Copy remote repository locally
fetch git fetch origin Download commits without merging
push git push origin main Upload local commits to remote
remote -v git remote -v List remote URLs
stash push git stash push -m "wip" Shelve uncommitted work
stash pop git stash pop Restore and remove latest stash
bisect start git bisect start; git bisect bad; git bisect good v1.0 Binary search for regression commit
tag git tag -a v1.0 -m "Release" Immutable release pointer

Resolve merge conflict markers

<<<<<<< HEAD
const port = 3000;
=======
const port = 8080;
>>>>>>> feature/config
# Edit to final value, remove markers, then:
git add config.js && git commit

Conflict markers show both sides; only the resolved content should remain.

Rebase feature onto latest main

git fetch origin
git switch feature/my-work
git rebase origin/main
# fix conflicts, then:
git add .
git rebase --continue
git push --force-with-lease

Use --force-with-lease instead of --force when updating rebased remote branches.

Inspect who changed a line

git blame -L 40,60 src/utils.js
git log -p -S "functionName" -- src/utils.js

blame shows line history; log -S finds commits that added/removed a string.

Common mistakes

  • Running git reset --hard on shared branches instead of git revert.
  • Rebasing commits others already pulled, causing duplicate history and conflicts.
  • Committing secrets or build artifacts because .gitignore was incomplete.
  • Using merge when a fast-forward or squash merge would keep history cleaner.

Key takeaways

  • Three trees: working directory, staging area, commits—commands move changes between them.
  • Rebase for private feature branches; merge or revert for shared public history.
  • Stash and bisect solve interruptions and regression hunts efficiently.
  • Always pull --rebase before push to reduce unnecessary merge commits.

Pro Tip

Configure git config pull.rebase true globally so git pull replays your commits instead of creating merge commits on every sync.