Appendix H — Git Command Reference

This appendix provides a comprehensive reference for Git commands and workflows; the concepts are introduced in Chapter 19.

H.1 Setup and Configuration

Table H.1: Git configuration commands
Command Description Example
git config --global user.name Set username git config --global user.name "Your Name"
git config --global user.email Set email git config --global user.email "you@example.com"
git config --global init.defaultBranch Set default branch git config --global init.defaultBranch main
git config --global core.editor Set default editor git config --global core.editor "vim"
git config --list Show all settings git config --list
git config --global --list Show global settings git config --global --list
git config --local --list Show local settings git config --local --list
git config --global alias.st Create alias git config --global alias.st status
git --version Show installed Git version git --version
git help CMD Manual page for a command git help commit

H.2 Repository Operations

Table H.2: Repository operations
Command Description Example
git init Initialize new repository git init
git init --bare Initialize bare repository git init --bare
git clone Clone remote repository git clone URL
git clone --depth 1 Shallow clone (latest only) git clone --depth 1 URL
git clone -b branch Clone specific branch git clone -b develop URL

H.3 Checking Status

Table H.3: Status and history commands
Command Description Example
git status Show working tree status git status
git status -s Short format status git status -s
git log View commit history git log
git log --oneline Condensed history git log --oneline
git log --graph Show branch graph git log --oneline --graph
git log -n 5 Show last n commits git log -n 5
git log --author Filter by author git log --author="name"
git log --since Filter by date git log --since="2024-01-01"
git log -p Show patches (diffs) git log -p
git log --stat Show change statistics git log --stat
git log -- file History for specific file git log -- path/to/file
git show Show commit details git show abc1234
git blame Show who changed each line git blame file.txt

H.4 Making Changes

Table H.4: Making changes
Command Description Example
git add file Stage specific file git add file.txt
git add . Stage all changes git add .
git add -A Stage all (including deletions) git add -A
git add -p Interactive staging git add -p
git commit -m Commit with message git commit -m "Add feature"
git commit -a Stage and commit tracked files git commit -am "Update"
git commit --amend Modify last commit git commit --amend
git commit --amend -m Change last commit message git commit --amend -m "New message"
git restore file Discard unstaged changes to a file git restore file.txt
git restore --staged file Unstage file (keep edits) git restore --staged file.txt
git checkout -- file Discard changes (older syntax, still common) git checkout -- file.txt
git reset HEAD file Unstage file (older syntax) git reset HEAD file.txt
git rm file Remove file from Git git rm file.txt
git rm --cached file Untrack file (keep locally) git rm --cached secret.txt
git mv old new Rename/move file git mv old.txt new.txt

H.5 Viewing Differences

Table H.5: Viewing differences
Command Description Example
git diff Show unstaged changes git diff
git diff --staged Show staged changes git diff --staged
git diff --cached Same as –staged git diff --cached
git diff HEAD Show all uncommitted changes git diff HEAD
git diff commit1 commit2 Compare commits git diff abc123 def456
git diff branch1 branch2 Compare branches git diff main feature
git diff --stat Summary of changes git diff --stat
git diff --name-only Changed files only git diff --name-only

H.6 Branching

Table H.6: Branch commands
Command Description Example
git branch List local branches git branch
git branch -a List all branches git branch -a
git branch -r List remote branches git branch -r
git branch name Create new branch git branch feature-x
git switch name Switch to branch git switch feature-x
git switch -c name Create and switch git switch -c feature-x
git checkout name Switch to branch (older syntax, still common) git checkout feature-x
git checkout -b name Create and switch (older syntax, still common) git checkout -b feature-x
git branch -d name Delete merged branch git branch -d feature-x
git branch -D name Force delete branch git branch -D feature-x
git branch -m old new Rename branch git branch -m old-name new-name

H.7 Merging

Table H.7: Merge commands
Command Description Example
git merge branch Merge branch into current git merge feature-x
git merge --no-ff Create merge commit always git merge --no-ff feature-x
git merge --squash Squash commits then merge git merge --squash feature-x
git merge --abort Abort merge in progress git merge --abort
git mergetool Launch merge conflict tool git mergetool
Note

git switch and git restore were introduced in Git 2.23 (2019) to split the overloaded git checkout into two clearly named commands. git checkout still works and appears in most older tutorials and answers online; treat the two forms as interchangeable.

H.8 Remote Operations

