Ad 320×100
Developer

Git Cheat Sheet

Quick reference for Git commands — branching, merging, rebasing, and workflows

git init

Initialize a new Git repository in the current directory

git clone <url>

Clone a remote repository

git clone --depth 1 <url>

Shallow clone (only latest commit) for faster download

git config --global user.name "Name"

Set your name for all repositories

git config --global user.email "[email protected]"

Set your email for all repositories

git config --global core.editor vim

Set default editor for commit messages

git config --global init.defaultBranch main

Set default branch name for new repos

git config --list

List all configuration settings

git config --global alias.co checkout

Create a Git alias (co → checkout)

git config --global credential.helper cache

Cache credentials temporarily in memory

git status

Show working tree status (modified, staged, untracked)

git add <file>

Stage a specific file for commit

git add .

Stage all changes in the current directory

git add -p

Interactively stage parts of files (hunk by hunk)

git commit -m "message"

Commit staged changes with a message

git commit -am "message"

Stage all tracked files and commit in one step

git commit --amend

Modify the most recent commit (message or files)

git diff

Show unstaged changes

git diff --staged

Show staged changes (ready to commit)

git diff HEAD~3

Compare working tree with 3 commits ago

git push

Push commits to the remote branch

git pull

Fetch and merge remote changes into current branch

git pull --rebase

Fetch and rebase local commits on top of remote

git fetch

Download remote changes without merging

git branch

List all local branches

git branch -a

List all branches (local and remote)

git branch <name>

Create a new branch

git branch -d <name>

Delete a merged branch

git branch -D <name>

Force delete an unmerged branch

git branch -m <old> <new>

Rename a branch

git switch <branch>

Switch to an existing branch

git switch -c <branch>

Create and switch to a new branch

git checkout <branch>

Switch to a branch (classic syntax)

git checkout -b <branch>

Create and switch to a new branch (classic)

git merge <branch>

Merge a branch into the current branch

git merge --no-ff <branch>

Merge with a merge commit (no fast-forward)

git rebase <branch>

Rebase current branch onto another

git rebase --abort

Abort an in-progress rebase

git rebase --continue

Continue rebase after resolving conflicts

git cherry-pick <commit>

Apply a specific commit to the current branch

git remote -v

List remote repositories with URLs

git remote add origin <url>

Add a new remote repository

git remote rename origin upstream

Rename a remote

git remote remove <name>

Remove a remote

git remote set-url origin <url>

Change the URL of a remote

git push -u origin <branch>

Push branch and set upstream tracking

git push origin --delete <branch>

Delete a remote branch

git fetch --all

Fetch from all configured remotes

git fetch --prune

Fetch and remove stale remote-tracking branches

git pull origin main

Pull changes from a specific remote branch

git push --force-with-lease

Force push safely (fails if remote was updated)

git log

Show commit history

git log --oneline

Show compact one-line commit history

git log --oneline --graph --all

Show branch graph of all branches

git log -p -2

Show diffs of the last 2 commits

git log --author="Name"

Filter commits by author

git log --since="2 weeks ago"

Show commits from the last 2 weeks

git log --stat

Show commit history with file change statistics

git show <commit>

Show details and diff of a specific commit

git blame <file>

Show who last modified each line of a file

git shortlog -sn

Show commit count per author, sorted

git reflog

Show history of HEAD movements (recovery tool)

git diff <branch1>..<branch2>

Compare two branches

git log --all --grep="fix"

Search commit messages for a keyword

git restore <file>

Discard unstaged changes to a file

git restore --staged <file>

Unstage a file (keep changes in working tree)

git reset HEAD~1

Undo last commit, keep changes staged

git reset --soft HEAD~1

Undo last commit, keep changes staged

git reset --hard HEAD~1

Undo last commit and discard all changes

git reset --hard origin/main

Reset branch to match remote (destroys local changes)

git revert <commit>

Create a new commit that undoes a specific commit

git clean -fd

Remove untracked files and directories

git clean -fdn

Dry run — show what would be removed

git rm --cached <file>

Remove file from staging (keep on disk)

git checkout <commit> -- <file>

Restore a file from a specific commit

git stash

Save uncommitted changes and clean working tree

git stash push -m "description"

Stash with a descriptive message

git stash list

List all stashed changesets

git stash pop

Apply the latest stash and remove it from list

git stash apply

Apply the latest stash but keep it in list

git stash apply stash@{2}

Apply a specific stash by index

git stash drop stash@{0}

Remove a specific stash

git stash clear

Remove all stashed entries

git stash show -p

Show the diff of the latest stash

git stash branch <branch>

Create a branch from a stash

git tag

List all tags

git tag v1.0.0

Create a lightweight tag

git tag -a v1.0.0 -m "Release 1.0"

Create an annotated tag with message

git tag -a v1.0.0 <commit>

Tag a specific commit

git tag -d v1.0.0

Delete a local tag

git push origin v1.0.0

Push a specific tag to remote

git push origin --tags

Push all local tags to remote

git push origin --delete v1.0.0

Delete a remote tag

git show v1.0.0

Show information about a tag

git describe --tags

Describe current commit relative to nearest tag

git submodule add <url> path/

Add a submodule to the repository

git submodule init

Initialize submodule configuration

git submodule update

Fetch and checkout submodule commits

git submodule update --init --recursive

Initialize and update all submodules recursively

git submodule foreach git pull

Pull latest changes in all submodules

git submodule status

Show the status of all submodules

git submodule deinit path/

Unregister a submodule

git rm path/

Remove a submodule from the repository

git bisect start

Start binary search for a bug-introducing commit

git bisect good <commit>

Mark a commit as not containing the bug

git bisect bad <commit>

Mark a commit as containing the bug

git bisect reset

End bisect session and return to original HEAD

git worktree add ../feature feature-branch

Create a linked working tree for a branch

git worktree list

List all linked working trees

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

Export repository as a zip archive

git grep "pattern"

Search for a pattern across tracked files

git format-patch -3

Create patch files for the last 3 commits

git apply patch.patch

Apply a patch file

git rev-parse HEAD

Show the full SHA of the current commit

git gc

Run garbage collection to optimize the repository

Commands shown are for Git 2.x and above.

Ad 320×100

Git Cheat Sheet

A free, searchable Git cheat sheet with the most useful commands, flags, and shortcuts in one place. Quickly look up syntax, copy commands, and learn version control with Git faster — perfect for beginners and a handy reference for experienced users.

FAQ

Is this Git cheat sheet free?
Yes. The entire cheat sheet is free to browse and copy from, with no sign-up required.
Can I search for a specific command?
Yes. Use the search to filter the Git commands instantly and jump straight to the syntax you need.
Who is the Git cheat sheet for?
It suits everyone from newcomers learning version control with Git to power users who want a quick reminder of less-used flags and shortcuts.