developer reference

Git cheat sheet

A practical Git reference with search, one-click copy and printable PDF output.

Go to commands
42
commands
9
categories
0
dependencies

42 commands visible

Init and clone

3 commands
git init

Initializes a repository in the current directory

Init and clonesafe
Practical example
git init  # or with a name
git init myproject
git clone <url>

Clones a remote repository locally

Init and clonesafe
Practical example
git clone https://github.com/user/repo.git
# into a specific directory
git clone <url> myfolder
# latest commit only
git clone --depth 1 <url>

Staging and commit

5 commands
git status

Shows modified, staged and untracked files

Staging and commitsafe
Practical example
git status
git status -s  # compact output
git add <file>

Adds files to the staging area

Staging and commitsafe
Practical example
git add index.html
git add .  # all changes
git add -p  # interactive, hunk by hunk
git commit -m "<msg>"

Commits staged changes with a message

Staging and commitsafe
Practical example
git commit -m "feat: add login page"
git commit -am "fix: typo"  # stage and commit tracked changes
git commit --amend

Edits the last commit message or content

Staging and commitcaution

Replaces the last commit. Coordinate before amending a published commit.

Practical example
git commit --amend -m "new message"
# add forgotten files
git add file.txt && git commit --amend --no-edit
git rm --cached <file>

Removes a file from Git’s index; the local file remains. Commit to stop tracking it.

Staging and commitcaution

The next commit removes this file from the repository; the local copy remains.

Practical example
git rm --cached secret.env

Stash

6 commands
git stash

Temporarily shelves uncommitted changes

Stashsafe
Practical example
git stash
git stash push -m "wip: login form"
git stash list

Lists all stashes

Stashsafe
Practical example
git stash list
git stash pop

Applies a stash and removes it only if application succeeds

Stashcaution

Check conflicts before continuing. Git retains the stash when application fails.

Practical example
git stash pop
git stash pop stash@{2}
git stash apply [stash@{n}]

Applies a stash without removing it

Stashsafe
Practical example
git stash apply stash@{1}
git stash drop [stash@{n}]

Deletes a specific stash

Stashcaution

Deletes the selected stash. Recovery is not guaranteed.

Practical example
git stash drop stash@{0}
git stash branch <branch>

Creates a branch from a stash and applies it

Stashsafe
Practical example
git stash branch feature/login stash@{0}

Branch and merge

7 commands
git branch

Lists local branches (-a includes remotes)

Branch and mergesafe
Practical example
git branch
git branch -a
git switch -c <name>

Creates and switches to a new branch

Branch and mergesafe
Practical example
git switch -c feature/auth
# legacy syntax
git checkout -b feature/auth
git switch <branch>

Switches to an existing branch

Branch and mergesafe
Practical example
git switch main
git switch -  # previous branch
git merge <branch>

Merges a branch into the current branch

Branch and mergesafe
Practical example
git switch main
git merge feature/auth
git merge --no-ff feature/auth  # preserve merge commits
git rebase <branch>

Rewrites branch history onto a new base

Branch and mergecaution

Rewrites commit history. Coordinate before rebasing a shared branch.

Practical example
git switch feature/auth
git rebase main
git rebase -i HEAD~3  # interactive: squash or reorder
git cherry-pick <hash>

Applies a specific commit to the current branch

Branch and mergesafe
Practical example
git cherry-pick a3f1c2e
git cherry-pick a3f1c2e^..b7d9e0f  # commit range
git branch -d <branch>

Deletes a branch only if already merged

Branch and mergesafe
Practical example
git branch -d feature/auth
git branch -D feature/auth  # force deletion

Remote

5 commands
git push --force-with-lease

Safer force push that fails if remote has new commits

Remotecaution

Rewrites remote history. Verify the branch and expected remote state before pushing.

Practical example
git push --force-with-lease

History and log

4 commands
git log --oneline --graph

Compact history with branch graph

History and logsafe
Practical example
git log --oneline --graph --decorate
git log -5 --oneline  # latest 5 commits
git log -S "<string>"

Finds commits that add or remove a string

History and logsafe
Practical example
git log -S "apiKey" --oneline
git blame <file>

Shows who last changed each line of a file

History and logsafe
Practical example
git blame src/main.py
git blame -L 10,20 src/main.py

Safe undo

5 commands
git restore <file>

Discards local unstaged changes

Safe undocaution

Discards unstaged edits to the selected file. Git may not be able to recover them.

Practical example
git restore index.html
git restore .  # all files
git reset HEAD~1

Undoes the last commit while keeping changes

Safe undocaution

The default mixed reset keeps working files but clears staging. --hard also discards tracked edits.

Practical example
git reset --soft HEAD~1  # keep changes staged
git reset HEAD~1  # unstage, keep files
git reset --hard HEAD~1  # DESTROYS local changes
git clean -fd

Deletes untracked files and directories, irreversible

Safe undocaution

Permanently deletes untracked files and directories. Preview with git clean -nd first.

Practical example
git clean -fdn  # preview
git clean -fd

Diff

3 commands
git diff

Changes not staged yet

Diffsafe
Practical example
git diff
git diff src/main.py
git diff --staged

Staged changes ready to commit

Diffsafe
Practical example
git diff --staged
git diff <branch1> <branch2>

Compares two branches

Diffsafe
Practical example
git diff main feature/auth
git diff --name-only main feature/auth

Config

4 commands
git config --global user.name "<name>"

Sets the global user name

Configsafe
Practical example
git config --global user.name "Enzo"
git config --global user.email "<email>"

Sets the global email

Configsafe
Practical example
git config --global user.email "enzo@example.com"
git config --global alias.<name> "<cmd>"

Creates an alias for a command

Configsafe
Practical example
# git lg = graphical log
git config --global alias.lg "log --oneline --graph --decorate"
git config --list

Shows the active configuration

Configsafe
Practical example
git config --list --show-origin

How to use this Git cheat sheet

How do I unstage a file without losing edits?

Use git restore --staged <file>. To stop tracking a file while keeping the local copy, use git rm --cached <file> and commit the removal.

Can I print this sheet as a PDF?

Use Print PDF. The printable version includes all commands and practical examples, including sections hidden by a search.

Can I use it locally?

Yes. Serve the static files with a local HTTP server. Search, filters and copying run in your browser; the Regex tester requires a worker-enabled HTTP context.