| name | git |
| description | Git and GitHub CLI mastery for version control operations. Use proactively for commits, branches,
PRs, rebases, conflict resolution, and git history management. Covers conventional commits,
branching strategies, and GitHub workflows.
Examples:
- user: "Commit these changes" → stage files, create conventional commit
- user: "Create a PR" → push branch, use gh pr create with proper template
- user: "Rebase on main" → fetch, rebase, handle conflicts
- user: "Fix merge conflict" → identify conflicts, resolve, complete merge
- user: "Squash my commits" → interactive rebase guidance
- user: "What's the git history?" → git log with appropriate flags |
Comprehensive guide for git operations, GitHub CLI usage, commit practices, and version control workflows.
Git CLI Essentials
Status & Information
git status
git status -sb
git diff
git diff --staged
git diff HEAD~3
git diff main...HEAD
git log --oneline -20
git log --graph --oneline
git log --stat
git log -p path/to/file
git log --author="name"
git blame path/to/file
git show <commit>
git show <commit>:path/file
Staging & Committing
git add <file>
git add .
git add -p
git add -u
git restore --staged <file>
git reset HEAD <file>
git commit -m "message"
git commit
git commit -am "message"
git commit --amend
git commit --amend --no-edit
Branches
git branch
git branch -a
git branch -vv
git branch <name>
git switch <name>
git switch -c <name>
git checkout -b <name>
git branch -d <name>
git branch -D <name>
git branch -m <old> <new>
Remote Operations
git fetch
git fetch origin
git pull
git pull --rebase
git push
git push -u origin <branch>
git push --force-with-lease
git push --force
git remote -v
git remote add <name> <url>
git remote set-url origin <url>
Merging & Rebasing
git merge <branch>
git merge --no-ff <branch>
git merge --abort
git rebase main
git rebase -i HEAD~3
git rebase --abort
git rebase --continue
git cherry-pick <commit>
git cherry-pick --abort
Undoing Changes
git restore <file>
git restore .
git checkout -- <file>
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git revert <commit>
git revert HEAD
git reflog
git reset --hard <reflog-ref>
Stashing
git stash
git stash -m "message"
git stash list
git stash pop
git stash apply
git stash drop
git stash clear
GitHub CLI (gh)
Authentication
gh auth login
gh auth status
gh auth logout
Pull Requests
gh pr create
gh pr create --title "..." --body "..."
gh pr create --draft
gh pr create --base main
gh pr list
gh pr list --state all
gh pr view
gh pr view <number>
gh pr view --web
gh pr review
gh pr review --approve
gh pr review --request-changes -b "feedback"
gh pr merge
gh pr merge --squash
gh pr merge --rebase
gh pr merge --delete-branch
gh pr checkout <number>
Issues
gh issue create
gh issue create --title "..." --body "..."
gh issue list
gh issue view <number>
gh issue close <number>
gh issue reopen <number>
gh issue comment <number> -b "..."
Repository
gh repo view
gh repo clone <owner/repo>
gh repo fork
gh repo create
Workflows & Actions
gh run list
gh run view <id>
gh run watch <id>
gh run rerun <id>
gh workflow list
gh workflow run <name>
API Access
gh api repos/{owner}/{repo}/pulls
gh api repos/{owner}/{repo}/issues
gh api repos/{owner}/{repo}/pulls/<n>/comments
Conventional Commits
Format
<type>(<scope>): <description>
[optional body]
[optional footer(s)]
Types
| Type | Description |
|---|
feat | New feature |
fix | Bug fix |
docs | Documentation only |
style | Formatting, no code change |
refactor | Code change, no feature/fix |
perf | Performance improvement |
test | Adding/fixing tests |
chore | Maintenance, tooling |
ci | CI/CD changes |
build | Build system changes |
revert | Revert previous commit |
Examples
git commit -m "feat(auth): add JWT refresh token support"
git commit -m "fix(api): handle null response in user endpoint"
git commit -m "feat(api)!: remove deprecated v1 endpoints
BREAKING CHANGE: v1 API endpoints are no longer available"
git commit -m "fix(cart): prevent negative quantities
Closes #123"
git commit
Scopes (Project-Specific)
Common scopes:
api, auth, ui, db, config
deps, ci, docs, test
- Feature names:
cart, checkout, profile
Branching Strategies
Trunk-Based Development (Recommended)
main ─────●─────●─────●─────●─────●─────
\ / \ /
●─● ●─●
(short-lived feature branches)
Rules:
- Main is always deployable
- Feature branches SHOULD live < 2 days
- MUST merge via PR with CI checks
- MUST delete branches after merge
Workflow:
git switch -c feat/add-login
git push -u origin feat/add-login
gh pr create --base main
gh pr merge --squash --delete-branch
GitHub Flow
main ─────●─────●─────●─────●─────●─────
\ /
●─────●
(feature branch)
Workflow:
- Create branch from main
- Add commits
- Open PR
- Review & discuss
- Merge to main
- Deploy
Git Flow (Complex Projects)
main ─────●───────────────●─────────
│ │
develop ─────●─────●─────●───●─────●───
\ / \ /
feature ●─● \ /
release─●
Branches:
main - production releases
develop - integration branch
feature/* - new features
release/* - release prep
hotfix/* - production fixes
Common Workflows
Feature Development
git switch main
git pull
git switch -c feat/user-profile
git add -p
git commit -m "feat(profile): add avatar upload"
git commit -m "feat(profile): add bio field"
git fetch origin
git rebase origin/main
git push -u origin feat/user-profile
gh pr create --title "feat(profile): add user profile page" --body "$(cat <<'EOF'
## Summary
- Add avatar upload with S3
- Add bio field with markdown support
## Testing
- [ ] Upload various image formats
- [ ] Test bio with special characters
EOF
)"
gh pr merge --squash --delete-branch
git switch main
git pull
Hotfix
git switch main
git pull
git switch -c hotfix/fix-login-crash
git commit -m "fix(auth): handle null session token"
git push -u origin hotfix/fix-login-crash
gh pr create --title "fix(auth): critical login crash" --body "..."
gh pr merge --merge --delete-branch
Conflict Resolution
git status
git add <resolved-file>
git merge --continue
git rebase --continue
git merge --abort
git rebase --abort
Squashing Commits
git rebase -i HEAD~5
gh pr merge --squash
Safety Practices
MUST NOT Do (Without Explicit Permission)
git push --force origin main
git push --force origin develop
git reset --hard origin/main
git push origin --delete <shared-branch>
MUST Ask Before
git push --force-with-lease
git rebase
git reset --hard
git clean -fd
Safe Practices
git push --force-with-lease origin feat/my-branch
git status
git log --oneline -5
git diff
git stash
git switch other-branch
git switch -
git stash pop
git reflog
git reset --hard HEAD@{2}
Commit Message Best Practices
MUST
- Use imperative mood: "add feature" not "added feature"
- Keep subject line < 72 characters
- Capitalize first letter of subject
- No period at end of subject
- Separate subject from body with blank line
- Explain "what" and "why" in body, not "how"
- Reference issues: "Closes #123" or "Fixes #456"
MUST NOT
- Use vague messages: "fix bug", "update code", "WIP"
- Combine unrelated changes in one commit
- Include generated files in commits
- Commit secrets, credentials, or .env files
Template
<type>(<scope>): <what changed>
<why this change was needed>
<any additional context>
<footer: issues, breaking changes>
Git Configuration
Recommended Settings
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"
git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global push.default current
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --decorate"
Useful Aliases
[alias]
s = status -sb
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
last = log -1 HEAD --stat
undo = reset --soft HEAD~1
amend = commit --amend --no-edit
recent = branch --sort=-committerdate --format='%(committerdate:relative)%09%(refname:short)'
Quick Reference
Daily Workflow
git fetch && git status
git switch main && git pull
git switch -c feat/new-thing
git add -p && git commit -m "..."
git fetch origin
git rebase origin/main
git push -u origin HEAD
gh pr create
Emergency Commands
git reset --soft HEAD~1
git restore . && git clean -fd
git reflog
git checkout -b recovered-branch <sha>
git merge --abort
git rebase --abort
git cherry-pick --abort