2

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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
MeMyselfNI
  • 65
  • 1
  • 5
  • Question #1: When you started to work on that bugfix, did you already have **uncommitted** changes in your working folder, intended for `feature/nice-task`? If the answer to that is no, then you can simply check out the new branch directly. If the answer is yes, then it becomes more complicated. – Lasse V. Karlsen Feb 22 '22 at 14:27
  • Yes, I did, unfortunately – MeMyselfNI Feb 22 '22 at 14:28
  • 2
    That's Regret Type 3. https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard/59675191#59675191 – matt Feb 22 '22 at 15:09
  • Doesn't help with your current situation, but remember `git worktree add` for the future. Add a worktree for the critical bug fix. – torek Feb 23 '22 at 08:56

1 Answers1

-1
  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.

isherwood
  • 58,414
  • 16
  • 114
  • 157