Git cheatsheet

Copy-ready Git commands for everyday workflows

All content runs locally in your browser

Last updated: February 6, 2026
Frank Zhao - Creator
CreatorFrank Zhao

Configuration

Set the global config (name)
git config --global user.name "[name]"
Set the global config (email)
git config --global user.email "[email]"
Set default editor
git config --global core.editor "code --wait"

Get started

Create a git repository
git init
Clone an existing repository
git clone [url]
Check working tree status
git status

Commit

Stage all changes
git add -A
Commit staged changes
git commit -m "[message]"
Commit tracked changes (add + commit)
git commit -am "[message]"
Add new modifications to the last commit (no message change)
git commit --amend --no-edit
Change last commit message
git commit --amend

Branches

List local branches
git branch
List all branches (local + remote)
git branch -a
Switch to a branch
git switch [branch]
Create and switch to a new branch
git switch -c [branch]
Rename local master branch to main
git branch -m master main

Remote

Show remotes
git remote -v
Fetch from origin
git fetch origin
Pull changes
git pull
Push current branch
git push
Push and set upstream
git push -u origin [branch]

Inspect

Show commit history
git log
Show compact history
git log --oneline --decorate --graph --all
Show unstaged changes
git diff
Show staged changes
git diff --staged
Show details of a commit
git show [commit]

Stash

Stash changes
git stash
Stash with a message
git stash push -m "[message]"
List stashes
git stash list
Apply and drop latest stash
git stash pop
Apply stash without dropping
git stash apply

I've made a mistake

Discard changes in a file
git restore [file]
Undo most recent commit and keep changes staged
git reset --soft HEAD~1
Undo most recent commit and keep changes (unstaged)
git reset HEAD~1
Undo most recent commit and get rid of changes
git reset HEAD~1 --hard
Reset branch to remote state
git fetch origin git reset --hard origin/[branch-name]

Introduction / overview

The Git Cheatsheet is a curated set of commands you can copy and paste for common workflows: configuring Git, cloning a repo, committing, working with branches, syncing with remotes, and safely undoing mistakes.

Quick promise: if you can remember just a few concepts — working tree, staging area, and HEADHEAD — Git becomes much less scary.

Who is this for?

  • Developers who use Git daily but don’t want to memorize every flag.
  • Students learning version control and looking for safe “starter” commands.
  • Anyone who occasionally runs an undo command and wants to double-check.

Pairs well with other tools

If you’re cleaning up URLs in scripts, try our Slugify String tool. If you’re debugging APIs, the URL Parser and HTTP Status Codes pages are handy companions.

How to use / quick start guide

  1. Pick the section that matches your goal (for example: Commit or I’ve made a mistake).
  2. Copy the command and replace placeholders like [branch] or [url].
  3. Run it once, then verify with git status (and if needed, git log --oneline).

How to interpret results

  • If git status says “nothing to commit”, your working tree is clean.
  • If it says “changes staged for commit”, your staging area (index) has updates ready for a commit.
  • If it says “changes not staged”, your files changed but aren’t staged yet.

Safety tip

Before you run any reset command, take 10 seconds to confirm what it will change. A quick mental model is thatHEADHEAD points to “your current commit”. Many undo commands simply move HEADHEAD.

Step-by-step examples

Example 1: Configure Git for the first time

Background: you installed Git (or set up a new laptop) and commits show the wrong author.

Inputs (replace placeholders):

git config --global user.name "Ada Lovelace"
git config --global user.email "[email protected]"

Result: new commits will use that identity. You can sanity-check with git config --list.

Example 2: Undo the last commit, keep changes staged

Background: you committed too early, but the commit content is correct — you just want to edit the message or add one more file.

Command:

git reset --soft HEAD~1

Mental model (what changes):

After –soft:\text{After --soft:}HEADHEAD1HEAD \leftarrow HEAD - 1(index unchanged)\text{(index unchanged)}(files unchanged)\text{(files unchanged)}

Practical next step: run git commit --amend (or add files, then commit again).

Real-world examples / use cases

Starting a feature branch

Background: you’re about to make a change and want a clean branch.

Input: branch name feature/login

git switch -c feature/login

Apply it: open a PR from that branch when you’re done.

Syncing your local branch with remote

Background: teammates pushed changes and you want the latest before you continue.

