6

I just committed some changes to a feature branch, but I realize I was on the wrong branch.

How do I take the last commit I made, and move it to a different, already existing, branch? I haven't pushed anything yet.

I've seen some similar questions but they all involve creating a new branch, and all the hard head resetting they're doing is a mite scary.

1

3 Answers 3

3
git checkout whereishouldhaveputit
git cherry-pick whereididputit
git checkout -B whereididputit whereididputit~
1

You need to really understand that git commits are never 'on' branches. In git, a branch is just a label that points to a single commit, the head of the branch.

So, if you committed to the wrong branch, you need to move the label back to where it was, which is the essence of a reset operation. That will undo your mistake from the point of view of the incorrect place you put the commit. The extra commit will just hang around, doing no harm, until it gets garbage-collected.

Thus, the basic plan would be to use a merge to apply the change as the successor to the head of some other branch, and then do a hard reset to put the label of the accidental target back to where it was.

2
  • there are older commits on wrongbranch that I don't want on rightbranch so I can't just merge wrong into right. Do I want cherrypick ?
    – Damon
    Commented Mar 5, 2014 at 3:10
  • then you want cherrypick to move to the right branch and git revert, which creates a new commit that undoes an old commit, on the wrong one.
    – bmargulies
    Commented Mar 5, 2014 at 13:35
1

You're going to need to use reset on your current branch - that's just how this works. Don't worry though - all your stuff will be safe by the time you use it.

First, while on your branch where you've accidentally added the commits (we'll call it branch1) create a new branch (let's call it temp)

git branch temp

As @bmargulies said, branches are just a reference to a commit. As long as you have a reference to a commit, you haven't lost it. We're going to move your branch1 branch back to where it started before you mistakenly made the commits, and so temp is our reference to the commits you've added - think of it as a backup. We can now make all sorts of changes to where branch1 points without worrying about losing your new commits.

If A, B and C were commits originally on branch1 and you accidentally added D E and F then your commit history will now look something like the following (brackets show which commit each branch is referencing):

A <- B <- C <- D <- E <- F (temp)(branch1)

If you added, say, 3 extra commits (D, E and F) that you didn't mean to add to branch1, then you can reset branch1 back to where it should be using:

git reset --hard HEAD~3

Now things will look like this:

A <- B <- C (branch1)<- D <- E <- F (temp)

Now the scary part. Rebasing. Rebase has a flag --onto which takes the following form:

git rebase --onto <branch a> <branch b> <branch c>

What it basically says is "find the common ancestor of branch b and branch c, take the changes introduced by each commit between the common ancestor and branch c and replay them onto branch a.

The "common ancestor" stuff might sound scary, but if branch b is an ancestor of branch c (read: arrows point from branch c to branch b) then the commit branch b points to is the common ancestor.

See where this is going? If we say the branch you want to move your commits onto is called branch2 then you can move the new commits to it using:

git rebase --onto branch2 branch1 temp

That will take commits D, E and F and put them on the end of branch2 (they'll be called something else though as this is a rebase - you've said you haven't pushed to a public repo so this shouldn't be a problem)

branch1 will now be where it was before you mistakenly added those commits, the new commits will be sitting on the end of branch2, and you can delete temp using

git branch -D temp
1
  • Just saw you said you only had one commit you wanted to move. Cherry pick will work fine for one commit. Rebasing is overkill. Commented Mar 5, 2014 at 3:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.