Git & GitHub Cheatsheet: Executive Summary
Git is a distributed version control system for tracking changes in files and coordinating work among multiple developers.
It stores snapshots of a project’s history (commits) that form a timeline, allowing features like branching, merging, and
reverting. Common workflows involve committing changes locally, then synchronizing with remote repositories (e.g., on
GitHub) via fetch, pull, and push. This cheatsheet summarizes core Git commands, repository elements, and example
problem-solving scenarios.
Core Git Commands
Command Purpose/Behavior
git init Initialize a new local Git repository (creates a .git directory).
git clone Copy a repository (usually from a remote URL) into a new directory. Sets up origin remote.
git status Show current branch, staged/unstaged/untracked changes. Does not display commit history.
git add Stage file contents for the next commit (add to index).
git commit Record a snapshot of staged changes into history (create a commit).
git log Show commit history (list of snapshots).
git diff Show changes between commits or between working tree and HEAD.
git branch List or create branches (independent lines of development).
git checkout / switch Switch branches or restore files. Checking out a branch updates the working directory to that branch’s
To create+switch: git checkout -b <name> (or use git switch <name>).
git merge Merge one branch into current branch (combine histories). Creates a new merge commit by default.
git rebase Reapply commits on top of another base (rewrite history to linearize).
git pull Fetch from remote and merge into current branch (i.e. fetch + merge).
git push Upload local commits to a remote repository, updating remote branches.
git fetch Download commits and refs from remote, updating origin/* without merging.
git reset Undo changes: --soft (keep work), --mixed (default, unstage), --hard (discard).
git stash Temporarily stash working directory changes (both staged and unstaged).
git tag Create or list tags (fixed references to commits, typically for releases).
git remote Manage tracked remote repositories (e.g., origin).
git config Set or get configuration options (e.g., [Link], alias.*).
Key Commands (details)
git init (Initialize a new local repo)
git init [directory] (Create an empty Git repository in the specified directory)
git init -b [branch name] (Initialize with a specific default branch name)
git clone [url] (Clone a repository into a new directory)
git clone --depth 1 [url] (Shallow clone, only latest history)
git clone -b [branch] [url] (Clone a specific branch)
git status (Show working tree status)
git status -s (Show status in short format)
git add [file] (Add file contents to the index)
git add . (Add all changes in current directory to the index)
git add -p (Interactively choose hunks of patch to add)
git commit -m "[message]" (Commit with a message)
git commit -a -m "[message]" (Automatically stage modified/deleted files and commit)
git commit --amend (Modify the last commit)
git commit --amend --no-edit (Add changes to last commit without changing message)
git log (List commit history)
git log --oneline (Show history in a compact single-line format)
git log --graph --oneline --all (Show ASCII graph of all branches)
git log -p (Show patch/diff introduced at each commit)
1
git diff (Show unstaged changes)
git diff --staged (Show staged changes ready to commit)
git diff [branch1]..[branch2] (Show differences between two branches)
git branch (List all local branches)
git branch -a (List all local and remote branches)
git branch [branch name] (Create a branch)
git branch -d [branch name] (Delete a branch)
git branch -m [new name] (Rename current branch)
git checkout [branch name] (Switch branches)
git checkout -b [branch name] (Create and switch to a branch)
git checkout -- [file] (Discard changes in working directory)
git switch [branch name] (Switch branches)
git switch -c [branch name] (Create and switch to a branch)
git merge [branch name] (Merge branch into current branch)
git merge --no-ff [branch name] (Merge creating a commit even if fast-forward)
git merge --abort (Abort an ongoing conflict-ridden merge)
git rebase [branch name] (Reapply local commits on top of another base)
git rebase -i [commit] (Interactive rebase for squashing/editing history)
git pull (Fetch from and integrate with another repository/branch)
git pull --rebase (Pull and rebase local commits instead of merging)
git push (Update remote refs along with associated objects)
git push -u origin [branch name] (Push branch and set upstream)
git push --force (Force push to overwrite remote history)
git push --tags (Push all local tags)
git fetch (Download objects and refs from another repository)
git fetch --prune (Remove remote-tracking branches that no longer exist)
git reset [file] (Unstage file, keep working directory changes)
git reset --soft HEAD~1 (Undo last commit, keep changes staged)
git reset --hard (Discard all local changes and unstaged untracked files)
git revert [commit] (Create a new commit that undoes the specified commit)
git stash (Stash current changes)
git stash pop (Apply and remove top stashed state)
git stash list (List all stashed states)
git stash apply stash@0 (Apply specific stash without dropping it)
git stash drop (Remove specific/top stash)
git tag [tag name] (Create a lightweight tag)
git tag -a [name] -m "[message]" (Create an annotated tag)
git tag -d [tag name] (Delete a tag)
git remote -v (List all configured remote repositories)
2
git remote add [name] [url] (Add a new remote repository)
git remote remove [name] (Remove a remote repository)
git config --global [Link] "[name]" (Set global author name)
git config --global [Link] "[email]" (Set global author email)
git config --global [Link] "[editor]" (Set global default text editor)
git config --list (List all current Git configurations)
.gitignore
A .gitignore file specifies intentionally untracked files that Git should ignore. Each line is a glob pattern:
*.log Ignore all .log files
build/ Ignore any directory named build/
*.aux Ignore LaTeX auxiliary files (e.g. .aux, .toc)
node modules/ Ignore [Link] dependencies folder
*.env Ignore environment files
!.gitignore Negate ignore: do not ignore the .gitignore file itself
Lines beginning with # are comments. A leading ! in a pattern re-includes previously ignored paths. A trailing slash
denotes directories only. Patterns are relative to the location of the .gitignore file.
To stop tracking a file that’s already in the repo (so that .gitignore can ignore it), use:
git rm --cached <file>
This removes the file from the index (Git stops tracking it) but leaves it on disk. Then commit the change to apply it.
Repository Elements
Branch: A named pointer to a commit (a line of development).
Commit: A snapshot of the repo at a point in time, identified by a SHA hash. Commits link in a history graph.
HEAD: The current branch or commit reference. Normally points to a branch’s tip.
Staging Area (Index): The intermediate area for changes marked to be committed.
Remote: A named reference to another repository (often on a server). Used with fetch/pull/push.
Tag: A fixed ref pointing to a specific commit (e.g., for a release). Tags do not move when new commits are made.
.gitignore: File listing ignore patterns for untracked files.
Hooks: Custom scripts in .git/hooks/ (e.g., pre-commit, post-commit) that run on Git events.
CI Files: Continuous integration configuration (e.g., GitHub Actions YAML in .github/workflows/), defining
automated build/test pipelines.
Unconventional Scenarios
Examples of solving development tasks with Git commands:
1. Move uncommitted changes from main to dev: If you made edits on main but want them on dev, do:
git stash # save current changes
git checkout -b dev # create and switch to dev
git stash pop # reapply changes on dev
git add .
git commit -m "Work moved to dev"
2. Remove an accidentally committed file (e.g. secret): If [Link] was committed, remove it and update
.gitignore:
git rm --cached [Link] # untrack the file
echo "[Link]" >> .gitignore
git commit -m "Remove secret key and ignore it"
git push
3. Undo last commit but keep changes: If the last commit was bad, revert it to staged changes:
git reset --soft HEAD^ # undo last commit, keep changes staged
git commit -m "Corrected commit message"
3
4. Find the commit that introduced a bug (git bisect): Use binary search to locate the offending commit:
git bisect start
git bisect bad # mark current (bad) commit
git bisect good <known-good> # mark a previous good commit
# Git checks out midpoint; test it:
git bisect good # if bug absent
# or
git bisect bad # if bug present
# Repeat until identified; then:
git bisect reset
5. Recover a lost commit via reflog: If you reset or deleted a branch, find the commit in reflog and restore it:
git reflog show # find lost commit SHA
git checkout -b recover <SHA> # restore on new branch
6. Cherry-pick a commit to another branch: To apply a specific commit (e.g., hotfix) onto main:
git checkout main
git cherry-pick <commitSHA>
This copies that change into main without merging the whole branch.
graph TD
A[main] --> B[dev]
B --> C[feature]
C --> D["merge commit"]
D --> A