■ Git & GitHub Master Cheatsheet
■ 1. Git & GitHub Commands Cheatsheet
Git Basics
• `git init` → Initialize a new Git repository
• `git clone ` → Clone (download) a repo
• `git status` → Show current repo state
• `git add ` → Stage a file
• `git add .` → Stage all changes
• `git commit -m 'msg'` → Save staged changes with a message
Branching & Switching
• `git branch` → List branches
• `git branch ` → Create a new branch
• `git checkout ` → Switch to branch
• `git checkout -b ` → Create & switch to branch
• `git switch ` → Alternative to checkout
• `git merge ` → Merge into current branch
Remote Repos (GitHub)
• `git remote add origin ` → Link repo to GitHub
• `git remote -v` → Show remotes
• `git push origin ` → Push commits
• `git push -u origin ` → Push & set upstream
• `git pull origin ` → Fetch & merge latest
• `git fetch` → Download changes only
Tags (Versions)
• `git tag` → List tags
• `git tag ` → Create tag
• `git tag -a v1.0 -m 'msg'` → Annotated tag
• `git push origin v1.0` → Push tag
• `git push origin --tags` → Push all tags
Undoing Mistakes
• `git reset ` → Unstage file
• `git reset --hard ` → Reset to commit (■ irreversible)
• `git revert ` → Undo commit safely
• `git restore ` → Discard local changes
Merge Conflicts
• Conflict markers appear (<<<<<<<, =======, >>>>>>>).
• Edit & decide what to keep.
• `git add ` → Stage fixed file.
• `git commit -m 'Resolved conflict'` → Save resolution.
Open Source Contribution Workflow
• 1. Fork repo → copy project
• 2. Clone → `git clone `
• 3. Create branch → `git checkout -b feature`
• 4. Commit changes → `git commit -m 'msg'`
• 5. Push branch → `git push origin feature`
• 6. Open Pull Request on GitHub
CI/CD Basics
• CI → Auto tests on every push/PR
• CD → Auto deployment after merge
• GitHub Actions → define `.github/workflows/ci.yml`
Pro Tips
• `git log --oneline --graph` → Pretty history
• `git diff` → Show unstaged changes
• `git stash` → Save temp work
• `git stash pop` → Restore work
• `git pull --rebase` → Clean history
• `git cherry-pick ` → Apply specific commit
■ 2. Visual Analogies & Workflows
■ Git vs GitHub
Git (Local) GitHub (Remote)
■ Personal notebook for code versions ■■ Cloud platform to share/collaborate
Track history, branches, commits Host repos, PRs, issues, CI/CD
■ Stage, Commit, Push (Assignment Analogy)
• Working Directory → Rough draft of answers
• Stage (`git add`) → Put ready answers aside
• Commit (`git commit`) → Copy to final notebook with note
• Push (`git push`) → Submit notebook to professor (GitHub)
■ Stash Analogy
■ Like hiding unfinished homework in a drawer to continue later.
■ Branch & Merge Analogy
• Branch → Photocopy of notebook to try new ideas safely
• Merge → Copy best answers back into main notebook
• Merge Conflict → Two people edited the same answer differently → decide what to keep
■ Git Workflow (Visual Flow)
Working Directory Stage Commit Push
Edit files git add git commit -m git push
■ Draft work ■ Ready to commit pile ■ Final copy with note ■■ Upload to GitHub
■■ 3. Common Git Errors & Fixes
■ Merge Conflict
• Cause: Two branches edited same file/line.
• Fix: Open file → choose what to keep → `git add` → `git commit`.
■ Detached HEAD
• Cause: Checked out specific commit, not a branch.
• Fix: `git checkout main` or create new branch → `git checkout -b new-branch`.
■ Rejected Push (non-fast-forward)
• Cause: Remote has new commits you don’t have.
• Fix: `git pull origin main --rebase` → then push again.
■ Accidentally committed to main
• Fix: `git checkout -b new-branch` → `git reset --hard HEAD~1` on main → push new branch.
■ Forgot to add file to commit
• Fix: `git add ` → `git commit --amend` (updates last commit).