Skip to content

Git Commands

Quick reference for common git operations.

  • git init - init new repo
  • git clone <url> - clone repo
  • git branch - list local branches
  • git branch -a - list all (local/remote) branches (-a = all)
  • git branch -d <name> - safe delete, only if merged (-d = delete)
    • use when: cleaning up after PR merged
  • git branch -D <name> - force delete, even if unmerged (-D = force delete)
    • use when: abandoning work you don’t need
  • git checkout <branch> - switch branch
  • git checkout -b <name> - create + switch (-b = branch)
  • git checkout -b <name> origin/<name> - create from remote
    • use when: checking out a branch that exists on remote but not locally
  • git status - show working tree status
  • git status -s - short format (-s = short)
  • git add <file> - stage file
  • git add . - stage all
    • includes untracked (new) files
  • git commit -m "msg" - commit with message (-m = message)
  • git commit --amend - amend last commit
    • fix message or add forgotten files
    • don’t use on already pushed commits
  • git restore --staged <file> - unstage file
  • git restore <file> - discard changes
    • warning: permanently deletes uncommitted changes
  • git remote -v - list remotes (-v = verbose)
  • git remote add <name> <url> - add remote
  • git remote remove <name> - remove remote
  • git remote set-url <name> <url> - change URL
  • git fetch - fetch from remote
    • check what’s new, doesn’t merge
    • safe to run anytime
  • git fetch --prune - fetch + remove stale branches
    • use when: cleaning up after PRs merged
  • git pull origin <branch> - fetch + merge from specific branch
    • can create merge commits
  • git pull --rebase origin <branch> - fetch + rebase (cleaner linear history)
    • puts your local commits on top of remote commits
    • avoids unnecessary merge commits
    • use when: syncing with main before pushing
  • git push -u origin <branch> - push + set upstream (-u = upstream)
    • use when: first push of a new branch
    • after this, just git push works
  • git push origin --delete <name> - delete remote branch
  • git stash - stash changes
    • temporarily save work without committing
    • use when: need to switch branches but not ready to commit
  • git stash push -m "msg" - stash with message (-m = message)
  • git stash -u - include untracked (-u = untracked)
  • git stash pop - apply + remove
    • use when: returning to stashed work
  • git stash apply - apply (keep in list)
    • use when: applying same stash to multiple branches
  • git stash list - list stashes
  • git stash drop - remove latest
  • git stash clear - remove all
  • git log - full history
  • git log --oneline - compact
  • git log --oneline --graph --all - graph all branches
    • visualize branch structure
  • git log -n 5 - last 5 commits (-n = number)
  • git log --author="name" - by author
  • git log -- <file> - for specific file
  • git diff - unstaged changes
  • git diff --staged - staged changes
    • see what will be committed
  • git diff <commit> - since commit
  • git diff <b1>..<b2> - between branches
  • git show <commit> - commit details
  • git merge <branch> - merge into current
  • git merge --no-ff <branch> - merge with commit (--no-ff = no fast-forward)
    • preserves branch history in graph
  • git merge --squash <branch> - squash merge
    • combines all commits into one
    • use when: cleaning up messy feature branch history
  • git merge --abort - abort merge
  • git rebase <branch> - rebase onto branch
    • rewrites history, use on local branches only
  • git rebase --continue - continue after conflict
  • git rebase --abort - abort rebase
  • git cherry-pick <commit> - apply commit
    • use when: need specific commit from another branch
  • git reset --soft HEAD~1 - undo commit, keep staged (--soft = keep staged)
    • use when: want to edit commit before recommitting
  • git reset --mixed HEAD~1 - undo commit, unstage (--mixed = unstage)
    • use when: want to restage selectively
  • git reset --hard HEAD~1 - undo commit, discard all (--hard = discard all)
    • warning: permanently deletes changes
  • git reset --hard origin/main - reset to remote
    • use when: local is messed up, want to match remote exactly
  • git revert <commit> - create undo commit
    • safe for pushed commits (doesn’t rewrite history)
  • git revert --no-commit <commit> - revert (no commit)
  • git tag - list tags
  • git tag <name> - lightweight tag
  • git tag -a <name> -m "msg" - annotated tag (-a = annotated, -m = message)
    • use for releases (includes metadata)
  • git tag -d <name> - delete local (-d = delete)
  • git push origin <tag> - push tag
  • git push --tags - push all tags
  • git push origin --delete <tag> - delete remote tag
  • git worktree add <path> <branch> - create new worktree for branch
    • work on multiple branches simultaneously
    • use when: need to check another branch without stashing
  • git worktree list - list all worktrees
  • git worktree remove <path> - remove worktree
  • git config --global user.name "Name" - set username
  • git config --global user.email "email" - set email
  • git config --global init.defaultBranch main - set default branch
  • git config --list - list settings
  • git config --global alias.st status
  • git config --global alias.co checkout
  • git config --global alias.br branch
  • git config --global alias.ci commit
  • git config --global alias.lg "log --oneline --graph --all"