All cheatsheets

Git Commands Cheat Sheet

Quick reference for the most useful Git commands: init, clone, branch, merge, rebase, reset, stash, remote operations and history inspection, with common options and practical examples.

31 commands

Getting Started

git init

Create a new Git repository in the current directory.

git init my-project
git clone <url>

Copy a remote repository to your local machine, including full history.

--depth 1 shallow clone (latest commit only); -b <branch> clone a specific branch

git clone -b main --depth 1 https://github.com/user/repo.git
git config

Set user name, email and other options; --global applies to all repositories.

git config --global user.name "Your Name" && git config --global user.email "[email protected]"

Everyday Work

git status

Show working tree state: modified, staged and untracked files.

-s short format; -b show branch info

git status -sb
git add <file>

Stage file changes for the next commit.

-A stage all changes; -p interactively pick hunks; -u stage tracked files only

git add -p src/index.ts
git commit

Record staged changes with a message.

-m message inline; -a stage tracked files and commit; --amend rewrite the last commit

git commit -am "fix: handle empty input"
git diff

Show line-level changes between working tree, index and commits.

--staged staged vs last commit; <branch> compare with a branch; --stat summary only

git diff --staged --stat
git restore <file>

Discard uncommitted changes in the working tree (or unstage with --staged).

git restore --staged src/index.ts

Branching

git branch

List, create or delete branches.

-d delete; -D force delete; -m rename; -a list remote branches too

git branch -m old-name new-name
git switch <branch>

Switch to another branch; -c creates it first (modern replacement for checkout).

git switch -c feature/login
git merge <branch>

Merge another branch into the current one.

--no-ff always create a merge commit; --abort cancel a conflicted merge

git merge --no-ff feature/login
git rebase <branch>

Replay your commits on top of another branch for a linear history.

-i interactive (squash/reword/reorder); --abort cancel; --continue after resolving

git rebase -i HEAD~3

Remote

git remote -v

List configured remote repositories with URLs.

git remote add origin https://github.com/user/repo.git
git fetch

Download objects and refs from the remote without merging.

--prune delete stale remote-tracking branches; --all fetch all remotes

git fetch --prune origin
git pull

Fetch from the remote and merge (or rebase) into the current branch.

--rebase rebase instead of merge; --ff-only fast-forward only

git pull --rebase origin main
git push

Upload local commits to the remote repository.

-u set upstream tracking; --force-with-lease safer force push; --tags push tags

git push -u origin feature/login

Undo & Fix

git reset

Move HEAD to another commit, optionally changing index and working tree.

--soft keep changes staged; --mixed (default) keep changes unstaged; --hard discard everything

git reset --hard HEAD~1
git revert <commit>

Create a new commit that undoes a previous commit. Safe for shared history.

git revert HEAD
git commit --amend

Rewrite the most recent commit: fix its message or add forgotten changes.

--no-edit keep the original message

git commit --amend --no-edit
git clean

Remove untracked files from the working tree.

-n dry run; -f force; -d include directories; -x include ignored files

git clean -ndx

History & Inspect

git log

Show commit history.

--oneline one line per commit; --graph branch graph; -p show diffs; -n limit count

git log --oneline --graph -15
git show <commit>

Show a commit's message and diff; also works for tags and files (git show HEAD:file).

git show HEAD~2 --stat
git blame <file>

Show who last modified each line of a file and in which commit.

-L n,m restrict to a line range

git blame -L 10,20 src/index.ts
git reflog

Show the history of HEAD movements — recover lost commits after a bad reset or rebase.

git reflog -20

Stash

git stash

Temporarily shelve uncommitted changes and clean the working tree.

-u include untracked files; push -m message; list show all stashes

git stash push -u -m "wip: login form"
git stash pop

Re-apply the most recent stash and remove it from the stash list.

apply re-apply but keep the stash entry

git stash apply stash@{1}

Tags & Release

git tag

List, create or delete tags marking release points.

-a annotated tag; -m message; -d delete

git tag -a v1.2.0 -m "Release 1.2.0"
git describe --tags

Show the most recent tag reachable from the current commit, plus distance.

git describe --tags --always

Advanced

git cherry-pick <commit>

Apply the changes of a specific commit onto the current branch.

-n apply without committing; -x append source commit hash to message

git cherry-pick -x a1b2c3d
git bisect

Binary-search the history to find the commit that introduced a bug.

start / bad / good / run <cmd> / reset

git bisect start && git bisect bad && git bisect good v1.0.0
git worktree add <path> <branch>

Check out another branch into a separate directory sharing the same repository.

git worktree add ../hotfix hotfix/urgent-bug

Cheatsheet version 1.0.0

Covers Git 2.30+