Skip to content
HQ8T

Git Cheat Sheet

Searchable Git commands with explanations.

Setup

  • git config --global user.name "Your Name"

    Set the name on your commits

  • git config --global user.email "you@example.com"

    Set the email on your commits

  • git config --global init.defaultBranch main

    Use main for new repositories

  • git init

    Start a repository in the current folder

  • git clone <url>

    Copy a remote repository

Everyday

  • git status

    See changed and staged files

  • git add <file>

    Stage a file (git add . for everything)

  • git commit -m "message"

    Commit staged changes

  • git commit -am "message"

    Stage tracked files and commit in one step

  • git diff

    Show unstaged changes

  • git diff --staged

    Show staged changes

  • git log --oneline --graph --all

    Compact history of all branches

Branches

  • git branch

    List local branches

  • git switch -c <branch>

    Create and switch to a new branch

  • git switch <branch>

    Switch to an existing branch

  • git merge <branch>

    Merge a branch into the current one

  • git branch -d <branch>

    Delete a merged branch

  • git branch -m <new-name>

    Rename the current branch

Remote

  • git remote add origin <url>

    Connect a remote repository

  • git push -u origin <branch>

    Push a branch and track it

  • git pull

    Fetch and merge from the tracked branch

  • git pull --rebase

    Fetch and replay your commits on top

  • git fetch --prune

    Update remote branches and remove deleted ones

  • git remote -v

    Show remote URLs

Undo

  • git restore <file>

    Discard unstaged changes to a file

  • git restore --staged <file>

    Unstage a file, keep the changes

  • git commit --amend

    Edit the last commit (before pushing)

  • git reset --soft HEAD~1

    Undo the last commit, keep changes staged

  • git revert <commit>

    Create a new commit that undoes one (safe after pushing)

  • git reflog

    Find lost commits and branch positions

Stash

  • git stash

    Save uncommitted changes for later

  • git stash -u

    Stash including untracked files

  • git stash list

    List stashes

  • git stash pop

    Re-apply the latest stash and drop it

Rebase & history

  • git rebase main

    Replay your branch on top of main

  • git rebase -i HEAD~3

    Edit, squash or reorder the last 3 commits

  • git cherry-pick <commit>

    Copy one commit onto the current branch

  • git blame <file>

    See who last changed each line

  • git show <commit>

    Show a commit's changes

Tags

  • git tag v1.0.0

    Tag the current commit

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

    Create an annotated tag

  • git push origin --tags

    Push all tags

Clean up

  • git rm --cached <file>

    Stop tracking a file but keep it on disk

  • git clean -n

    Preview untracked files that would be deleted

  • git clean -fd

    Delete untracked files and folders (careful!)

How to use

  1. 1Search for what you want to do, like undo or rename.
  2. 2Read the one-line explanation.
  3. 3Copy the command.
  4. 4Replace placeholders like <branch> before running it.

Frequently asked questions

How do I undo the last commit?

git reset --soft HEAD~1 keeps your changes staged. If you've already pushed, use git revert <commit> instead.

What's the difference between merge and rebase?

Merge joins histories with a merge commit; rebase replays your commits on top for a straight history. Don't rebase shared branches.

How do I rename a branch?

git branch -m new-name renames the current branch.

How do I save work without committing?

git stash saves it; git stash pop brings it back.

Is git switch the same as git checkout?

git switch is the newer, clearer command for changing branches; checkout still works.