GitGit Cheat Sheet

Git Command Cheatsheet

A dense, organised reference for everyday Git commands. Each section covers one category with the command, what it does, and the most commonly used flags.

Setup and Configuration

Command

What it does

Common flags

git config --global user.name "Name"

Set your display name for commits

--global (all repos), --local (this repo only)

git config --global user.email "e@x.com"

Set your commit email

--global

git config --global core.editor "code --wait"

Set default text editor

--global

git config --list

Show all configuration values

--global, --local, --show-origin

git config --global alias.st status

Create a command alias

--global

git config --global core.autocrlf input

Set line-ending handling

true (Win), input (Mac/Linux), false

Getting Started

Command

What it does

Common flags

git init

Create a new repository in current directory

--bare (server repo), -b main (initial branch name)

git init

Create a new repository in a new directory

git clone

Clone a remote repository locally

--depth=1 (shallow), --branch=, --single-branch

git clone

Clone into a specific directory name

Staging (Index)

Command

What it does

Common flags

git add

Stage a specific file

git add .

Stage all changes in current directory

git add -p

Interactively stage hunks

-i (interactive mode)

git add -u

Stage only modified/deleted files (not untracked)

git rm

Remove a file and stage the removal

--cached (unstage only, keep file)

git mv

Move/rename a file and stage the change

git restore --staged

Unstage a file (keep working tree changes)

Committing

Command

What it does

Common flags

git commit -m "message"

Commit staged changes with inline message

-a (stage modified tracked files), --allow-empty

git commit

Commit staged changes, open editor for message

-v (show diff in editor)

git commit --amend

Modify the most recent commit

--no-edit (keep message), -m "new message"

git commit --fixup=

Create a fixup commit for later squashing

git commit --squash=

Create a squash commit for interactive rebase

Branching

Command

What it does

Common flags

git branch

List local branches

-a (all), -r (remote), -v (verbose), --merged, --no-merged

git branch

Create a branch at current HEAD

git branch

Create a branch at a specific commit

git branch -d

Delete a merged branch

-D (force delete even if unmerged)

git branch -m

Rename a branch

git branch --set-upstream-to=origin/

Set tracking remote for current branch

git switch

Switch to an existing branch

-c (create and switch)

git checkout

Switch to a branch (legacy syntax)

-b (create and switch)

git checkout -b origin/

Create local branch tracking remote

Merging

Command

What it does

Common flags

git merge

Merge a branch into the current branch

--no-ff (always create merge commit), --squash, --ff-only

git merge --abort

Abort an in-progress merge

git merge --continue

Continue a merge after resolving conflicts

git mergetool

Open visual merge tool for conflicts

--tool=

git cherry-pick

Apply a specific commit to current branch

-n (no commit), -x (append source in message), --abort, --continue

Rebasing

Command

What it does

Common flags

git rebase

Rebase current branch onto another

-i (interactive), --onto, --autosquash

git rebase -i HEAD~N

Interactively edit last N commits

git rebase -i

Interactively rebase from a commit

--autosquash (apply fixup/squash commits)

git rebase --abort

Abort a rebase in progress

git rebase --continue

Continue after resolving conflicts

git rebase --skip

Skip the current conflicting commit

Remotes

Command

What it does

Common flags

git remote -v

List remotes with their URLs

git remote add

Add a new remote

git remote remove

Remove a remote

git remote rename

Rename a remote

git remote set-url

Change the URL of a remote

git fetch

Download remote changes without merging

--all (all remotes), --prune, --tags

git pull

Fetch and merge remote changes

--rebase, --ff-only, --no-commit

git push

Push branch to remote

-u (set upstream), --force-with-lease, --tags, --delete

git push --force-with-lease

Force push only if remote matches expected state

Undoing Changes

Command

What it does

Common flags

git restore

Discard changes in working tree

--staged (unstage), --source=

git revert

Create a new commit that undoes a commit

-n (no auto-commit), --mainline 1 (for merge commits)

git reset HEAD~1

Undo last commit, keep changes staged

git reset --soft HEAD~1

Undo last commit, keep changes staged

git reset --mixed HEAD~1

Undo last commit, keep changes unstaged (default)

git reset --hard HEAD~1

Undo last commit, DISCARD all changes

git clean -fd

Delete untracked files and directories

-n (dry run), -x (also remove .gitignored files)

Inspection and Diff

Command

What it does

Common flags

git status

Show working tree status

-s (short), -b (show branch)

git log

Show commit history

--oneline, --graph, --all, -p, --stat, -n , --since, --author

git diff

Show unstaged changes

--staged (staged changes), --stat

git diff ..

Show diff between two commits

git diff ...

Show what branch2 added since divergence

git show

Show a specific commit

--stat, --name-only

git blame

Show who last changed each line

-L , (line range), -w (ignore whitespace)

git shortlog -sn

Summary of commits per author

--all, --no-merges

git bisect start

Start binary search for a bug

good , bad , reset

git grep

Search for a pattern in tracked files

-n (line numbers), -l (filenames only), -i (case insensitive)

Stashing

Command

What it does

Common flags

git stash

Save dirty working tree to stash stack

-u (include untracked), -m "message"

git stash list

List all stash entries

git stash pop

Apply most recent stash and remove it

stash@{N} (specific stash)

git stash apply

Apply most recent stash but keep it in list

stash@{N}

git stash drop stash@{N}

Delete a specific stash entry

git stash clear

Delete all stash entries

git stash branch

Create a branch from a stash

stash@{N}

git stash show

Show a summary of changes in the stash

-p (full diff)

Tags

Command

What it does

Common flags

git tag

List all tags

-l "v2.*" (pattern match)

git tag

Create a lightweight tag at HEAD

git tag -a -m "msg"

Create an annotated tag with message

git tag -a

Tag a specific commit

git tag -d

Delete a local tag

git push origin

Push a single tag to remote

git push origin --tags

Push all tags to remote

git push origin --delete

Delete a tag from remote

git describe --tags

Describe current commit relative to nearest tag

--abbrev=7, --always

Advanced and Power User Commands

Command

What it does

Common flags

git reflog

Show history of HEAD movements (safety net)

--expire, --all

git worktree add

Create a second working tree from same repo

git submodule add

Add a submodule to the repo

--depth=1

git submodule update --init

Initialise and fetch all submodules

--recursive

git archive --format=zip HEAD > out.zip

Export repo snapshot as archive

--prefix=, --output=

git bundle create repo.bundle --all

Create a portable bundle of the entire repo

git filter-repo --path

Rewrite history to include only a path

(separate install required)

git gc

Run garbage collection / repo optimisation

--aggressive, --prune=now

git fsck

Verify integrity of the repository

--full, --unreachable

git rev-parse HEAD

Print the full SHA of a ref

--short (abbreviated SHA)

git ls-files

List tracked files

-m (modified), -o (untracked), --exclude-standard

Quick Reference: Escape Hatches

The most important recovery commands

Bash
# Recover a deleted branch
git reflog
git checkout -b recovered-branch <hash>

# Undo a mistaken git reset --hard
git reflog
git reset --hard <hash-before-reset>

# Recover a dropped stash
git fsck --unreachable | grep commit | awk '{print $3}' | xargs git log --oneline --no-walk

# Abort any in-progress operation
git merge --abort
git rebase --abort
git cherry-pick --abort

# See what git would have pushed/pulled
git fetch --dry-run
git push --dry-run