I have fixed two bugs in a piece of code, and it modifies common files. For example, bug fix 1 modified file A, B, C, and bug fix 2 modified B, C, and D. I have all changes in one commit. For the purpose of code reviews, I need to split the changes into two commits (two branches actually).
What I am thinking is that from my current branch fix
, I create two branches like git branch fix-1 fix
and git branch fix-2 fix
. Both branches have all changes. Now, in branch fix-1
, I would like to keep the changes relevant to fix 1 only, that is, all changes in file A, and parts of changes in files B and C. Is it possible to do it in git? What command is relevant for this?
I know that in git add
, I can do interactive add where I can select hunks of code for adding, not just at file level. Is there something similar I can do here?
If the commit was your last commit, here is a solution.
Checkout the branch with the fix
git checkout fix
Reset back to before the fixes, but keep the changes in your working dir
git reset HEAD^
Create branch for fix-1
git checkout -b fix-1
Add/commit fix-1 fixes
git add -p
git commit
Stash the remaining changes
git stash
Checkout/create branch for fix-2
git checkout -b fix-1
Pop off your stashed changes
git stash pop
Commit/add them
git commit -a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With