Git & GitHub
Git & GitHub Command Reference Guide
Comprehensive developer reference featuring essential commands, workflow architectures, GitHub CLI shortcuts, and recovery mechanisms.
Total Commands: 0
| Category | Scope / Tool | Command | Syntax | Common Flags | Command Example | Description & Notes |
|---|---|---|---|---|---|---|
| 1. Setup | Git Core | Config | git config |
--global, --local, --list | git config --global user.name "Alex" |
Sets configuration options for user name, email, default editor, and line endings globally or locally. |
| 1. Setup | Git Core | Initialize Repository | git init |
-b <branch>, --bare | git init -b main |
Creates an empty Git repository or reinitializes an existing one in the current working directory. |
| 1. Setup | Git Core | Clone Repository | git clone |
--depth, --branch, --recursive | git clone --depth 1 <url> |
Clones a repository into a newly created directory. Use --depth 1 for shallow clones to save space. |
| 2. Basic Snapshot | Git Core | Status | git status |
-s, --short, --ignored | git status -s |
Displays working tree status, showing tracked, untracked, modified, and staged files. |
| 2. Basic Snapshot | Git Core | Stage Changes | git add |
., -A, -p, -u | git add -p |
Adds file contents to the index (staging area). Use -p for interactive patch staging of chunks. |
| 2. Basic Snapshot | Git Core | Commit | git commit |
-m, -am, --amend, --no-verify | git commit -m "feat: add user login" |
Records changes to the local repository. Use --amend to update or reword the last commit. |
| 2. Basic Snapshot | Git Core | Diff | git diff |
--staged, --stat, HEAD | git diff --staged |
Shows changes between commits, working tree, and staging area. Use --staged to inspect staged work. |
| 3. Branching | Git Core | Branch Management | git branch |
-a, -m, -d, -D | git branch -d feature/login |
Lists, creates, renames, or deletes branches. -d deletes safely; -D forces deletion. |
| 3. Branching | Git Core | Switch Branch | git switch |
-c, --detach | git switch -c feature/oauth |
Modern command to switch branches. Use -c to create and immediately switch to a new branch. |
| 3. Branching | Git Core | Merge | git merge |
--no-ff, --squash, --abort | git merge --no-ff feature/ui |
Joins branch histories together. --no-ff preserves explicit merge commit history. |
| 3. Branching | Advanced | Rebase | git rebase |
-i, --onto, --continue, --abort | git rebase -i HEAD~4 |
Reapplies commits on top of another base tip. -i enables interactive rebase for squashing/reordering. |
| 4. Syncing | Git Core | Remote Management | git remote |
-v, add, remove, set-url | git remote add origin <url> |
Manages tracked remote repositories. -v prints remote URL endpoints for fetch and push. |
| 4. Syncing | Git Core | Fetch | git fetch |
--all, --prune, --tags | git fetch origin --prune |
Downloads objects and refs from remote without merging into local working branch. --prune removes stale branches. |
| 4. Syncing | Git Core | Pull | git pull |
--rebase, --autostash, --ff-only | git pull --rebase origin main |
Fetches from remote and immediately integrates. Using --rebase prevents dirty merge commits. |
| 4. Syncing | Git Core | Push | git push |
-u, --force-with-lease, --tags | git push -u origin main |
Updates remote references along with associated objects. --force-with-lease is safer than --force. |
| 5. Utilities | Git Core | Stash | git stash |
push, pop, list, apply, drop | git stash push -u -m "WIP nav" |
Temporarily shelves uncommitted modified and untracked (-u) changes for a clean working tree. |
| 5. Utilities | Advanced | Cherry-Pick | git cherry-pick |
-x, -n, --continue, --abort | git cherry-pick <commit-sha> |
Applies the change introduced by existing specific commit(s) onto the current HEAD branch. |
| 6. Inspection | Git Core | Log History | git log |
--oneline, --graph, --all, -n | git log --oneline --graph --all |
Shows commit logs. --oneline --graph renders a clean visual ASCII tree of all branch nodes. |
| 6. Inspection | Git Core | Blame | git blame |
-L, -e, -w | git blame -L 10,25 index.js |
Annotates each line in a file with the author and commit SHA that last modified it. |
| 6. Inspection | Safety Net | Reflog | git reflog |
show, expire, delete | git reflog show HEAD |
Tracks every HEAD movement in local repository. Essential safety net to recover dropped commits or rebases. |
| 7. Undoing | Git Core | Restore File | git restore |
--staged, --source | git restore --staged app.js |
Restores working tree files or unstages changes without affecting local commit history. |
| 7. Undoing | Git Core | Reset | git reset |
--soft, --mixed, --hard | git reset --soft HEAD~1 |
Resets current HEAD to specified state. --soft preserves staging; --hard wipes all uncommitted work. |
| 7. Undoing | Git Core | Revert Commit | git revert |
--no-commit, -m | git revert <commit-sha> |
Creates a brand new commit that safely undoes changes from a previous specified commit. Safe for shared branches. |
| 8. GitHub CLI | gh CLI | Create Repo | gh repo create |
--public, --private, --source | gh repo create my-app --public |
Creates a new GitHub repository directly from terminal and wires up local remote automatically. |
| 8. GitHub CLI | gh CLI | Pull Request | gh pr create |
--title, --body, --web, --draft | gh pr create -t "Fix auth bug" |
Opens pull requests on GitHub. Use --web to preview PR UI directly in browser. |
| 8. GitHub CLI | gh CLI | PR Merge | gh pr merge |
--squash, --auto, --delete-branch | gh pr merge --squash -d |
Merges GitHub pull request from CLI. -d automatically deletes local and remote feature branches. |
Git Lifecycle Architecture
[ Working Directory ] →
git add → [ Staging Area ] → git commit → [ Local Repository ] → git push → [ Remote Repo (GitHub) ]
1. Working Tree
Untracked and modified files reside on your local disk before being marked for snapshotting.
2. Staging Area (Index)
The index acts as a prep area where specific changes are formatted into atomic commits.
3. Local Head (Repository)
Committed snapshots stored permanently in local .git object storage database.
4. Remote (Origin)
Shared remote central hub (GitHub) facilitating team collaboration, CI/CD pipelines, and PR reviews.
Core Terminology
GitHub Objects
Pro Tips & Time Savers
Interactive Rebase Squashing
Clean up messy local commit histories before submitting a pull request by squashing multiple work-in-progress commits into a concise feature commit.
git rebase -i HEAD~3
Replace 'pick' with 'squash' (or 's') in editor.
Recover Lost Commits with Reflog
ACCIDENTALLY ran
git reset --hard? Don't panic. Find your lost commit SHA in reflog and reset back to safety.
git reflog show HEAD
git reset --hard HEAD@{1}
Reflog keeps history even after hard resets.
Git Global Aliases
Speed up daily commands by configuring short global terminal aliases inside your global
.gitconfig file.
git config --global alias.co checkout
git config --global alias.st "status -s"
Professional Git Rules & Best Practices
Core Development Hygiene
- Make Atomic Commits: Keep each commit focused on a single logical change or bug fix.
- Never Force Push to Main: Avoid
git push --forceon shared branches; use--force-with-leaseif mandatory. - Pull with Rebase: Use
git pull --rebaseto maintain a linear git commit graph free of excess merge noise. - Maintain Clean .gitignore: Exclude environment secrets (
.env), binary dependencies, and OS auto-generated files early.
Conventional Commit Standard
Structure commit messages systematically to automate semantic versioning and changelogs:
feat(auth): implement OAuth2 token refresh flow
fix(ui): correct mobile table card breakpoint styling
docs(readme): add GitHub CLI deployment instructions
fix(ui): correct mobile table card breakpoint styling
docs(readme): add GitHub CLI deployment instructions