Docsity

Git & GitHub Cheat Sheet – Complete Quick Reference (2026), Cheat Sheet of Programming Methodologies

Toggle sidebar
bg1
DEVELOPER QUICK REFERENCE • GIT / GITHUB
PRINT DOUBLE-SIDED • Replace example names/URLs with your own
GIT & GITHUB CHEAT SHEET
Commands, everyday workflow, commits, branches, merging, and conflict resolution | FRONT
1. Git vs GitHub
Git GitHub
Local version-control system Cloud platform for hosting Git
repositories
Tracks file history; works offline Collaboration, code review, issues, CI/CD
Use Git to commit and branch Use GitHub to share and review changes
Think: Git = history of your code; GitHub = shared home for that history.
2. First-time setup
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
git config --list
git config user.name
3. Create or clone a repo
git init -b main
git clone https://github.com/user/repo.git
git clone -b branch-name <url>
git clone --depth 1 <url> # shallow clone
4. The core workflow
Stage Command Purpose
Working staging→git add <file> Select changes
Staging local→git commit -m "msg" Save snapshot
Local remote→git push Upload commits
Remote local/work→git pull Fetch + merge
Remote local only→git fetch Download, do not merge
git status -sb # status, short
git diff # unstaged changes
git diff --staged # staged changes
git log --oneline --graph --all
git show <commit> # inspect commit
git blame <file> # line history
5. Stage & commit
git add <file> # specific file
git add . # all current changes
git add -p # choose hunks
git restore --staged <file> # unstage
git commit -m "feat: add login"
git commit -am "fix: typo" # tracked files only
git commit --amend --no-edit
Good commit messages
Use type(scope): short summary
feat = feature • fix = bug • docs = docs
refactor • test • chore • style
6. Branches
git branch # local list
git branch -a # local + remote
git switch -c feature/login # create + switch
git switch main
git branch -m new-name # rename
git branch -d feature/login # safe delete
git branch -D feature/login # force delete
git push origin --delete feature/login
Common strategies: GitHub Flow (main + short-lived feature branches); Git Flow
(main/develop/release/hotfix); trunk-based (very short-lived branches).
7. Merge vs rebase
MERGE REBASE
History Preserves branch graph Replays commits; linear
history
Merge commit Sometimes No extra merge commit
Shared branch Usually safe Avoid rewriting shared
history
Use for Integrating work Cleaning local branch history
git merge feature/login
git merge --no-ff feature/login
git merge --abort
git rebase main
git rebase -i HEAD~3 # reorder/squash/edit
git rebase --continue
git rebase --abort
git cherry-pick <commit>
Golden rule
Do not rebase public/shared branches. Merge them or coordinate history changes
with the team.
8. Resolve a merge conflict
1. Run git status and open conflicted files.
<<<<<<< HEAD
current-branch version
=======
incoming-branch version
>>>>>>> feature/login
2. Edit the file; remove markers and keep the desired result.
git add <resolved-file>
git commit # finish merge
git merge --abort # cancel instead
git mergetool # visual tool
pf2

Partial preview of the text

Download Git & GitHub Cheat Sheet – Complete Quick Reference (2026) and more Cheat Sheet Programming Methodologies in PDF only on Docsity!

DEVELOPER QUICK REFERENCE • GIT / GITHUB

PRINT DOUBLE-SIDED • Replace example names/URLs with your own

GIT & GITHUB CHEAT SHEET

Commands, everyday workflow, commits, branches, merging, and conflict resolution | FRONT

1. Git vs GitHub

Git GitHub

Local version-control system Cloud platform for hosting Git

repositories

Tracks file history; works offline Collaboration, code review, issues, CI/CD

Use Git to commit and branch Use GitHub to share and review changes

Think: Git = history of your code; GitHub = shared home for that history.

2. First-time setup

git config --global user.name "Your Name"

git config --global user.email "[email protected]"

git config --global init.defaultBranch main

git config --list

git config user.name

3. Create or clone a repo

git init -b main git clone https://github.com/user/repo.git git clone -b branch-name git clone --depth 1 # shallow clone

4. The core workflow

Stage Command Purpose Working → staging git add Select changes Staging → local git commit -m "msg" Save snapshot Local → remote git push Upload commits Remote → local/work git pull Fetch + merge Remote → local only git fetch Download, do not merge git status -sb # status, short git diff # unstaged changes git diff --staged # staged changes git log --oneline --graph --all git show # inspect commit git blame # line history

5. Stage & commit

git add # specific file git add. # all current changes git add -p # choose hunks git restore --staged # unstage git commit -m "feat: add login" git commit -am "fix: typo" # tracked files only git commit --amend --no-edit

Good commit messages

Use type(scope): short summary

feat = feature • fix = bug • docs = docs

refactor • test • chore • style

6. Branches

