3. Collaborating on GitHub

Practice Lab: Real Terminal & Git Scenarios

Solve 6 everyday developer situations involving .gitignore, accidental staging, git revert, cherry-pick, merge conflicts, and git stash before opening a Pull Request.

3 min readUpdated 2026-11-30
In This Lesson
  • Fix accidentally staged secrets before committing
  • Switch branches cleanly mid-feature using git stash
  • Recover from local or pushed mistakes safely with reset, revert, and cherry-pick

Think through each real-world scenario below, decide which terminal/Git command solves it cleanly, and open the solution to check your reasoning!

How to Practice Effectively

Before opening each solution, type your predicted commands in a scratch folder or on paper. Paying close attention to whether a commit was already pushed determines whether you use reset or revert.

Scenario 1: You Accidentally Staged .env

You ran git add ., and then git status shows .env (containing an API key!) sitting under Changes to be committed. You haven't committed yet. How do you remove .env from staging without deleting the .env file?

Scenario 2: Urgent Bug While Halfway Through a Feature

You are halfway through editing Header.tsx on feat/search, and a teammate asks you to test a 1-line fix on main right away. Your edits aren't ready to commit yet. What do you run?

Scenario 3: Committed Locally With a Typo in Code

You ran git commit -m "Add quiz" locally (not pushed yet!), then noticed you forgot to save one extra file. How do you undo the commit, keep your edits, add the missing file, and re-commit?

Scenario 4: Bad Commit Already Pushed to Shared main

Commit c4a910f was merged and pushed to main, and production started throwing 500 errors. Teammates have already pulled main. Why must you not run git reset --hard HEAD~1, and what should you run instead?

Scenario 5: Port One Hotfix Commit Without Merging an Entire Feature Branch

A colleague fixed a critical crash in commit 7d2e81b on feat/v2-redesign, which isn't ready to merge for another two weeks. How do you copy only that fix onto main?

Scenario 6: Finding Which Commit Changed a Specific Function

A function named formatMb suddenly broke and you want to see every recent commit that touched src/components/mdx/Playground.tsx (or search for commits that added/removed formatMb). Which commands show those commits with diffs?

Git Scenarios Practice Progress

0/6 done