Git Cheat Sheet

GIT CHEAT SHEET

INSTALLATION & GUIS

Git for all platforms: https://git-scm.com

SETUP

Configuring user information used across all local repositories.

git config --global user.name "[firstname lastname]"    # set an identifiable name for commits
git config --global user.email "[valid-email]"          # set email associated with history markers
git config --global color.ui auto                       # enable colorized output

SETUP & INIT

Configuring user information, initializing and cloning repositories.

git init                 # initialize an existing directory as a Git repository
git clone [url]          # retrieve an entire repository from a hosted URL

STAGE & SNAPSHOT

Working with snapshots and the Git staging area.

git status                              # show modified files in working directory, staged for commit
git add [file]                          # stage file as it looks now for your next commit
git reset [file]                        # unstage a file while retaining changes
git diff                                # diff of what is changed but not staged
git diff --staged                       # diff of what is staged but not yet committed
git commit -m "[descriptive message]"   # commit staged content as a new snapshot

BRANCH & MERGE

Isolating work in branches, changing context, and integrating changes.

git branch                       # list branches; * marks the current branch
git branch [branch-name]         # create a new branch at the current commit
git checkout [branch]            # switch to another branch and check it out
git merge [branch]               # merge the specified branch into the current one
git log                          # show all commits in the current branch's history

SHARE & UPDATE

Retrieving updates from another repository and updating local repos.

git remote add [alias] [url]      # add a Git URL as an alias
git fetch [alias]                 # fetch all branches from that remote
git merge [alias]/[branch]        # merge remote branch into your current branch
git push [alias] [branch]         # transmit local commits to the remote branch
git pull                          # fetch and merge commits from the tracking remote

TRACKING PATH CHANGES

Versioning file removes and path changes.

git rm [file]                       # delete file and stage the removal
git mv [existing-path] [new-path]   # move/rename a file and stage the move
git log --stat -M                   # show commit logs indicating moved paths

INSPECT & COMPARE

Examining logs, diffs, and object information.

git log                              # commit history for current branch
git log branchB..branchA             # commits on branchA not on branchB
git log --follow [file]              # commits that changed file, even across renames
git diff branchB...branchA           # diff of what is in branchA and not in branchB
git show [SHA]                       # show any object in human-readable form

REWRITE HISTORY

Rewriting branches, updating commits and clearing history.

git rebase [branch]              # apply commits of current branch ahead of the specified one
git reset --hard [commit]        # clear staging area and rewrite working tree from commit

TEMPORARY COMMITS (STASH)

Temporarily store modified, tracked files to change branches.

git stash                        # save modified and staged changes
git stash list                   # list stack-order of stashed changes
git stash pop                    # write working from top of stash stack
git stash drop                   # discard changes from top of stash stack

IGNORING PATTERNS

Prevent unintentional staging or committing of files.

git config --global core.excludesfile [file]  # system-wide ignore patterns
# .gitignore examples:
logs/
*.notes
pattern*/

FIND A REGRESSION (BISECT)

Quickly locate the commit that introduced a bug by binary searching your history.

# Manual workflow
git bisect start                    # enter bisect mode
git bisect bad [bad-commit]         # a commit/branch/tag where the bug is present (e.g., HEAD)
git bisect good [good-commit]       # a commit where things worked
git bisect reset                    # leave bisect mode (return to original state)

# Automated workflow (0 = good, non‑zero = bad)
# Provide a script/command that exits with 0 when the bug is ABSENT.
# Git will run it at each step until the culprit commit is found.
git bisect start [bad] [good]
git bisect run [command]

# examples:
#   git bisect run npm test
#   git bisect run sh -c "make -j && ./app --self-check"

# Extras
git bisect skip                     # skip untestable commits
git bisect log                      # show decisions made (good/bad/skip)
git bisect replay [file]            # repeat a previous bisect session