Git Notes
Comprehensive Git command reference organized by category. Includes syntax, common options, and practical examples for every major Git command.
This reference organizes every major Git command by category with practical usage examples. Bookmark this page and return to it whenever you need to remember syntax or discover options you did not know existed.
Git has over 150 commands, but day-to-day development uses roughly 20 of them. We cover the essential commands thoroughly and include advanced ones that solve specific problems when you need them.
Repository Setup Commands
| Command | Purpose | Example |
|---|---|---|
git init | Create new repository | git init my-project |
git clone <url> | Copy remote repository | git clone git@github.com:org/repo.git |
git clone --depth 1 | Shallow clone (latest only) | git clone --depth 1 <url> |
git clone --branch <name> | Clone specific branch | git clone -b develop <url> |
git clone --recurse-submodules | Clone with submodules | git clone --recurse-submodules <url> |
Daily Workflow Commands
Staging and Committing
| Command | Purpose |
|---|---|
git add <file> | Stage specific file |
git add . | Stage all changes in current directory |
git add -A | Stage all changes (entire repository) |
git add -p | Interactive partial staging (choose hunks) |
git commit -m "msg" | Commit with inline message |
git commit | Commit with editor for longer message |
git commit --amend | Modify the last commit |
git commit --amend --no-edit | Add files to last commit without changing message |
git commit --allow-empty -m "msg" | Create commit with no file changes |
Status and Diff
| Command | What It Shows |
|---|---|
git status | Current state of working directory and staging |
git status -s | Short format status output |
git diff | Unstaged changes (working dir vs staging) |
git diff --staged | Staged changes (staging vs last commit) |
git diff HEAD | All changes since last commit |
git diff branch1..branch2 | Differences between two branches |
git diff --stat | Summary of changes (files and line counts) |
Branching Commands
| Command | Purpose |
|---|---|
git branch | List local branches |
git branch -a | List all branches (local + remote) |
git branch -r | List remote-tracking branches |
git branch <name> | Create new branch (do not switch) |
git branch -d <name> | Delete branch (safe - checks merged) |
git branch -D <name> | Force delete branch (no check) |
git branch -m <new-name> | Rename current branch |
git branch -m <old> <new> | Rename specific branch |
git branch --merged main | List branches merged into main |
git checkout -b <name> | Create and switch to new branch |
git switch -c <name> | Create and switch (modern syntax) |
git switch <name> | Switch to existing branch |
git checkout <name> | Switch branch (older syntax) |
Merging and Rebasing
| Command | Purpose |
|---|---|
git merge <branch> | Merge branch into current branch |
git merge --no-ff <branch> | Force merge commit (no fast-forward) |
git merge --squash <branch> | Squash all commits into one, stage only |
git merge --abort | Cancel in-progress merge |
git rebase <branch> | Rebase current branch onto target |
git rebase -i HEAD~n | Interactive rebase (squash, reorder, edit) |
git rebase --continue | Continue after resolving conflict |
git rebase --abort | Cancel rebase, restore original state |
git cherry-pick <hash> | Apply specific commit to current branch |
git cherry-pick --no-commit <hash> | Apply changes without committing |
Remote Operations
| Command | Purpose |
|---|---|
git remote -v | List remotes with URLs |
git remote add <name> <url> | Add new remote connection |
git remote remove <name> | Remove a remote |
git remote set-url <name> <url> | Change remote URL |
git fetch | Download all remote changes (no merge) |
git fetch --prune | Fetch and remove stale remote refs |
git pull | Fetch and merge (or rebase) |
git pull --rebase | Fetch and rebase local on top of remote |
git pull --ff-only | Only pull if fast-forward possible |
git push | Upload to tracked remote branch |
git push -u origin <branch> | Push and set upstream tracking |
git push --force-with-lease | Safe force push (checks for changes) |
git push origin --delete <branch> | Delete remote branch |
git push --tags | Push all tags to remote |
History and Inspection
| Command | Purpose |
|---|---|
git log | Full commit history |
git log --oneline | Compact one-line per commit |
git log --oneline --graph --all | Visual branch graph |
git log --author="name" | Filter by author |
git log --since="2024-01-01" | Filter by date |
git log -p | Show patches (diffs) with each commit |
git log --stat | Show file change statistics |
git log -S "text" | Find commits that added/removed text |
git log --grep="message" | Search commit messages |
git show <hash> | Display specific commit details |
git blame <file> | Show who changed each line and when |
git reflog | Show HEAD movement history |
Undoing Changes
| Command | Effect |
|---|---|
git restore <file> | Discard working directory changes |
git restore --staged <file> | Unstage (keep changes in working dir) |
git reset --soft HEAD~1 | Undo commit, keep changes staged |
git reset HEAD~1 | Undo commit, keep changes unstaged |
git reset --hard HEAD~1 | Undo commit, discard all changes |
git revert <hash> | Create new commit undoing target |
git stash | Temporarily shelve all changes |
git stash pop | Restore most recent stash |
git stash list | Show all stashed entries |
git stash drop | Delete most recent stash |
git clean -fd | Remove untracked files and directories |
Configuration Commands
# Identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Editor
git config --global core.editor "code --wait"
# Default branch name
git config --global init.defaultBranch main
# Pull behavior
git config --global pull.rebase true
# Auto-prune on fetch
git config --global fetch.prune true
# Aliases (shortcuts)
git config --global alias.st "status"
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.unstage "restore --staged"
# View all config
git config --list --show-originUseful Combinations and Patterns
# See what would change in a PR (compare feature to main)
git log main..feature/auth --oneline
git diff main...feature/auth --stat
# Find when a bug was introduced
git bisect start
git bisect bad HEAD
git bisect good v1.0.0
# Git binary searches through commits
# Create a patch file
git format-patch -1 HEAD
git apply patch-file.patch
# Archive a repository snapshot
git archive --format=zip HEAD > project.zipInterview Questions
Q1: What is the difference between git fetch and git pull?
Fetch downloads remote changes and updates remote-tracking branches without modifying your working directory or local branches. Pull does fetch plus merge (or rebase). Fetch is safer for reviewing changes before integrating them.
Q2: How do you see what changed in the last commit?
git show HEAD shows the commit message and full diff. git diff HEAD~1 HEAD shows only the diff. git log -1 --stat shows which files changed and how many lines.
Q3: What command shows who last modified each line of a file?
git blame filename shows the author, commit hash, and date for every line. Use git blame -L 10,20 filename to limit to specific line range.
Q4: How do you create a Git alias for a complex command?
git config --global alias.lg "log --oneline --graph --all --decorate" creates the alias git lg. Aliases are stored in ~/.gitconfig and can reference any valid git command with arguments.
Q5: How do you search for specific text across all commits in history?
git log --all -S "searchText" finds commits that added or removed the text (pickaxe search). git log --all --grep="message text" searches commit messages. git grep "text" $(git rev-list --all) searches file content across all commits.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Command Reference - Complete Command Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, resources, command, reference, git command reference - complete command guide
Related Git & GitHub Topics