Git Cheat Sheet — Commands You Can Copy

Every Git command you use day to day — and the ones you need when something goes wrong — explained in one line. Type your branch, remote and file names once and copy ready-to-run commands.

Runs in your browserUpdated
Your valuesEvery example below updates as you type

Setup & config

7
git config --global user.name "Ada Lovelace"

Set the author name recorded on your commits.

git config --global user.email "ada@example.com"

Set the email recorded on your commits — use the one linked to your GitHub/GitLab account.

git config --global init.defaultBranch main

Name the first branch of every new repository.

git config --global pull.rebase true

Make git pull rebase your local commits instead of creating merge commits.

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

Use VS Code for commit messages and interactive rebases.

git config --global alias.lg "log --oneline --graph --decorate --all"

Add a git lg shortcut for a compact history graph.

git config --list --show-origin

Show every setting and the file it comes from — handy when a setting “doesn’t work”.

Start a repository

6
git init

Turn the current folder into a Git repository.

git clone git@github.com:acme/app.git

Download a repository with its full history.

git clone --depth 1 git@github.com:acme/app.git

Shallow clone with only the latest commit — much faster for large repositories and CI.

git remote add origin git@github.com:acme/app.git

Connect a local repository to a remote.

git remote -v

List remotes and their fetch/push URLs.

git remote set-url origin git@github.com:acme/app.git

Change the URL of a remote, e.g. from HTTPS to SSH.

Stage & commit

12
git status -sb

Short status with the branch name and how far you are ahead of or behind the remote.

git add src/app.js

Stage one file for the next commit.

git add -p

Review and stage changes hunk by hunk — the best way to make focused commits.

git add -A

Stage everything: new, modified and deleted files.

git commit -m "Add login form"

Commit the staged changes with a message.

git commit --amend --no-edit

Add the staged changes to the last commit and keep its message. Avoid on commits you already pushed.

git commit --amend -m "Better message"

Rewrite the message of the last commit.

git diff

Show changes that are not staged yet.

git diff --staged

Show exactly what the next commit will contain.

git restore --staged src/app.js

Unstage a file but keep your edits.

git restore src/app.js

destructive Throw away uncommitted edits to a file and return it to the last commit.

git rm --cached src/app.js

Stop tracking a file (for example after adding it to .gitignore) while keeping it on disk.

Branches

9
git branch -a

List local and remote branches.

git switch -c feature/login

Create a new branch and switch to it.

git switch feature/login

Switch to an existing branch.

git switch -

Jump back to the branch you were on before.

git branch -m feature/login

Rename the current branch.

git branch --merged main

List branches already merged into the main branch — safe to delete.

git branch -d feature/login

Delete a local branch that has been merged.

git branch -D feature/login

destructive Force-delete a local branch even if its commits are not merged anywhere.

git push origin --delete feature/login

destructive Delete the branch on the remote.

Sync with the remote

6
git fetch --all --prune

Download new commits and forget remote branches that were deleted.

git pull

Fetch and integrate the upstream branch into the current one.

git pull --rebase origin main

Replay your local commits on top of the latest main.

git push -u origin feature/login

Push a new branch and set it as upstream, so later git push needs no arguments.

git push --force-with-lease

destructive Force-push after a rebase, but refuse if someone else pushed in the meantime. Much safer than --force.

git push origin --tags

Push all local tags.

Merge & rebase

10
git merge feature/login

Merge another branch into the current one.

git merge --no-ff feature/login

Always create a merge commit so the branch stays visible in history.

git merge --squash feature/login

Stage all changes of a branch as one change, ready for a single commit.

git rebase main

Move your branch’s commits on top of the latest main for a linear history.

git rebase -i HEAD~3

destructive Interactively squash, reorder, reword or drop the last N commits. Rewrites history.

git rebase --continue

Continue a rebase after resolving conflicts and staging the files.

git rebase --abort

Cancel a rebase and return to where you started.

git merge --abort

Cancel a merge that has conflicts.

git checkout --theirs src/app.js

During a conflict, take the incoming version of a file (--ours keeps yours).

git cherry-pick a1b2c3d

Apply a single commit from another branch onto the current one.

Undo & fix mistakes

9
git reset --soft HEAD~1

Undo the last commit but keep its changes staged.

git reset HEAD~1

Undo the last commit and keep its changes as unstaged edits.

git revert a1b2c3d

Create a new commit that undoes an earlier one — the safe way to undo on shared branches.

git reset --hard HEAD~1

destructive Delete the last commit and all its changes.

git reset --hard origin/main

