0

I have a develop branch with a mix of commits for two features:

2b50732 develop feature 1 commit
13b07e8 develop feature 1 commit
d5da9b5 develop feature 2 commit
a42b21d develop feature 1 commit
3c032d1 develop feature 2 commit
d994014 develop feature 1 commit

I want to move the commits for feature 2 into a separate branch; second-feature. The reason for this is that I want to merge my develop branch into my master but the second feature isn't ready yet so I want those commits pulled out for the moment to be re-merged again at a later date

I have read this answer already: Move the most recent commit(s) to a new branch with Git which suggests simply branching and reseting:

git branch second-feature
git reset --hard HEAD~10
git checkout second-feature

but (ignoring the fact I want specific commmit moved, not consecutive) this only works for local changes as git reset --hard ... will require a forced push which will in turn affect the develop branches history. In my case, develop is already pushed to the remote and I want to avoid affecting the history.

Is there an approach for moving these commits from the develop branch to a second-feature branch while maintaining the remote develop history as well as allowing them to be merged back again into develop later?

2
  • So what does the "without affecting history" means?
    – Ali
    Apr 23, 2014 at 22:35
  • Short answer: No, this is not possible. Commit hashes are based on, among other things, the hash of the parent object. If you remove the "feature 2" commits from that branch you will be changing the ancestry of the remaining "feature 1" commits, which will change their commit hashes.
    – Chris
    Apr 24, 2014 at 0:28

1 Answer 1

4

My sugestion is to create a branch at a point where you want both develop and second feature to match. git co <the commit you like in both places> then create the feature branch there git co -b <feature-branch-name>.

Then once you have the feature-branch-name branch start cherry picking the commits of that feature you want. git cherry-pick <hash of commit I want>. Repeat until done.

Finally checkout the develop branch again and revert the commits you only want the in the feature branch. git revert <hash of commit I previously cherry picked into feature>.

Enjoy!

You can continue to cherry-pick commits from develop into feature to keep it up to date and when you are ready to merge the feature you can go ahead and do it as a merge or you can cherry-pick the differing commits.

In this way you actually keep the entire history of what happened, you added a commit to develop, you decided it is better suited for a feature branch, you moved it there and reverted that commit in develop and when you were done you merged develop into branch. Nothing is lost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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