Git & GitHub cheat sheet

Git and GitHub manage source code and collaboration workflows.

On this page

Table of contents

Use this page for fast recall. Use Full documentation when you want the complete end-to-end path.

Quick workflow

A simple 5-step flow you can follow when using Git & GitHub in real work.

1) Setup

Install/run the tool and confirm version. Create a minimal config.

2) Small change

Do one small action end-to-end to prove the workflow.

3) Validate

Check output, logs, and status. Catch mistakes early.

4) Automate

Convert it into a repeatable script or pipeline step.

5) Productionize

Add safety: secrets, rollback, observability, and docs.

Workflows you will actually reuse

These are practical sequences you can copy into your own checklist or runbook.

Clean PR workflow (team-friendly)

Goal: Ship changes with reviewable history and minimal conflicts.

- Create a feature branch from `main`.

- Commit small, logical changes with clear messages.

- Rebase onto latest `main` before opening PR (or before merge).

- Request review, address feedback, keep PR focused.

- Merge using your team’s strategy (merge commit, squash, or rebase).

Key Concepts

- Commits

- Branches

- Pull requests

Learning path (high-level):

- Basic git flow

- Branching strategy

- Code review process

Quick Start

Initialize repository

Command

git init

Add and commit changes

Command

git add . && git commit -m 'init'

Push to remote

Command

git push origin main

Common Commands

Short descriptions and practical intent. Search, filter, copy, and reuse.

Showing 25

Daily
git status

Show changed files and branch status.

Daily
git diff

See unstaged changes.

Daily
git diff --staged

See staged changes.

Daily
git add .

Stage all changed files.

Daily
git add -p

Stage changes interactively (best practice).

Daily
git commit -m "msg"

Create a commit from staged changes.

History
git commit --amend

Edit last commit (before pushing).

History
git log --oneline --decorate --graph -n 20

Readable recent history.

History
git show <sha>

Inspect a commit.

Branches
git branch -vv

Show local branches and upstream tracking.

Branches
git checkout -b feature/x

Create and switch to a new branch.

Branches
git switch -c feature/x

Modern alternative to checkout (create branch).

Branches
git switch -

Toggle to previous branch.

Remotes
git fetch --all --prune

Update refs and delete removed remote branches.

Remotes
git pull --rebase

Update branch with a clean history.

Remotes
git push -u origin HEAD

Push current branch and set upstream.

Remotes
git push --force-with-lease

Safer force push (protects others’ work).

Stash
git stash push -m "wip"

Temporarily save changes.

Stash
git stash pop

Re-apply stashed changes.

History
git rebase -i HEAD~3

Interactive rebase to squash/clean commits.

Recovery
git reset --soft HEAD~1

Undo last commit but keep changes staged.

Recovery
git reset --hard HEAD

Discard local changes (danger).

Recovery
git revert <sha>

Create a new commit that undoes a commit (safe for shared branches).

Releases
git tag -a v1.0.0 -m "release"

Create an annotated tag.

Releases
git push origin --tags

Push tags to remote.

Copyable snippets

Small blocks you can drop into your terminal, config, or runbook.

Create a feature branch and push upstream

bash

git switch -c feature/my-change
# work...
git add -p
git commit -m "feat: add X"
git push -u origin HEAD

Squash last 3 commits into 1

bash

git rebase -i HEAD~3
# mark commits as squash, save/exit

Troubleshooting checklist

When things break, follow this order to stay calm and move fast.

- If you have conflicts: understand the conflicting files, fix markers, then `git add` and continue rebase/merge.

- If you pushed secrets: rotate credentials, remove from history (BFG/filter-repo), and force push carefully.

- If your branch is behind: `git fetch --all` then rebase/merge from `origin/main`.

Pitfalls

The common mistakes that slow people down when using Git & GitHub.

- Force-pushing to shared branches without communicating.

- Not pulling/rebasing before pushing, causing messy histories.

- Committing secrets (use `.gitignore` and secret scanning).

Mini lab (practice)

Do these tasks in order. You will feel the tool instead of just reading about it.

- Create a feature branch, make 2 commits, then squash them into 1 clean commit.

- Resolve a merge conflict intentionally (edit the conflict markers, then commit).

- Tag a release and push the tag to remote.

Interview prompts

Use these to test if you truly understand the basics (and can explain them clearly).

- When do you use rebase vs merge, and why?

- How do you recover from a bad commit pushed to remote?

- Describe a branching strategy you’ve used and its tradeoffs.

Official Docs

https://git-scm.com/doc