World map

 

BM: Baca dalam Bahasa Melayu

World 11: Rescue Center

I committed on the wrong branch

You will learn: how to move a commit to the branch it belongs on.

The problem

A new commit, Add about page, lands on main. The main label and HEAD move up to it.
6d3a9b1a8feb63270a76e

Oops. You committed your about page on main. It was meant for its own branch. This happens to everyone. Here’s the safe way out.

Try it

First question: did you push it? Not yet. So which fix fits?

Pick one answer.

Watch

A new label, about, appears on the Add about page commit. Then main moves back one commit, to Add style. The commit stays safe on about.
6d3a9b1a8feb63270a76e140649c

git branch about sticks a new label on your commit. git reset --hard HEAD~1 moves main back one step. Your commit stays safe on about.

Try it

Your turn. Move the commit to a branch called about. Then switch to it.

6d3a9b1a8feb63270a76e140649c

Predict

Careful: reset --hard also wipes changes you haven’t committed. When is it safe here?

Pick one answer.

Check

What if you had pushed it? Pick the fix for each case.

Drag a card into a box. Or tap the card, then tap the box.

      You did it!

      You can now move a commit to the branch it belongs on.

      Go deeper (optional)

      Does the right branch exist already? Switch to it and run git cherry-pick main to copy the commit. Then take it off main with git reset --hard HEAD~1. Already pushed? Run git revert HEAD on main and push. Then git switch -c about and git cherry-pick main~1 bring your work over.