4

Occasionally I commit work only to later realize that it would have been better off in it's own branch.

Hindsight is 20/20 and if I was thinking ahead I would have made a new branch and then committed onto that sparing me this problem in the first place.

Is it possible to move an existing commit onto a new Branch? If so, how is it done? and can it be achieved through Visual Studio?

Liam
  • 27,717
  • 28
  • 128
  • 190
Roboko
  • 49
  • 1
  • 4
  • 1
    Does this answer your question? [How do I move a commit between branches in Git?](https://stackoverflow.com/questions/3710192/how-do-i-move-a-commit-between-branches-in-git) – Liam Sep 15 '20 at 14:40
  • 2
    *can it be achieved through Visual Studio* I doubt it, VS is has a really poor GIT interface. Do yourself a favour learn the commands or get a [UI for GIT that actually works](http://gitextensions.github.io/) – Liam Sep 15 '20 at 14:41
  • @Liam - your link assumes cherry-pick is necessary (which I don't believe it is based on the wording of the question), and also this question asks how to do it with Visual Studio, which that question and answer doesn't address. For the command line, this would be more relevant: https://stackoverflow.com/q/1628563/184546 – TTT Sep 16 '20 at 13:49

1 Answers1

2

It can be done in Visual Studio. Here are the steps:

  1. First make sure you don't have any pending changes. (Stash, commit, or undo them.)
  2. Right click on your checked out branch and choose "New Local Branch From..."
  3. Enter in the name of your new branch, but uncheck "Checkout branch".
  4. Now "View History" on your current branch.
  5. In the history view, right click on the commit you want to reset to. This would be the commit just before your first commit that you intended to add to the new branch. Select "Reset --> Delete Changes (--hard)".
  6. Now checkout your new branch.

You now will be in the exact same situation as if you created the new branch in the first place.

Side note: I do recommend learning how to do this from the command line rather than using the GUI. Once you know it, it's faster, easier, doesn't tie you to any particular UI, and enables you to script your favorite workflows more easily. As an example, here's the same workflow from the command line:

  1. First make sure you don't have any pending changes. (Stash, commit, or undo them.)
  2. git branch new-branch-name # create new branch like what's checked out now
  3. git reset --hard HEAD~1 # go back one commit (change 1 to however many you need)
  4. git checkout new-branch-name
TTT
  • 22,611
  • 8
  • 63
  • 69
  • 3
    This isn't "moving commits between branches". Your just deleting commits. That's not what the OP is asking. They need to use [`cherry-pick`](https://stackoverflow.com/a/3710351/542251) – Liam Sep 16 '20 at 07:54
  • 1
    @Liam - My answer moves the commit to a new branch which is based off the current branch. OP didn't explicitly specify if the new branch is based off of the current branch or not. Cherry pick would only be required if it's not. But based on the wording of the first sentence, "would have been better off in it's own branch" I think my interpretation is likely correct. – TTT Sep 16 '20 at 12:40
  • @Liam - btw, if cherry-pick is required, it's a few extra steps in Visual Studio, but still possible. – TTT Sep 16 '20 at 12:47
  • 1
    @Liam this just came up in another question and it reminded me of this discussion we had last year. I think my comment on the other question might clear up what I was trying to say, which is: "Cherry pick will work regardless of your specific scenario so that's a good default approach. Note if your scenario is that you intended to create a *new* branch off of the branch you're currently on and just forgot, then you could save a step (compared to cherry-pick) by instead just creating a new branch from where you are right now, and then reset your current branch back to before the new commit(s)." – TTT Apr 22 '21 at 14:33