I did some work on the wrong branch; I forgot to check out the right branch. Is it possible to change to the other branch and commit my changes there?

  • The easiest way is to check git log and note your commit hashes, checkout your right branch and do: git cherry-pick h4sh. – kenorb Sep 16 '15 at 15:31
  • Other way is to checkout the right branch and merge it the one which you worked, but then it'll merge the other changes as well, so it depends if you want to do that. – kenorb Sep 16 '15 at 15:32
  • Have you made commits on the wrong branch, or just done work that is uncommited? – heavyd Sep 16 '15 at 15:41
  • I don't have made any comits yet. I figured that was not a smart idea to do when on the wrong branch. Does that make a difference heavyd? – Ravenix Sep 17 '15 at 7:19

Depending on the types of changes you make, you may not be able to just switch branches. If that is the case, you can use the git stash command to allow you to change branches. Just use the following commands:

git stash
git checkout <new_branch>
git stash pop

When my changes are not comitted yet. I could just checkout the right branch. The changes will be still there. Just checkout the-right-branch and then you can commit your changes in the-right-branch. Thanks all for reacting.

  • first stash and then pop is actually a good idea! – Ravenix Sep 18 '15 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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