Search EverydayTab

Type a tool name, tag or category.

EverydayTabEverydayTab

Git Cheat Sheet

Commonly used Git commands reference

Fill placeholders once and every command below uses them. 63 commands.

Setup

  • git config --global user.name "<name>"

    Set the name recorded on your commits

  • git config --global user.email "<email>"

    Set the email recorded on your commits

  • git config --global init.defaultBranch main

    Use main for new repositories

  • git config --global pull.rebase true

    Rebase instead of merge when pulling

  • git config --list --show-origin

    Show all settings and where they come from

Start a repository

  • git init

    Create a repository in the current folder

  • git clone <url>

    Download a repository and its history

  • git clone --depth 1 <url>

    Shallow clone — latest snapshot only, much faster

  • git remote add origin <url>

    Connect a local repo to a remote

  • git remote -v

    List remotes and their URLs

Everyday work

  • git status -sb

    Short status with branch info

  • git add <file>

    Stage a file

  • git add -p

    Stage changes interactively, hunk by hunk

  • git commit -m "<message>"

    Commit staged changes

  • git commit -am "<message>"

    Stage all tracked files and commit

  • git diff

    Unstaged changes

  • git diff --staged

    Staged changes (what will be committed)

  • git pull

    Fetch and integrate remote changes

  • git push -u origin <branch>

    Push a new branch and set its upstream

Branches

  • git switch -c <branch>

    Create and switch to a new branch

  • git switch <branch>

    Switch to an existing branch

  • git switch -

    Go back to the previous branch

  • git branch -a

    List local and remote branches

  • git branch -d <branch>

    Delete a merged branch

  • git branch -D <branch>

    Force-delete a branch (even if unmerged)destructive

  • git push origin --delete <branch>

    Delete a remote branchdestructive

  • git branch -m <new-name>

    Rename the current branch

Merge & rebase

  • git merge <branch>

    Merge a branch into the current one

  • git merge --no-ff <branch>

    Always create a merge commit

  • git rebase main

    Replay your commits on top of main

  • git rebase -i HEAD~<n>

    Edit, squash, reorder or drop the last n commits

  • git rebase --continue

    Continue after resolving conflicts (or --abort)

  • git cherry-pick <commit>

    Apply one commit from another branch

  • git merge --abort

    Give up a merge that has conflicts

Undo & fix mistakes

  • git restore <file>

    Discard unstaged changes to a filedestructive

  • git restore --staged <file>

    Unstage a file (keep the changes)

  • git commit --amend

    Edit the last commit (message or content)

  • git commit --amend --no-edit

    Add staged changes to the last commit

  • git reset --soft HEAD~1

    Undo the last commit, keep changes staged

  • git reset --hard HEAD~1

    Delete the last commit and its changesdestructive

  • git revert <commit>

    Create a new commit that undoes a commit — safe on shared branches

  • git reflog

    Every position HEAD has been — recover “lost” commits

  • git clean -fd

    Delete untracked files and foldersdestructive

  • git push --force-with-lease

    Force-push only if nobody else pushed meanwhiledestructive

Stash

  • git stash push -m "<message>"

    Shelve uncommitted changes

  • git stash -u

    Stash including untracked files

  • git stash list

    List stashes

  • git stash pop

    Re-apply the latest stash and remove it

  • git stash apply stash@{<n>}

    Re-apply a stash but keep it

History & search

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

    Compact visual history of all branches

  • git log -p <file>

    Every change to a file

  • git log -S "<text>"

    Find commits that added or removed text

  • git blame <file>

    Who last changed each line

  • git show <commit>

    Show a commit's changes

  • git bisect start

    Binary-search for the commit that introduced a bug

  • git grep "<text>"

    Search tracked files

Tags & releases

  • git tag -a v<version> -m "<message>"

    Create an annotated tag

  • git push origin --tags

    Push all tags

  • git describe --tags

    Nearest tag + commits since

Advanced

  • git worktree add ../<folder> <branch>

    Check out another branch in a second folder

  • git submodule update --init --recursive

    Fetch submodules after cloning

  • git sparse-checkout set <folder>

    Only check out part of a large repo

  • git gc --prune=now

    Clean up and compress the repository