destructive Make your local branch exactly match the remote, discarding local commits and edits.

git reflog

List every commit HEAD pointed to recently — how you find “lost” commits after a bad reset or rebase.

git reset --hard HEAD@{1}

destructive Go back to where HEAD was before the last reset, rebase or checkout.

git clean -nd

Preview which untracked files and folders git clean would delete.

git clean -fd

destructive Delete untracked files and folders. Run the -nd preview first.

Stash

6
git stash push -m "wip: feature/login"

Put uncommitted changes aside with a label.

git stash -u

Stash including untracked files.

git stash list

List all stashes.

git stash pop

Re-apply the latest stash and remove it from the list.

git stash apply stash@{0}

Re-apply a stash but keep it in the list.

git stash drop stash@{0}

destructive Delete a stash permanently.

History & inspection

9
git log --oneline --graph --all

Compact graph of all branches.

git log -p -- src/app.js

History of one file with the diff of every change.

git log --author="Ada Lovelace" --since="2 weeks ago"

Commits by one author in a time window.

git log -S "functionName"

Find commits that added or removed a string — great for “when did this disappear?”.

git blame -w src/app.js

Show who last changed each line, ignoring whitespace-only changes.

git show a1b2c3d

Show a commit’s message and diff.

git diff main...feature/login

Changes made on a branch since it split from main — what a pull request would show.

git shortlog -sn

Number of commits per author.

git bisect start
git bisect bad
git bisect good v1.2.0

Binary-search history for the commit that introduced a bug. Mark each step good or bad, then git bisect reset.

Tags & releases

4
git tag -a v1.2.0 -m "Release v1.2.0"

Create an annotated tag for a release.

git push origin v1.2.0

Push one tag.

git describe --tags --abbrev=0

Print the most recent tag.

git tag -d v1.2.0

destructive Delete a local tag.

Worktrees & extras

4
git worktree add ../feature/login feature/login

Check out another branch in a separate folder — work on two branches without stashing.

git worktree list

List linked worktrees.

git submodule update --init --recursive

Fetch and check out all submodules after cloning.

git archive --format=zip -o release.zip HEAD

Export the current tree as a ZIP without the .git folder.

Your data never leaves your computer. All processing happens 100% locally in your browser.

How to use the Git Cheat Sheet

  1. Type your own values (branch, remote, file, commit) in the boxes at the top — every example on the page updates instantly.
  2. Search or jump to a section to find the command you need.
  3. Click Copy to copy a ready-to-run line, or Copy section for a commented block.
  4. Commands marked destructive delete or overwrite data — read the note before running them.

The Git workflow in one minute

Most work follows the same loop: create a branch with git switch -c, make changes, review them with git diff, stage with git add -p, commit, and push with git push -u. Before opening a pull request, update your branch with git pull --rebase origin main so it applies cleanly.

Commands that rewrite history

commit --amend, rebase, reset and push --force change existing commits. That is fine on a branch only you use, but on shared branches it forces everyone else to repair their copies. On shared branches undo with git revert, and if you must force-push, use --force-with-lease.

Nothing is lost for 30 days

Even after a bad reset --hard or rebase, committed work is still in the repository. git reflog lists where HEAD has been, so you can return to any recent state. Uncommitted changes are the exception — commit or stash often.

Generate commands for specific situations with the Git command generator and start new repositories with the .gitignore generator.

Tools that pair well with the Git Cheat Sheet

  • Docker Cheat Sheet — The Docker and Docker Compose commands you reach for every week, explained in one line.
  • Linux Commands Cheat Sheet — The Linux and bash commands developers use on servers and laptops — files, search, permissions, processes, logs, networking and SSH.
  • AI Prompts for Developers — Battle-tested prompts for the coding tasks you do every day — debugging, reviews, tests, refactoring, SQL, commit messages and documentation.

Frequently asked questions

What is the difference between git fetch and git pull?
git fetch downloads new commits from the remote but does not touch your branch. git pull fetches and then merges (or rebases) them into your current branch.
How do I undo the last commit?
Use git reset --soft HEAD~1 to undo the commit and keep the changes staged. If the commit is already pushed to a shared branch, use git revert <commit> instead so nobody’s history is rewritten.
Should I use merge or rebase?
Rebase your own feature branch onto main to keep history linear and easy to read. Merge (or squash-merge) when bringing finished work into a shared branch. Never rebase commits other people have already pulled.
How do I get back a commit I lost after a reset?
Run git reflog, find the line from before the reset, and run git reset --hard HEAD@{n} or create a branch from that commit hash.

Related tools