Skip to content

Scenarios

I committed to the wrong branch

As long as you haven't pushed yet, this is a small mistake that only affects your local repository and is easy to fix.

If you wanted to commit to branch A but committed to branch B instead, then here's what you did:

  1. You created a new commit in the local repository based on B.
  2. You moved the branch head B to the new commit.
% git log --graph --oneline --all
* c14bf96 (HEAD -> B) Example 4
| * 449074f (A) Example 3
| * 139ca68 Example 2
|/
* d876b6a Example 1

To fix this mistake, you need to do 2 things:

  1. Copy the commit to branch A, where it was intended to be created, and move A to the new commit.
  2. Move the branch head B back to where it originally was.

Cherry-picking

Cherry-picking applies a copy of the specified commit to the current branch. The current branch head is then moved to the new commit as if we had used git commit and made the change from scratch.

Let's begin with step 1, getting the new commit to where it was supposed to be. First, we switch to the branch where we intended to create the commit. When inspecting the graph, note how HEAD now points at branch A.

% git switch A
Switched to branch 'A'

% git log --graph --oneline --all
* c14bf96 (B) Example 4
| * 449074f (HEAD -> A) Example 3
| * 139ca68 Example 2
|/
* d876b6a Example 1

Then we copy the commit from branch B to the current branch. Note that git log --graph by default arranges the graph in a way that shows the newest commit at the top, so the graph is arranged differently from before. In this example, 9912692 is a copy of c14bf96, but based on a different parent commit.

% git cherry-pick B
[A 9912692] Example 4
 <...>

% git log --graph --oneline --all
* 9912692 (HEAD -> A) Example 4
* 449074f Example 3
* 139ca68 Example 2
| * c14bf96 (B) Example 4
|/
* d876b6a Example 1

Now to step 2: Resetting branch B to its previous state. The easiest way to do this is with the git branch -f <commit or ref> command, which forces the branch to point at the specified commit.

Many roads lead to Rome, but some are more dangerous than others!

In case you're thinking of switching to branch B (git switch B) and then hard resetting it git reset --hard <commit or ref>) before switching back to A: Yes, this also works, but it's more complicated and requires additional care. To reset a branch head, especially if it's not even the current branch, prefer simple pointer operations that don't affect the working tree and potentially uncommitted changes. This is not about the "fastest" way to achieve a result, but about reducing unnecessary and potentially risky extra steps.

While we are still on branch A, we move the local branch B back to where it was. B~1 means "the parent of B" (1st generation parent). We could also write the commit ID d876b6a instead.

% git branch -f B B~1

% git log --graph --oneline --all
* 9912692 (HEAD -> A) Example 4
* 449074f Example 3
* 139ca68 Example 2
* d876b6a (B) Example 1

Visually check before and after

Use commands like git show and git log --graph to make sure you understand the current situation, and to verify afterwards that what you did had the desired effect. Did you reset the correct branch? Does the graph look the way you expected? This not only helps prevent (further) mistakes, but it also helps build an accurate mental model of what actually happens. The mental model that so many developers lack, preventing them from using Git with confidence.