1

I accidentally made a commit in the wrong branch but since I can do Amend I'm thinking if it's possible to use it to transfer the changes to the right branch. Can I change branch instead and do the amend there or will it be applied to the same branch where it was committed?

sylenix
  • 161
  • 2
  • 8
  • Possible duplicate of [How to undo "git commit --amend" done instead of "git commit"](https://stackoverflow.com/questions/1459150/how-to-undo-git-commit-amend-done-instead-of-git-commit) – gerrit Nov 09 '18 at 16:00

1 Answers1

1

Assuming you have not yet pushed the first branch with the mistaken commit, you may try cherry picking it to the right branch, then rolling back the first branch:

# switch to correct branch, and cherry-pick desired commit
git checkout branch2
git cherry-pick <SHA-1 of commit>

# then switch to first branch, and remove incorrect commit
git checkout branch1
git reset --hard HEAD~1

The <SHA-1> of the commit you want can be found by using git log branch1. Note that if you have already pushed branch1, then a safer option would be to git revert that commit. But, you may still use cherry picking to move the commit to the second branch.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks very much for the great Answer Tim! I'm more of wondering or trying to discover if there is another way of solving it than the usual way. I forgot to add it to the details of my question hehe. Thanks again! – sylenix Sep 28 '18 at 03:35