Back to All Cheatsheet Libraries
cheatsheets
Core Git commands, GitHub CLI shortcuts, workflow architectures, and recovery mechanisms for everyday version control.
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
Git & GitHub Terminology
The vocabulary behind the commands above — the words that show up in error messages, PR reviews, and docs. Search by term or category.
Showing 0 terms
| Term | Category | What it means |
|---|---|---|
| Repository (repo) | Core | The project's full history and all its files, tracked by Git. Lives in a hidden .git folder at the project root. |
| Working tree | Core | The actual files you see and edit on disk right now — one checked-out snapshot of the repository. |
| Staging area (index) | Core | A holding area between the working tree and a commit. git add puts changes here; git commit records what's staged. |
| Commit | Core | A saved snapshot of the staged changes, with an author, message, timestamp, and a pointer to its parent commit(s). Identified by a hash. |
| Hash / SHA | Core | The unique ID of a commit (or any Git object) — a 40-char SHA-1 or 64-char SHA-256 string. You can abbreviate it to the first 7+ characters. |
| HEAD | Core | A pointer to the commit you currently have checked out — usually the tip of your current branch. |
| Detached HEAD | Core | The state when HEAD points directly at a commit instead of a branch. New commits here belong to no branch and can be lost unless you make a branch. |
| Branch | Branching | A movable pointer to a commit — a named line of development. Creating one is cheap: it's just a 41-byte file naming a commit. |
| main / master | Branching | The conventional name of the default branch. New repos default to main; older ones often use master. |
| Checkout / switch | Branching | Move HEAD to a branch or commit, updating the working tree to match. git switch is the newer, branch-focused command; git checkout also does other jobs. |
| Merge | Branching | Combine another branch's history into your current one, creating a merge commit with two parents (unless it can fast-forward). |
| Fast-forward | Branching | A merge with no divergence: the branch pointer just slides forward to the other branch's tip, with no merge commit created. |
| Merge conflict | Branching | When two branches change the same lines and Git can't auto-combine them. You edit the marked files, then git add and commit to resolve. |
| Rebase | Branching | Replay your commits on top of another branch to get a linear history, rewriting those commits with new hashes. Never rebase commits others have pulled. |
| Squash | Branching | Combine several commits into one, usually via interactive rebase or a squash-merge, to tidy history before merging. |
| Cherry-pick | Branching | Apply a single commit from one branch onto another, without merging the whole branch. |
| Stash | Branching | Shelve uncommitted changes so you have a clean working tree, then reapply them later with git stash pop. |
| Tag | Branching | A fixed, named pointer to a specific commit, usually marking a release (e.g. v1.2.0). Unlike a branch, it doesn't move. |
| Remote | Remote | A named reference to another copy of the repository, typically hosted (e.g. on GitHub). git remote -v lists them. |
| Origin | Remote | The default name for the remote you cloned from. It's a convention, not a special keyword. |
| Upstream | Remote | The remote/branch your local branch tracks and pulls from. Also the original repo a fork was made from. |
| Tracking branch | Remote | A local branch linked to a remote branch, so plain git pull and git push know where to go. |
| Clone | Remote | Download a full copy of a remote repository, including all history, and set up origin automatically. |
| Fetch | Remote | Download new commits and refs from a remote without changing your working tree or current branch. |
| Pull | Remote | Fetch, then merge (or rebase) the remote changes into your current branch. Effectively fetch + merge. |
| Push | Remote | Upload your local commits to a remote branch. |
| Fork | GitHub | Your own server-side copy of someone else's GitHub repo, so you can propose changes via a pull request without write access to the original. |
| Pull request (PR) | GitHub | A GitHub proposal to merge one branch into another, with review, discussion, checks, and a merge button. Called a merge request on GitLab. |
| Draft PR | GitHub | A pull request marked not-ready-for-review, so reviewers and merge aren't triggered yet. |
| Issue | GitHub | A GitHub tracked item for a bug, task, or feature request, with labels, assignees, and discussion. |
| GitHub Actions | GitHub | GitHub's built-in CI/CD. Workflows defined in .github/workflows/*.yml run on events like push or pull request. |
| Workflow | GitHub | A single automated process in GitHub Actions, made of jobs and steps, triggered by events. |
| Protected branch | GitHub | A branch with rules — required reviews, passing checks, no force-push — enforced before anything can merge into it. |
| CODEOWNERS | GitHub | A file listing who owns which paths, so the right people are auto-requested for review on PRs touching those files. |
| Release | GitHub | A GitHub package built on a tag, with release notes and downloadable assets. |
| Gist | GitHub | A lightweight, shareable snippet or single-file repo hosted on GitHub. |
| reset | Undo & history | Move the current branch to another commit. --soft keeps changes staged, --mixed (default) unstages them, --hard discards them. |
| revert | Undo & history | Create a new commit that undoes an earlier one, keeping history intact — the safe way to undo a commit others have. |
| amend | Undo & history | Replace the most recent commit with a new one (e.g. to fix its message or add a file). Rewrites that commit's hash. |
| reflog | Undo & history | A local log of everywhere HEAD has been. Your safety net for recovering commits after a bad reset or rebase. |
| HEAD~ / HEAD^ | Undo & history | Ways to reference ancestors: HEAD~2 is two commits back on the first-parent line; HEAD^2 is the second parent of a merge. |
| bisect | Undo & history | A binary search across commits to find the one that introduced a bug. |
| blame | Undo & history | Show who last changed each line of a file, and in which commit. |
| .gitignore | Internals | A file listing patterns Git should not track (build output, secrets, dependencies like node_modules). |
| Object (blob / tree / commit) | Internals | Git's storage units: a blob is file contents, a tree is a directory listing, and a commit points to a tree plus parent commit(s). |
| Submodule | Internals | Another Git repository embedded inside yours at a fixed commit, tracked as a pointer rather than copied files. |
| Worktree | Internals | An extra working directory attached to the same repo, so you can check out a second branch without a separate clone. |
| Shallow clone | Internals | A clone with truncated history (--depth) to save time and space when you don't need the full log. |