In git, how do I remove a commit from a branch but keep that commit in a separate branch?

Multi tool use
Multi tool use


In git, how do I remove a commit from a branch but keep that commit in a separate branch?



I have a DEV branch with a history looking like this (with each list item being a separate commit):



Suppose I want to "delete" feature 3 from this branch, but I don't want to lose the code in this feature so I want to put it in it's own separate branch.



Basically, from the dev branch above, I want to end up with two branches looking like this:



DEV BRANCH:



NEW BRANCH:



Any ideas how I can achieve this in git?





Possible duplicate of Move some commits to another branch
– phd
Jul 2 at 8:51




3 Answers
3



Create a new branch from feature3. Then reset feature3 from the dev branch. However, if you have pushed these commits you will need to use revert


feature3


reset


feature3


dev


revert



First create newbranch from feature3


newbranch


feature3


git checkout dev
git branch newbranch feature3
git reset --hard feature3



Now if you haven't pushed to remote


git reset --hard feature3



Or if you have pushed


git revert feature3



Also, if you want to add specific commits to newbranch, like feature7 you can use


newbranch


feature7


git checkout newbranch
git cherry-pick feature7





Perfect. Worked flawlessly. Actually also needed the cherry-picking. Thanks!
– Weblurk
Jul 2 at 9:18


#create NEW
git branch NEW commit3

#rewrite DEV
git rebase -i commit2 DEV

#in the editor remove the line "pick commit3", save and exit.



To create a new branch till feature3:


feature3


git checkout <feature3_commit_id>
git checkout -b new_branch



To remove feature3 from dev branch


feature3


git checkout dev_branch
git rebase --onto <feature2_commit_id> <feature3_commit_id> <feature5_commit_id>



Just FYI, you can use git log --oneline to find commit ids.


git log --oneline






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

2 TT,5zKYAv7V58
ClGj0pzlWRID8p4WKwa,hK01WpeKKzQJN8,TMyUs4wJnFbip2,hY LF9ZITzH5RM3tqsq e cJTUZ,Nig,EfTO,C19I6gVK nbc,a727

Popular posts from this blog

Rothschild family

Cinema of Italy