git branch # local list git branch -a # local + remote git switch -c feature/login # create + switch git switch main git branch -m new-name # rename git branch -d feature/login # safe delete git branch -D feature/login # force delete git push origin --delete feature/login

Common strategies: GitHub Flow (main + short-lived feature branches); Git Flow

(main/develop/release/hotfix); trunk-based (very short-lived branches).

7. Merge vs rebase

MERGE REBASE

History Preserves branch graph Replays commits; linear history Merge commit Sometimes No extra merge commit Shared branch Usually safe Avoid rewriting shared history Use for Integrating work Cleaning local branch history git merge feature/login git merge --no-ff feature/login git merge --abort git rebase main git rebase -i HEAD~3 # reorder/squash/edit git rebase --continue git rebase --abort git cherry-pick

Golden rule

Do not rebase public/shared branches. Merge them or coordinate history changes

with the team.

8. Resolve a merge conflict

1. Run git status and open conflicted files.

<<<<<<< HEAD

current-branch version

incoming-branch version >>>>>>> feature/login

2. Edit the file; remove markers and keep the desired result.

git add git commit # finish merge git merge --abort # cancel instead git mergetool # visual tool

DEVELOPER QUICK REFERENCE • GIT / GITHUB

PRINT DOUBLE-SIDED • Replace example names/URLs with your own

GIT & GITHUB CHEAT SHEET

Remotes, undoing, stashing, GitHub collaboration, recovery, and practical fixes | BACK

9. Remote repositories

git remote -v git remote add origin git remote set-url origin git remote show origin git push -u origin main # set upstream git push origin main git fetch origin git fetch --prune # remove stale refs git pull --rebase origin main git push --force-with-lease # caution

Remote shorthand

origin = your usual remote (often your fork). upstream = original project when

contributing through a fork.

10. Undo / recover

Goal Command Keeps work? Discard unstaged file git restore No local edits Unstage, keep edits git restore --staged Yes Undo commit, keep staged git reset --soft HEAD~1 Yes Undo commit, keep unstaged git reset HEAD~1 Yes Discard commit + edits git reset --hard HEAD~1 NO - destructive Undo shared commit git revert Creates new commit Find lost commit git reflog Recoverable refs Preview untracked cleanup git clean -n Dry run Delete untracked files git clean -fd Destructive

Quick rule

reset rewrites local history; revert is safer on shared branches; restore changes

files. Use reflog before panic.

11. Stash & tags

git stash push -m "WIP: form" git stash -u # include untracked git stash list git stash apply # keep stash git stash pop # apply + remove git stash show -p stash@{0} git tag -a v1.0.0 -m "Release" git push origin v1.0. git push --tags

12. Common workflows

Feature →PR:

git switch main && git pull origin main git switch -c feature/new-thing

edit files, then:

git add. && git commit -m "feat: new thing" git push -u origin feature/new-thing

open Pull Request on GitHub; review + merge

Sync a fork:

git remote add upstream git fetch upstream git switch main && git merge upstream/main git push origin main

13. GitHub collaboration

Pull Request (PR) = review proposed changes before merging. Typical flow: branch →

commit → push → open PR → request review → fix comments → merge →delete

branch.

Fork workflow: fork on GitHub → clone your fork → add original as upstream→

fetch/merge upstream/main → push to your fork →open PR.

gh auth login gh repo create my-project --public gh repo clone owner/repo gh pr create --title "Add login" --body "..." gh pr list gh pr checkout 42 gh pr status gh pr merge 42 --squash gh issue create --title "Bug" --body "..."

14. .gitignore essentials

node_modules/ venv/ pycache/ .env .env.local .DS_Store *.log *.pyc build/ dist/ .vscode/

Stop tracking but keep local file:

git rm --cached git check-ignore -v

Never commit secrets

Ignore .env and credentials before staging. If a secret was pushed, revoke/rotate it;

deleting the file alone does not erase repository history.

15. Common errors →fixes

Error / symptom Likely fix Not a git repository cd into repo or git init Push rejected / refs behind git pull --rebase, resolve, then push Merge conflict Edit markers → git add →git commit Detached HEAD git switch main or create a branch No upstream branch git push -u origin Permission denied (publickey) Configure SSH key or use HTTPS Uncommitted changes block switch git stash or commit first Unrelated histories Confirm repos; only then consider --allow- unrelated-histories

16. Aliases & extra tools

git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.lg "log --oneline --graph --all" git log -S "search-term" --oneline git bisect start # binary-search a bug git bisect good git bisect bad # repeat, then: git bisect reset

17. Most-used commands

Task Command Start / copy git init • git clone Check state git status -sb Save a change git add. • git commit -m "msg" Share / update git push • git pull Branch git switch -c History graph git log --oneline --graph --all Undo safely git revert Temporarily park git stash / git stash pop