Skip to content

14. Real-World Git Scenarios

You’ve now seen every command this course set out to teach. The final step is putting them together the way you will in a real job, where the question is never “what does git rebase do?” but “my teammate just pushed over my work, what now?”. This chapter walks through the situations you’ll actually face, tying together everything from the previous chapters.

Working alone, you can push to main whenever you like. On a team, that quickly becomes chaos. The shared discipline that keeps everyone sane looks like this:

  1. Start from the latest code. Before creating a branch, sync your local main:

    Terminal window
    git switch main
    git pull
  2. Work on a branch, never directly on main. Give it a descriptive name:

    Terminal window
    git switch -c feature/user-profile
  3. Commit small, push early. Push your branch so teammates can see it and so your work is backed up:

    Terminal window
    git push -u origin feature/user-profile
  4. Open a pull request and let it be reviewed. This is where the platform features from Chapter 13 come in.

  5. Keep your branch fresh. If main moves while you work, bring those changes into your branch regularly so the eventual merge is painless:

    Terminal window
    git switch feature/user-profile
    git merge main # or: git rebase main

This is the everyday team conflict, and you’re already equipped for it. When your pull request reports a conflict, sync main into your branch locally, resolve the conflict markers as we did in Chapter 4, commit, and push. The pull request updates itself.

Imagine production is broken right now, but main already contains half-finished features that aren’t ready to ship. You can’t just deploy the latest code. This is the classic hotfix scenario.

The pattern is to branch the fix off the last released, stable point rather than off the current development tip:

Terminal window
# Branch from the production tag or main, depending on your workflow
git switch -c hotfix/payment-crash v2.3.1
# ... make the minimal fix ...
git commit -m "fix: prevent crash when payment token is missing"
git push -u origin hotfix/payment-crash

Once reviewed, you merge the hotfix into production and also bring it back into your development line so the bug doesn’t reappear in the next release. If the fix landed first on the release branch, the git cherry-pick command is the perfect tool to copy that single commit over to main:

Terminal window
git switch main
git cherry-pick <hotfix-commit-hash>

Release branches follow a similar idea. When you’re preparing a version to ship, you cut a release/2.4 branch, stop adding new features to it, and only allow bug fixes and final polish. Meanwhile, main keeps moving with new work. When the release is ready, you tag it (from Chapter 7):

Terminal window
git tag -a v2.4.0 -m "Release 2.4.0"
git push origin --tags

Everyone makes mistakes in Git. What separates a calm developer from a panicked one is knowing that almost everything is recoverable. Here’s a quick-reference rescue table pulling together the tools from across this course.

”Oh no, I…”The rescue
…edited a file and want the original backgit restore <file> (Ch. 6)
…staged a file by accidentgit restore --staged <file> (Ch. 6)
…wrote a bad commit messagegit commit --amend (Ch. 6)
…committed too early and need to add a filegit commit --amend --no-edit (Ch. 6)
…need to undo a commit that’s already pushedgit revert <hash> (Ch. 6)
…reset hard and lost commitsgit reflog then git reset --hard HEAD@{n} (Ch. 8)
…committed on a detached HEADgit switch -c rescue-branch (Ch. 11)
…committed to the wrong branchgit cherry-pick it to the right one, then reset the wrong one (Ch. 8)
…committed a secretRotate it immediately, then scrub history (Ch. 6, Ch. 10)

Worked example: “I committed to main by mistake”

Section titled “Worked example: “I committed to main by mistake””

You meant to work on a branch, but you’ve made two commits straight onto main. Here’s the calm fix:

Terminal window
# Create a branch that keeps your two commits exactly where they are
git switch -c feature/where-this-belongs
# Move main back to before your commits (it still has them via the new branch)
git switch main
git reset --hard HEAD~2

Your work is safe on feature/where-this-belongs, and main is clean again. Because you understand what reset and switch actually do, a situation that looks alarming becomes three short commands.

You started this course thinking Git might be intimidating, and you’ve ended it recovering lost commits and designing branching strategies. That’s real growth. 🎉

The last and most important step is practice. Git only becomes second nature when your fingers know it, so head over to the Learning resources chapter for sandboxes and exercises, then go break things in a test repository. You now have the knowledge to fix anything you break, which is exactly the confidence a good developer needs.