Cheat Sheet

Git Commands

Git is the version control system behind almost every codebase. This sheet is a full git commands list with examples: starting a repo, the everyday commit and push loop, branching and merging, and the undo commands (reset, revert, restore) that get you out of trouble.

Last updated September 11, 2026

Commit and Push

The everyday loop: check what changed, stage it, commit it, push it.

CommandWhat it does
What changed, what is staged, what is untracked
Stage a file ( stages everything)
Stage hunk by hunk, choosing what goes in
Commit the staged changes
Stage every tracked file and commit in one step
Redo the last commit: change the message or add staged files
Push the current branch to its upstream
Push a new branch and set its upstream in one go
Force push, but refuse if someone else pushed first
Fetch the upstream branch and merge it in
Fetch and replay your commits on top instead of merging

Create a Branch

CommandWhat it does
Create a branch and switch to it
Same thing, the older form
Create a branch but stay where you are
New branch starting from main, not from here
List local branches ( includes remote ones)
Print the branch you are on
Rename the current branch
Publish the branch to the remote and track it

Clone a Repo

CommandWhat it does
Clone over HTTPS
Clone over SSH, using your SSH key
Clone into a folder with a different name
Shallow clone: latest commit only, much faster
Clone and check out a specific branch
Clone including submodules
Turn the current folder into a new repo instead

on an SSH clone means the server never saw a key it accepts; see Permission denied (publickey).

Start a New Repo

CommandWhat it does
Turn the current folder into a Git repo
Init with as the first branch (Git 2.28+)
Create the folder and init it in one step
Stage everything for the first commit
The first commit
Point the repo at an empty remote
First push, and set the branch to track

The whole sequence, from an empty folder to a pushed repo:

git init -b main
git add .
git commit -m "Initial commit"
git remote add origin [email protected]:user/repo.git
git push -u origin main

Switch Branches

(Git 2.23+) only switches branches; also overwrites files, which is exactly how edits get lost by accident.

CommandWhat it does
Switch to a branch
Same, the older command
Back to the branch you were just on
Create and switch
Look at an old commit (detached HEAD; returns)
Keep detached-HEAD commits by putting a branch on them
Take one file from another branch into this one

Merge

Merge from the branch that should receive the changes: switch to main, then merge the feature in.

CommandWhat it does
Merge feature into the current branch
Always create a merge commit, even when fast-forward is possible
Bring all of feature over as one uncommitted change set
Bail out of a conflicted merge, back to the state before it
List the files still in conflict
Take your side of a conflicted file ( for the other)
Mark a conflict as resolved after editing it
Finish the merge once every conflict is resolved

Rebase

Rebase replays your commits on top of another branch, so history stays a straight line. It rewrites commits, so keep it off branches other people pull from. The git rebase cheat sheet covers interactive rebase and conflicts step by step.

CommandWhat it does
Replay this branch's commits on top of main
Interactive: squash, reword, reorder, or drop the last 3 commits
Carry on after fixing a conflict
Put everything back the way it was before the rebase
Drop the commit that conflicts and move on
Update from main without a merge commit

Stash

CommandWhat it does
Shelve uncommitted changes and get a clean tree
Include untracked files
Stash with a label you will recognise later
List all stashes
Reapply the latest stash and remove it from the list
Reapply a specific stash, keep it in the list
See what a stash actually contains
Turn a stash into a new branch
Delete one stash ( deletes all)

Reset

Reset moves the branch pointer to another commit. The flag decides what happens to your files: keeps changes staged, the default (mixed) keeps them unstaged, deletes them.

CommandWhat it does
Undo the last commit, keep the changes staged
Undo the last commit, keep the changes unstaged
Undo the last commit and throw the changes away
Make the branch match the remote exactly, dropping local work
Unstage a file without losing the edit
Every commit HEAD has pointed at, including ones you reset away
Jump back to a commit found in the reflog

Revert

Revert undoes a commit by adding a new commit with the opposite change. History is untouched, which makes it the safe undo for anything already pushed.

CommandWhat it does
Undo the last commit with a new commit
Undo any specific commit
Undo several commits, then commit once
Revert a merge commit, keeping the main line's side
Stop a revert that hit conflicts

Log and History

CommandWhat it does
One line per commit
Branch graph in the terminal
Every change to one file, with the diffs
Commits by one person
Recent commits only
Search commit messages
Commits that added or removed a string anywhere in the code
One commit in full: message, author, diff
Who last touched each line, and in which commit
Commit count per author
Commits on the remote you have not merged yet

Diff

CommandWhat it does
Unstaged changes against the last commit
What is about to be committed
Everything that differs between two branches
Between any two commits
One file against another branch
Compare a local branch with its remote counterpart
Just file names and change counts
File names only
Everything changed in the last 3 commits

Delete a Branch

CommandWhat it does
Delete a local branch that is merged
Delete it even if unmerged (the work is gone with it)
Delete the branch on the remote
Drop local records of branches already deleted on the remote
List branches fully merged into main, safe to delete

Undo Changes

The undo command depends on where the change lives: unstaged, staged, committed, or pushed. Reset and Revert above handle the committed cases.

CommandWhat it does
Discard uncommitted edits to a file (no undo for this)
Discard every uncommitted edit in the repo
Older form of the same
Unstage a file, keep the edit
Fix the last commit message
Add the staged files to the last commit, keep its message
Delete untracked files and folders ( first to preview)
The escape hatch: find the commit a bad reset or rebase lost