Table H.8: Remote operations
Command Description Example
git remote List remotes git remote
git remote -v List remotes with URLs git remote -v
git remote add name URL Add remote git remote add origin URL
git remote remove name Remove remote git remote remove origin
git remote rename old new Rename remote git remote rename origin upstream
git remote set-url name URL Change remote URL git remote set-url origin NEW_URL
git fetch Download from remote git fetch origin
git fetch --all Fetch all remotes git fetch --all
git pull Fetch and merge git pull origin main
git pull --rebase Fetch and rebase git pull --rebase origin main
git push Upload to remote git push origin main
git push -u Push and set upstream git push -u origin main
git push --force Force push (caution!) git push --force origin branch
git push --tags Push all tags git push --tags
git push origin --delete Delete remote branch git push origin --delete feature-x

H.9 Authentication with GitHub

GitHub no longer accepts account passwords for Git operations (since 2021). Use one of the following:

Table H.9: GitHub authentication methods
Method Steps
SSH key ssh-keygen -t ed25519 -C "you@uoregon.edu"; copy ~/.ssh/id_ed25519.pub; GitHub → Settings → SSH and GPG keys → New SSH key; test with ssh -T git@github.com; use git@github.com:user/repo.git URLs
Personal access token (HTTPS) GitHub → Settings → Developer settings → Personal access tokens → Fine-grained tokens; grant Contents: Read and write; paste the token when Git asks for a password; cache with git config --global credential.helper
GitHub CLI Install gh, run gh auth login, follow the prompts

H.10 Stashing

Table H.10: Stash commands
Command Description Example
git stash Stash changes git stash
git stash save "message" Stash with message git stash save "WIP feature"
git stash list List stashes git stash list
git stash show Show stash contents git stash show -p stash@{0}
git stash pop Apply and remove stash git stash pop
git stash apply Apply stash (keep it) git stash apply stash@{0}
git stash drop Delete stash git stash drop stash@{0}
git stash clear Delete all stashes git stash clear

H.11 Tags

Table H.11: Tag commands
Command Description Example
git tag List tags git tag
git tag name Create lightweight tag git tag v1.0.0
git tag -a name -m Create annotated tag git tag -a v1.0.0 -m "Release"
git tag -d name Delete local tag git tag -d v1.0.0
git push origin name Push tag to remote git push origin v1.0.0
git push origin --delete tag Delete remote tag git push origin --delete v1.0.0
git checkout tag Checkout tag git checkout v1.0.0

H.12 Undoing Changes

Table H.12: Undo commands
Command Description Example
git reset --soft HEAD~1 Undo commit, keep staged git reset --soft HEAD~1
git reset HEAD~1 Undo commit, unstage git reset HEAD~1
git reset --hard HEAD~1 Undo commit, discard changes git reset --hard HEAD~1
git reset --hard origin/main Reset to remote state git reset --hard origin/main
git revert commit Create undo commit git revert abc1234
git restore --source commit file Restore file from commit git restore --source abc123 file.txt
git checkout commit -- file Restore file from commit (older syntax) git checkout abc123 -- file.txt
git reflog Show reference log git reflog

H.13 Advanced Operations

Table H.13: Advanced operations
Command Description Example
git rebase branch Rebase onto branch git rebase main
git rebase -i HEAD~n Interactive rebase git rebase -i HEAD~3
git rebase --abort Abort rebase git rebase --abort
git rebase --continue Continue after resolving git rebase --continue
git cherry-pick commit Apply specific commit git cherry-pick abc1234
git bisect start Start binary search git bisect start
git bisect good/bad Mark commit git bisect good
git clean -n Preview untracked removal git clean -n
git clean -f Remove untracked files git clean -f
git clean -fd Remove untracked files/dirs git clean -fd
git archive Create archive of tree git archive --format=zip HEAD > archive.zip

H.14 Useful Aliases

Add these to your ~/.gitconfig for convenience:

[alias]
    st = status
    co = checkout
    br = branch
    ci = commit
    lg = log --oneline --graph --decorate
    last = log -1 HEAD
    unstage = reset HEAD --
    visual = !gitk
    aliases = config --get-regexp alias

H.15 .gitignore Patterns

Table H.14: .gitignore patterns
Pattern Description
*.log All .log files
!important.log Exception (don’t ignore)
/TODO Only root TODO
build/ Directory named build
doc/*.txt .txt in doc only
doc/**/*.pdf .pdf in doc and subdirs
**/logs logs directory anywhere

Common .gitignore Entries

# OS files
.DS_Store
Thumbs.db

# Editor files
*.swp
*~
.vscode/
.idea/

# Language specific
__pycache__/
*.pyc
.venv/
venv/
.ipynb_checkpoints/
.Rhistory
.RData
.Rproj.user/
node_modules/

# Large data files (keep them out of Git)
*.fastq
*.fastq.gz
*.bam
*.vcf
data/raw/

# Sensitive data
.env
*.pem
credentials.json

# Build artifacts
build/
dist/
*.egg-info/