Git Cheat Sheet

Comprehensive reference for Git commands

Back to Cheat Sheets

Getting Started

  • $ git init Start a new repo
  • $ git clone <url> Clone an existing repo

Prepare to Commit

  • $ git add <file> Add untracked file or unstaged changes
  • $ git add . Add all untracked files and unstaged changes
  • $ git add -p Choose which parts of a file to stage
  • $ git mv <old> <new> Move file
  • $ git rm <file> Delete file
  • $ git rm --cached <file> Tell Git to forget about a file without deleting it
  • $ git reset <file> Unstage one file
  • $ git reset Unstage everything
  • $ git status Check what you added

Make Commits

  • $ git commit Make a commit (opens editor)
  • $ git commit -m 'message' Make a commit with message
  • $ git commit -am 'message' Commit all unstaged changes

Move Between Branches

  • $ git switch <name> Switch branches
  • $ git checkout <name> Switch branches (legacy)
  • $ git switch -c <name> Create and switch to a new branch
  • $ git checkout -b <name> Create and switch to a new branch (legacy)
  • $ git branch List branches
  • $ git branch --sort=-committerdate List branches by most recently committed to
  • $ git branch -d <name> Delete a branch
  • $ git branch -D <name> Force delete a branch

Diff Staged/Unstaged Changes

  • $ git diff HEAD Diff all staged and unstaged changes
  • $ git diff --staged Diff just staged changes
  • $ git diff Diff just unstaged changes

Diff Commits

  • $ git show <commit> Show diff between a commit and its parent
  • $ git diff <commit> <commit> Diff two commits
  • $ git diff <commit> <file> Diff one file since a commit
  • $ git diff <commit> --stat Show a summary of a diff
  • $ git show <commit> --stat Show a summary of a diff (show)

Discard Your Changes

  • $ git restore <file> Delete unstaged changes to one file
  • $ git checkout <file> Delete unstaged changes (legacy)
  • $ git restore --staged --worktree <file> Delete all staged and unstaged changes to one file
  • $ git checkout HEAD <file> Delete all staged and unstaged changes (legacy)
  • $ git reset --hard Delete all staged and unstaged changes
  • $ git clean Delete untracked files
  • $ git stash 'Stash' all staged and unstaged changes

Edit History

  • $ git reset HEAD^ "Undo" the most recent commit (keep your working directory the same)
  • $ git rebase -i HEAD~6 Squash the last 5 commits into one
  • $ git reflog BRANCHNAME Undo a failed rebase (find commit ID)
  • $ git reset --hard <commit> Reset to found commit ID
  • $ git commit --amend Change a commit message (or add a file you forgot)

Code Archaeology

  • $ git log main Look at a branch's history
  • $ git log --graph main Look at history with graph
  • $ git log --oneline Look at history (oneline)
  • $ git log <file> Show every commit that modified a file
  • $ git log --follow <file> Show history including before rename
  • $ git log -G banana Find every commit that added or removed "banana"
  • $ git blame <file> Show who last changed each line of a file

Important Files

  • .git/config Local git config
  • ~/.gitconfig Global git config
  • .gitignore List of files to ignore

Combine Diverged Branches

  • $ git rebase main Combine with rebase (from current branch)
  • $ git merge banana Combine with merge (into main)
  • $ git merge --squash banana Combine with squash merge
  • $ git cherry-pick <commit> Copy one commit onto the current branch

Restore an Old File

  • $ git checkout <commit> <file> Get the version of a file from another commit
  • $ git restore <file> --source <commit> Get version (restore)

Add a Remote

  • $ git remote add <name> <url> Add a remote

Push Your Changes

  • $ git push origin main Push main branch to remote origin
  • $ git push Push the current branch to its remote "tracking branch"
  • $ git push -u origin <name> Push a branch that you've never pushed before
  • $ git push --force-with-lease Force push
  • $ git push --tags Push tags

Pull Changes

  • $ git fetch origin main Fetch changes (but don't change any of your local branches)
  • $ git pull --rebase Fetch changes and then rebase your current branch
  • $ git pull origin main Fetch changes and then merge them into your current branch
  • $ git pull Pull changes

Configure

  • $ git config user.name "Name" Set user name
  • $ git config --global ... Set option globally
  • $ git config alias.st status Create an alias
Reference: git-scm.com