Git Cheat Sheet#

See also: Git Cheat Sheet

Setup and Config#

  • git config: Manage configuration options.
    • Scope:
      • --system: System-wide settings.
      • --global: User-specific settings.
      • --local: Repository-specific settings (default).
    • Verb:
      • list --show-origin: List all settings and their source file.
    • Common Keys:
      • user.name, user.email: Identification.
      • core.editor: Default text editor.
      • init.defaultBranch: Default branch name for git init.
      • color.ui: Enable colored output.
      • alias.<alias-name>: Create custom aliases (eg. git config --global alias.co checkout). See gitconfig
      • credential.helper: Cache or store credentials (eg. cache or store).

Start a Working Area#

  • git init: Initialize a new Git repository.
  • git clone <repo> [<dir>]: Clone a repository into a new directory.
    • -o <name>: Use <name> instead of origin to track upstream.

Work on the Current Change#

  • git add <path>: Add file contents to the index.
    • -i, --interactive: Interactive mode.
    • -p, --patch: Interactive hunk selection.
  • git restore <file>: Restore working tree files.
    • --staged: Unstage a file.
    • -p, --patch: Interactive hunk selection.
  • git rm: Remove files from the working tree and from the index (equivalent to rm <file> && git add <file>).
    • --cached: Remove from index only (stop tracking). Useful for removing accidentally committed files (eg. git rm --cached .env then add .env to .gitignore).
  • git mv <from> <to>: Move or rename a file (equivalent to mv <from> <to>, git rm <from>, git add <to>).
  • git clean: Remove untracked files from the working tree.
    • -n: Dry run (show what would be deleted).
    • -f: Force (required to perform deletion).
    • -d: Remove untracked directories as well.
    • -x: Remove ignored files as well (eg. build artifacts).
    • -X: Remove only ignored files.

Examine the History and State#

See also: revisions

  • git status: Show the working tree status.
    • -s, --short: Show status concisely (XY format: X=staged, Y=WD; A: added, M: modified, ??: untracked).
  • git diff: Show changes between commits, commit and working tree, etc.
    • Default: working tree vs. index.
    • --staged, --cached: index vs. HEAD.
    • <commit>: working tree vs. <commit>.
    • --check: Check for whitespace errors.
  • git difftool: Show changes using external diff tools.
  • git log: Show commit logs.
    • -<n>: Show the last <n> commits.
    • -- <path>: Show only commits related to the specified path.
    • --no-merges: Do not show merge commits.
    • --graph --oneline --all: Visualize the history.
    • -p, --patch: Show diffs for each commit.
    • --stat: Show abbreviated stats.
    • -S <string>: Search for commits that change the number of occurrences of a string.
  • git show [<object>]: Show various types of objects (commits, tags, files, etc.).
  • git reflog: Show the log of where HEAD and branch references have been.
    • Think of the reflog as Git’s version of shell history.