Remotes

SSH remotes () authenticate with your SSH key instead of a token; generate one with ssh-keygen and see the SSH cheat sheet for key handling.

CommandWhat it does
List remotes and their URLs
Connect a local repo to a remote
Change the URL, e.g. switch HTTPS to SSH
Download remote commits without touching your files
Update every remote, drop deleted branches
Track the original repo of a fork
Bring the original repo's changes into your fork
Set the current branch's upstream
Disconnect a remote

Repo Info

CommandWhat it does
Which Git version is installed
The branch you are on (Git 2.22+)
Same, and it works on older Git
Full hash of the current commit
The repo's remote URL
Absolute path to the repo root
Short status, plus branch and ahead/behind counts
The email this repo commits with
Every command this install has

Cherry-Pick

CommandWhat it does
Copy one commit onto the current branch
Copy several commits
Copy a range (commits after A, up to and including B)
Record the source commit in the new message
Apply the changes without committing yet
Carry on after fixing a conflict ( cancels)

Tags

CommandWhat it does
Annotated tag: use this for releases
Lightweight tag, just a pointer
List tags ( filters)
Push one tag; plain does not send tags
Push every tag
Delete a tag locally
Delete it on the remote too
Look at the code as it was at that tag (detached HEAD)
Nearest tag to the current commit

Submodules

CommandWhat it does
Put another repo inside this one at that path
Fetch submodule contents after a plain clone
Clone and fetch submodules in one step
Move submodules to their latest upstream commit
Which commit each submodule sits on
Run a command inside every submodule
Remove a submodule (then commit the removal)

Config

Set your name and email before the first commit; every commit records them permanently.

CommandWhat it does
Your name, for every repo on this machine
Your email; match the one on GitHub or GitLab
A different email for just this repo
Every setting and which file it comes from
New repos start on main
Editor for commit messages and rebases
Make rebase instead of merge
First push sets the upstream by itself (Git 2.37+)
Shortcut: runs
Windows line endings: check out CRLF, commit LF ( on Mac and Linux)

Credentials

HTTPS remotes ask for a username and password on every push unless a credential helper stores them. GitHub and GitLab want a personal access token there, not your account password.

CommandWhat it does
macOS: keep credentials in the Keychain
Windows: use Git Credential Manager
Hold them in memory for 15 minutes ( to extend)
Save them to a plain text file in your home directory
Forget cached credentials right now
Stop storing them and go back to prompting
Switch to SSH so Git stops asking at all

An SSH remote authenticates with your key instead; make one with ssh-keygen.

Gotchas

  • Only two commands destroy work with no undo: on uncommitted changes, and . Anything committed survives and finds it; anything never committed does not.
  • Never force push a branch other people pull from. When a rebase makes a force push unavoidable, at least refuses to overwrite commits you have not seen.
  • is plus . If your history fills up with "Merge branch" commits, set and it becomes fetch plus rebase.
  • Commits with the wrong author email do not fix themselves when you correct the config. fixes the last one; older ones need an interactive rebase.
  • A "detached HEAD" is not an error, it just means no branch will record new commits. keeps them, walks away.
  • does not push tags. Release tags need or .

Git FAQ

How do I go back to a previous commit in Git?

Three different things hide behind that question. To look at an old commit without changing anything, git checkout <hash> puts you in detached HEAD and git switch - brings you back. To move the branch itself back, git reset --hard <hash>, which throws away every local change after that point. To undo a commit that is already pushed, git revert <hash> adds a new commit with the opposite change and leaves history intact. If a reset went too far, git reflog lists every commit HEAD has pointed at for about 90 days, and git reset --hard with that hash restores it.

Which Git command creates a new branch?

git branch feature creates it but leaves you where you are. git switch -c feature creates it and moves you onto it, which is usually what people want; git checkout -b feature is the older spelling of the same thing. A new branch starts from wherever HEAD is right now, so to branch off main while sitting somewhere else, name the start point: git switch -c hotfix main. Nothing exists on the remote until git push -u origin feature, which also sets the upstream so later pushes need no arguments.

Which Git command initializes a new repository?

git init, run inside the folder you want tracked. It creates a .git directory and nothing else, so no files are tracked until you add and commit them. Git 2.28 and later accept git init -b main to name the first branch, or set it once with git config --global init.defaultBranch main. Running git init in a folder that already has files is fine, and running it twice does no harm. You never need it after git clone, which initializes the repo for you.

Which Git commands need an internet connection?

Only the ones that talk to a remote: clone, fetch, pull, push, git remote update, git ls-remote, and submodule updates that fetch. Everything else runs locally, including commit, branch, merge, rebase, log, diff, stash, tag and reset, because your clone holds the full history. That is why you can keep committing on a plane and push the lot when you land. git pull is the one that catches people out offline, since it is fetch plus merge and dies at the fetch.

What are Git porcelain and plumbing commands?

Porcelain commands are the ones meant for people: status, add, commit, branch, merge, log. Plumbing commands are the low-level ones they are built on, such as hash-object, cat-file, rev-parse, update-ref and rev-list, and they are what scripts should call. The naming trips people up in one place. git status --porcelain does the opposite of what it sounds like: it prints a stable, machine-readable format meant for scripts, and unlike normal status output it will not change between Git versions.

Related cheat sheets