Input: remote is origin

git fetch origin\ngit pull

Apply it: if conflicts appear, resolve them, then re-run git status.

Recovering from an accidental commit

Background: you committed debug logs and want to fix it quickly.

Input: undo 1 commit

git reset --soft HEAD~1

Apply it: remove the logs, then recommit with a clean message.

Parking work temporarily (stash)

Background: you need to quickly switch tasks without committing half-finished work.

Input: message "wip: refactor"

git stash push -m "wip: refactor"

Apply it: come back later with git stash pop.

Common scenarios / when to use

You’re not sure what changed

Useful when: you edited files and want a quick overview.

git status\ngit diff

Not ideal when: you need a full history view — use git log.

You need to create a clean branch

Useful when: you want to isolate work before opening a PR.

git switch -c [branch]

Not ideal when: you need to keep history linear — consider rebasing, carefully.

You want to undo the last commit

Useful when: the commit is wrong, but you still want the code changes.

git reset --soft HEAD~1

Not ideal when: the commit is already pushed to others — consider revert.

You want to match the remote exactly

Useful when: your local branch is messy and you want a clean reset.

git fetch origin\ngit reset --hard origin/[branch-name]

Not ideal when: you have local work you need — stash or create a backup branch first.

You need to pause work without committing

Useful when: you must switch tasks quickly.

git stash push -m "[message]"

Not ideal when: you want reviewable history — commit to a WIP branch.

You’re about to run a destructive command

Useful when: you want a quick pre-flight check.

git status\ngit log --oneline -5

Not ideal when: you need to recover remote history — use hosting tools (GitHub/GitLab) too.

Tips & best practices

Avoid common mistakes

  • Prefer git reset --soft (safer) over --hard unless you’re absolutely sure.
  • Always check git status after any “undo” operation.
  • If a commit is already shared, consider git revert (non-destructive) rather than rewriting history.

A tiny workflow that saves time

  • Start with git status.
  • Review changes with git diff.
  • Stage intentionally with git add -A, then commit.

Calculation method / formula explanation

Git isn’t “math”, but a clean mental model behaves like a little state machine. Most commands change one or more of these three: the working tree, the index (staging area), and HEADHEAD.

The 3-state model

  • Working tree: files on disk (we’ll call it WTWT).
  • Index (staging area): what’s queued to commit (call it II).
  • HEADHEAD: your current commit pointer (call it HH).
–soft reset: HH1\text{--soft reset: }H \leftarrow H - 1,,I unchangedI\ \text{unchanged},,WT unchangedWT\ \text{unchanged}

That’s why git reset --soft HEAD~1 is usually the safest “undo last commit” choice when you plan to recommit.

Related concepts / background info

Branch vs remote branch

A local branch is what you work on. A remote-tracking branch (like origin/main) is Git’s view of what the server currently has.

Reset vs revert

Reset rewrites your local history by moving HEADHEAD. Revert creates a new commit that undoes a previous commit. If others already pulled your commits, revert is often the safer social choice.

Stash isn’t a backup

Stash is a convenience shelf, not a long-term storage plan. If the work matters, consider committing to a “wip” branch.

FAQs, limitations, sources

Why does Git ask for my name/email?

Git stores an author identity with each commit. Setting user.name and user.email makes your commits consistent across repositories.

What’s the difference between git reset --soft and git reset --hard?

Soft reset mainly moves HEADHEAD. Hard reset can overwrite both staged changes and files on disk, which is why it’s risky.

I pushed a bad commit. Should I reset it?

If others might have pulled it, prefer git revert (non-destructive) to avoid breaking their history.

How do I find a commit quickly?

Use a compact graph view: git log --oneline --decorate --graph --all, then copy the commit hash into git show [commit].

Why does git commit -am miss new files?

Because -a only stages tracked files. New files still need git add.

Is stash safe?

It’s usually fine for short-term use, but it’s not a substitute for committing. If it’s important, commit it to a branch.

Can I “reset” this cheatsheet?

Yes — the Reset button clears copy state and scrolls back to the top.

Limitations / disclaimers

This cheatsheet is a convenience reference, not a full Git tutorial. Destructive commands (especially anything with --hard) can delete local work. If you’re unsure, make a backup branch or consult your team’s workflow.