Home > Software engineering >  Commit changes to a different branch
Commit changes to a different branch

Time:02-26

Say I am working in a branch called "feature/nice-task". I am asked to urgently drop whatever I am working on and fix a huge critical bug that my ex-coworker John Doe left right before being fired for poor coding practices.

I create a branch "bugfix/horrible-bug", but in a wave of anger forget to switch to that branch. So, I continue to work in "feature/nice-task", and spot it right before I am going to commit. Is there still a way for me to commit these changes to "bugfix/horrible-bug" without spoiling "feature/nice-task" with unrelated commits?

What do I do?

CodePudding user response:

  1. Branch from where you are to create a temporary backup of all work.
  2. Commit any uncommitted work to the temporary branch.
  3. Checkout the commit from which you want to create the branch you intended to create.
  4. Create the branch you intended to create.
  5. Cherry-pick the commit(s) you need from the faulty branch.
  6. Reset the faulty branch to the last good commit.
  7. Delete the temporary branch.

All these steps are covered well in other tutorials and discussions, so I won't provide specific commands.

  •  Tags:  
  • git
  • Related