| name | working-with-git |
| description | Git version control cheatsheet - viewing history, making commits, diffs, and descriptions |
Working with Git
Quick reference for common Git operations used in development workflows.
Detecting Git
Check if the project uses Git:
test -d .git && echo "git" || echo "not git"
Viewing the Log
Show recent commits:
git log --oneline -10
Show log with graph:
git log --oneline --graph --all -10
Show specific commit:
git log -1 <commit-sha>
Find commits by message:
git log --oneline --grep="keyword"
Viewing Diffs
See uncommitted changes:
git diff
See staged changes:
git diff --cached
Diff between commits:
git diff <base-sha>..<head-sha>
Diff stats:
git diff --stat <base-sha>..<head-sha>
Show what changed in a commit:
git show <commit-sha>
Getting Commit References
Current commit SHA:
git rev-parse HEAD
Parent commit SHA:
git rev-parse HEAD~1
Branch's remote tracking commit:
git rev-parse origin/main
Relative references:
HEAD- current commitHEAD~1orHEAD^- parent commitHEAD~2- grandparent commitorigin/main- remote branch tip
Making Commits
Stage and commit:
git add <files>
git commit -m "commit message"
Commit all tracked changes:
git commit -am "commit message"
Stage specific hunks interactively:
git add -p
Modifying Commit Descriptions
Amend last commit message:
git commit --amend -m "new message"
Amend last commit (open editor):
git commit --amend
Change older commit messages (interactive rebase):
git rebase -i HEAD~3
(Then mark commits with reword in the editor)
Branch Information
Current branch:
git branch --show-current
Check if branch tracks remote:
git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null
Quick Status
See working tree status:
git status
Short status:
git status -s
Common Patterns
Get range for code review:
BASE_SHA=$(git rev-parse HEAD~1)
HEAD_SHA=$(git rev-parse HEAD)
echo "Reviewing: $BASE_SHA..$HEAD_SHA"
Compare against main:
git diff origin/main..HEAD
See files changed:
git diff --name-only <base>..<head>