Grow, Mark and Tweak Your Common History#

  • git commit: Record changes to the repository.
    • -m <message>: Commit message.
    • -a, --all: Stage all tracked files and commit.
    • -p, --patch: Interactive hunk selection.
    • --amend: Replace the last commit with a new one (can fix message or add missing changes).
      • --no-edit: Use the existing commit message without launching an editor.
  • git branch: Manage branches.
    • No arguments: List local branches.
    • <branch>: Create a new branch at HEAD.
    • -d <branch>: Delete a merged branch.
    • -D <branch>: Force delete a branch (required for unmerged branches).
    • -m, --move [<oldbranch>] <newbranch>: Rename a branch.
    • -f <branch> <ref>: Force move <branch> to <ref>.
    • -u <remotebranch> [<branch>]: Set upstream for <branch> (defaults to current branch).
    • --merged [<commit>], --no-merged [<commit>]: Filter branches by merge status relative to <commit> (defaults to HEAD).
  • git switch: Switch branches.
    • -c <branch> [<ref>]: Create and switch to a new branch referencing <ref> (defaults to HEAD).
  • git merge <branch>: Join two or more development histories together.
    • --squash: Produce the working tree and index state as if a real merge happened, but do not actually make a commit or move the HEAD.
  • git mergetool: Resolve merge conflicts using external merge tools (eg. vimdiff).
  • git rebase <base>: Reapply commits on top of another base tip.
    • -i, --interactive: Interactive rebase.
    • git rebase <branch1> <branch2>: Equivalent to git checkout <branch2> && git rebase <branch1>.
    • --onto <newbase> <upstream> <branch>: Rebase a range of commits.
  • git cherry-pick <commit>: Apply the changes introduced by some existing commits.
  • git reset <ref>: Reset current HEAD to the specified state.
    • --hard: Discard changes in WD and index.
    • -p, --patch: Interactive hunk selection.
    • Comparison: git reset <ref> moves HEAD (and the current branch), while git branch -f <branch> <ref> force-moves a general branch to a new commit.
  • git stash: Temporarily shelves changes. push is the default and preferred subcommand (the older save is deprecated).
    • push (or git stash alone):
      • -m <msg>: Push a new stash with a message.
      • -u, --include-untracked: Include untracked files.
      • -k, --keep-index: Stash changes but keep the index intact.
      • -p, --patch: Interactive hunk selection.
      • -i, --interactive: Interactive selection menu.
    • list: List all stashes.
    • pop [<stash>]: Apply and remove from list.
      • --index: Restore the staging area (index) state.
    • apply [<stash>]: Apply and keep in list.
      • --index: Restore the staging area (index) state.
    • drop [<stash>]: Remove from list.
    • branch <name> [<stash>]: Create a new branch from a stash and pop it.
  • git revert <ref>: Revert changes introduced by some existing commits.
    • -n, --no-commit: Apply changes without creating a new commit.
    • git revert <old-ref>..<new-ref>: Revert a range of commits (<old-ref> is exclusive, <new-ref> is inclusive). Use <old-ref>^..<new-ref> to include <old-ref>.
  • git tag <tag> [<commit>]: Create a tag referencing <commit> (defaults to HEAD).
    • Default is a lightweight tag.
    • -a: Create an annotated tag.
    • -l, --list: List tags.
    • -d <tag>: Delete a tag.

Collaborate#

  • git remote: Manage set of tracked repositories.
    • -v, --verbose: Show URLs of remotes.
    • add <name> <url>: Add a new remote.
    • rm <name>, remove <name>: Remove a remote.
    • show <name>: Show information about a remote.
    • rename <old> <new>: Rename a remote.
  • git fetch [<remote>] [<src-ref>][:<dst-ref>]: Download objects and refs from another repository.
  • git pull [<remote>] [<refspec>]: Fetch from and integrate with another repository or a local branch.
    • Equivalence:
      CommandManual Equivalent
      git pull <remote> <src>git fetch <remote> <src> && git merge <remote>/<src>
      git pull --rebase <remote> <src>git fetch <remote> <src> && git rebase <remote>/<src>
      git pull <remote> <src>:<dst>git fetch <remote> <src>:<dst> && git merge FETCH_HEAD
      git pull --rebase <remote> <src>:<dst>git fetch <remote> <src>:<dst> && git rebase FETCH_HEAD
    • --rebase: Used to solve git push failures when the remote has new commits. It applies local commits on top of newly fetched remote commits, enabling a fast-forward push.
  • git push [<remote>] [<src-ref>][:<dst-ref>]: Update remote refs along with associated objects.
    • Default Behavior: In modern Git (2.0+), running git push without arguments pushes the current branch to its corresponding upstream branch (only if the names match).
    • Global Push Options:
      • -a, --all: Push all branches.
      • --mirror: Mirror the repository (push all refs, including deletions).
    • -u, --set-upstream: Set upstream (tracking) relationship for a local branch. If the branch name is omitted, it defaults to the currently checked-out branch (eg. git push -u origin). Required for the first push of a new branch.
    • --delete <branch>: Delete a remote branch.
    • --tags: Push all local tags.
  • Note: git fetch <remote> <src>:<dst> and git push <remote> <src>:<dst> are symmetrical opposites. git pull is git fetch followed by a merge or rebase.
  • git ls-remote <remote>: List references in a remote repository.

Worktree#

  • git worktree: Manage multiple working trees.
    • list: List worktrees.
    • add <path> [-b] <branch>: Add a new worktree.

Interactive Selection (-p, --patch, -i, --interactive)#

Many commands (add, reset, restore, stash push, commit) support a “patch” mode (-p, --patch) or an “interactive” menu (-i, --interactive) to select changes at the hunk (part of a file) level.

  • y: stage/restore/stash this hunk.
  • n: do not stage/restore/stash this hunk.
  • q: quit; do not stage this hunk or any of the remaining ones.
  • s: split the current hunk into smaller hunks.
  • e: manually edit the current hunk.

# git