Git Cheatsheet

Staging and Committing

Use this Git reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Staging Files (git add)

git add <file>                  # Stage a specific file
git add <dir>/                  # Stage all changes in a directory
git add .                       # Stage all changes in the current directory (recursive)
git add -A                      # Stage all changes across the entire working tree
git add *.js                    # Stage files matching a glob
git add -u                      # Stage modifications and deletions only (not new files)
git add -p                      # Interactively stage hunks (patch mode)
git add -i                      # Interactive staging menu
git add -N <file>               # Intend to add (marks untracked file for git diff)

Patch Mode (-p) Interactive Commands

KeyAction
yStage this hunk
nSkip this hunk
sSplit hunk into smaller hunks
eManually edit the hunk
qQuit, don't stage remaining
aStage this and all remaining hunks in the file
dSkip this and all remaining hunks in the file
?Print help

Unstaging Files (git restore / git reset)

git restore --staged <file>     # Unstage a file (keep changes in working tree)
git restore --staged .          # Unstage everything
git reset HEAD <file>           # Same as restore --staged (older syntax)
git reset HEAD                  # Unstage all staged changes

Discarding Working Tree Changes

git restore <file>              # Discard unstaged changes in a file (irreversible)
git restore .                   # Discard all unstaged changes
git restore --source=HEAD~2 <file>  # Restore file to state at HEAD~2
git checkout -- <file>          # Older equivalent of git restore <file>

Warning: git restore <file> permanently discards uncommitted changes.

Committing (git commit)

git commit -m "message"         # Commit staged changes with inline message
git commit                      # Open editor for commit message
git commit -a -m "message"      # Stage all tracked-file changes and commit
git commit --amend              # Amend the last commit (message or staged content)
git commit --amend --no-edit    # Amend last commit without changing the message
git commit --amend -m "new msg" # Amend last commit and change message
git commit --allow-empty -m "trigger CI"  # Commit with no changes
git commit -v                   # Show diff in editor while writing commit message
git commit --dry-run            # Show what would be committed without committing
git commit --no-verify          # Skip pre-commit and commit-msg hooks

Commit with Multiple Lines

git commit -m "subject line

More detailed explanation after a blank line.
Can span multiple paragraphs."

Commit Signing

git commit -S -m "signed commit"        # GPG-sign this commit
git commit --gpg-sign=<key> -m "msg"   # Sign with a specific key

Viewing Staged Changes (Before Commit)

git diff --staged               # Show diff of what is staged vs. last commit
git diff --cached               # Identical to --staged (older alias)
git diff --staged --stat        # Summary of staged changes
git diff --staged <file>        # Staged diff for a specific file only

Viewing Unstaged Changes

git diff                        # Diff of working tree vs. staging area
git diff <file>                 # Diff for a specific file
git diff HEAD                   # Diff of working tree (all changes) vs. HEAD
git diff --word-diff            # Show word-level diff instead of line-level
git diff --stat                 # Show summary of changed files

Removing and Moving Files

git rm <file>                   # Remove file from working tree and stage the removal
git rm --cached <file>          # Remove from index only (keep file on disk)
git rm -r <dir>/                # Recursively remove a directory
git mv <old> <new>              # Rename/move a file and stage the change

Commit Message Conventions (Conventional Commits)

<type>(<scope>): <short summary>

[optional body]

[optional footer(s)]

Common types: feat, fix, docs, style, refactor, test, chore, perf, ci, build

# Examples
git commit -m "feat(auth): add passwordless login"
git commit -m "fix(api): handle null user in billing check"
git commit -m "docs: update README with setup instructions"

Checking What Will Be Committed

git status                      # High-level view of staged/unstaged/untracked
git diff --staged --name-only   # Just the filenames that are staged
git diff --staged --name-status # Filenames with M/A/D status codes