Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In git, how do I remove a commit from one branch and apply it to a different branch?

Tags:

git

branch

I have two branches off of master, each one for a different feature, and then I have a synthesis branch that combines the two. I committed something to the synthesis branch, but now I see I would have rather applied that change to one of the branches particular to that feature. Is there a way to do this unapply/apply somewhere else maneuver with git?

like image 290
prismofeverything Avatar asked Nov 20 '09 22:11

prismofeverything


People also ask

How do you remove a commit from one branch to another?

Make sure you are on the branch to which you have been committing. Use git log to check how many commits you want to roll back. Then undo the commits with git reset HEAD~N where “N” is the number of commits you want to undo. Then create a new branch and check it out in one go and add and commit your changes again.

Can a commit be taken from one branch and move to a different branch in git?

You can move commits from one branch to another branch if you want changes to be reflected on a different branch than the one to which you pushed the changes.


1 Answers

Cherry-pick commit to target branch and reset source branch. Assuming, you want to move the latest commit from source branch to target, do:

git checkout target git cherry-pick source git checkout source git reset --hard source^ 

If the commit wasn't the last, you will have to use git rebase -i instead of the last command and choose specific commit name for your cherry-pick.

like image 61
P Shved Avatar answered Sep 19 '22 09:09

P Shved