Git

Git Cheatsheet

Quick reference of Git commands by group — copy & paste when you need it.

Git Basics
  • git init

    Create an empty Git repo in the current directory. Run with no arguments to init where you are.

  • git clone [repo]

    Clone a repo to your machine. The repo can be on the local FS or remote over HTTP/SSH.

  • git config user.name [name]

    Set the author for commits. Add --global to apply to every repo.

  • git add [path]

    Stage changes to prepare a commit. Use . to stage all, -p to stage hunk by hunk.

  • git commit -m "msg"

    Commit the staged snapshot with an inline message.

  • git status

    Show staged / unstaged / untracked files in the working tree.

Rewriting Git History
  • git commit --amend

    Replace the latest commit with the staged changes merged in. Stage nothing → just edit the message.

  • git rebase [base]

    Rebase the current branch onto base. base can be a commit ID, branch, tag, or HEAD~N.

  • git rebase -i HEAD~N

    Interactive rebase — squash, reword, drop, edit each commit.

  • git reflog

    History of every HEAD change — a lifesaver after an accidental reset --hard.

  • git reset --hard [commit]

    Clear staging + reset the working tree to the given commit. Dangerous.

Fix branches, commits and clean up history

Git Branches
  • git branch

    List local branches. Add -a to see remotes too.

  • git switch -c [name]

    Create and check out a new branch (replaces git checkout -b).

  • git switch [name]

    Switch to another branch.

  • git branch -d [name]

    Delete a merged local branch. -D to force-delete.

  • git push origin --delete [name]

    Delete a branch on the remote.

Merge & Rebase
  • git merge [branch]

    Merge the given branch into the current one.

  • git merge --squash [branch]

    Combine all of a branch’s commits into a single commit.

  • git rebase main

    Replay the current branch on top of main for a straight history.

  • git rebase --continue

    Continue the rebase after resolving conflicts. --abort to cancel.

  • git cherry-pick [hash]

    Bring a specific commit from another branch into the current one.

Inspect & Compare
  • git log --oneline --graph --all

    A compact commit-history graph for all branches.

  • git log -p [file]

    Change history of a single file with diffs.

  • git diff

    Compare the working tree with staging.

  • git diff --staged

    Compare staging with the latest commit.

  • git blame [file]

    See who changed which line, in which commit.

  • git show [hash]

    Show the full contents of a commit.

Stash & Restore
  • git stash

    Temporarily shelve working changes to switch branches cleanly.

  • git stash push -m "wip"

    Stash with a descriptive message.

  • git stash list

    List shelved stashes.

  • git stash pop

    Pop the latest stash and remove it from the list.

  • git restore [file]

    Restore a file (unstaged) back to the HEAD state.

  • git restore --staged [file]

    Unstage a file.

Undo Changes
  • git reset --soft HEAD~1

    Drop the latest commit, keeping staged changes.

  • git reset --mixed HEAD~1

    Drop the commit + unstage (the default of git reset).

  • git reset --hard HEAD~1

    Drop the commit + discard all changes. Be careful!

  • git revert [hash]

    Create an inverse commit to undo an old one — safe for shared branches.

  • git clean -fd

    Remove untracked files/folders. -n to dry-run first.

Remote
  • git remote -v

    List remote URLs.

  • git remote add origin [url]

    Add a remote named origin.

  • git fetch [remote]

    Download objects from the remote without merging.

  • git pull --rebase

    Fetch + rebase the current branch onto upstream — straight history.

  • git push origin [branch]

    Push a branch to the remote.

  • git push --force-with-lease

    Safe force push — fails if the remote has new commits.

Tags & Releases
  • git tag v1.0.0

    Create a lightweight tag at HEAD.

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

    Annotated tag (carries metadata, recommended for releases).

  • git push origin v1.0.0

    Push a specific tag to the remote.

  • git push origin --tags

    Push all tags.

  • git tag -d v1.0.0

    Delete a local tag.

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

    Configure the global author name.

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

    Configure the author email.

  • git config --global init.defaultBranch main

    Set the default branch for git init.

  • git config --global pull.rebase true

    Make pull use rebase by default instead of merge.

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

    Use VS Code as the editor for commit messages.