For my project I use git-flow. I have master
and develop
branch synchronized with remote-repo
Usually for all changes I create a feature
branch, edit the files, commit the changes on the feature
branch and close the branch with a merge in develop
.
But sometimes I forget to create a feature
branch and I edit files directly in develop
. How can I move the canges in a new feature
branch?
If you've already made the commit to develop
, and your history looks like this:
A---B---C---D develop
Here, C
is the last correct commit on develop
, and D
should have been committed to a feature branch. Do the following (while on develop
):
git branch feature # creates feature branch pointing at D
git reset --hard C # or HEAD^, HEAD~1, etc - resets develop back to C
This will result in:
A---B---C develop
\
D feature
This will also work well if you've made more than one commit, as you can substitute whichever commit reference you need to reset develop
back to in the second command.
It's worth noting that reset --hard
will discard any uncommitted changes. I would always advise only manipulating commit history with a clean working tree, but to avoid this you could use a different reset mode like --merge
, which will allow the reset as long as your uncommitted changes aren't to any files changed in C...D
.
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