Back to All Cheatsheet Libraries cheatsheets

Git & GitHub

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

HEAD SHA-1 / SHA-256 Origin Upstream Detached HEAD

GitHub Objects

Pull Request (PR) Fork GitHub Actions Issue Release Tag

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 --force on shared branches; use --force-with-lease if mandatory.
  • Pull with Rebase: Use git pull --rebase to 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

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) CoreThe project's full history and all its files, tracked by Git. Lives in a hidden .git folder at the project root.
Working tree CoreThe actual files you see and edit on disk right now — one checked-out snapshot of the repository.
Staging area (index) CoreA holding area between the working tree and a commit. git add puts changes here; git commit records what's staged.
Commit CoreA 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 CoreThe 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 CoreA pointer to the commit you currently have checked out — usually the tip of your current branch.
Detached HEAD CoreThe 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 BranchingA 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 BranchingThe conventional name of the default branch. New repos default to main; older ones often use master.
Checkout / switch BranchingMove 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 BranchingCombine another branch's history into your current one, creating a merge commit with two parents (unless it can fast-forward).
Fast-forward BranchingA merge with no divergence: the branch pointer just slides forward to the other branch's tip, with no merge commit created.
Merge conflict BranchingWhen 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 BranchingReplay 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 BranchingCombine several commits into one, usually via interactive rebase or a squash-merge, to tidy history before merging.
Cherry-pick BranchingApply a single commit from one branch onto another, without merging the whole branch.
Stash BranchingShelve uncommitted changes so you have a clean working tree, then reapply them later with git stash pop.
Tag BranchingA fixed, named pointer to a specific commit, usually marking a release (e.g. v1.2.0). Unlike a branch, it doesn't move.
Remote RemoteA named reference to another copy of the repository, typically hosted (e.g. on GitHub). git remote -v lists them.
Origin RemoteThe default name for the remote you cloned from. It's a convention, not a special keyword.
Upstream RemoteThe remote/branch your local branch tracks and pulls from. Also the original repo a fork was made from.
Tracking branch RemoteA local branch linked to a remote branch, so plain git pull and git push know where to go.
Clone RemoteDownload a full copy of a remote repository, including all history, and set up origin automatically.
Fetch RemoteDownload new commits and refs from a remote without changing your working tree or current branch.
Pull RemoteFetch, then merge (or rebase) the remote changes into your current branch. Effectively fetch + merge.
Push RemoteUpload your local commits to a remote branch.
Fork GitHubYour 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) GitHubA GitHub proposal to merge one branch into another, with review, discussion, checks, and a merge button. Called a merge request on GitLab.
Draft PR GitHubA pull request marked not-ready-for-review, so reviewers and merge aren't triggered yet.
Issue GitHubA GitHub tracked item for a bug, task, or feature request, with labels, assignees, and discussion.
GitHub Actions GitHubGitHub's built-in CI/CD. Workflows defined in .github/workflows/*.yml run on events like push or pull request.
Workflow GitHubA single automated process in GitHub Actions, made of jobs and steps, triggered by events.
Protected branch GitHubA branch with rules — required reviews, passing checks, no force-push — enforced before anything can merge into it.
CODEOWNERS GitHubA file listing who owns which paths, so the right people are auto-requested for review on PRs touching those files.
Release GitHubA GitHub package built on a tag, with release notes and downloadable assets.
Gist GitHubA lightweight, shareable snippet or single-file repo hosted on GitHub.
reset Undo & historyMove the current branch to another commit. --soft keeps changes staged, --mixed (default) unstages them, --hard discards them.
revert Undo & historyCreate a new commit that undoes an earlier one, keeping history intact — the safe way to undo a commit others have.
amend Undo & historyReplace 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 & historyA local log of everywhere HEAD has been. Your safety net for recovering commits after a bad reset or rebase.
HEAD~ / HEAD^ Undo & historyWays 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 & historyA binary search across commits to find the one that introduced a bug.
blame Undo & historyShow who last changed each line of a file, and in which commit.
.gitignore InternalsA file listing patterns Git should not track (build output, secrets, dependencies like node_modules).
Object (blob / tree / commit) InternalsGit'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 InternalsAnother Git repository embedded inside yours at a fixed commit, tracked as a pointer rather than copied files.
Worktree InternalsAn extra working directory attached to the same repo, so you can check out a second branch without a separate clone.
Shallow clone InternalsA clone with truncated history (--depth) to save time and space when you don't need the full log.