1

I have made a commit on a wrong branch and I wanted to know if I can move the commit from the branch to another.

Here's my actual situation

Branch 1 ----- 2 --- 4 --- 5 --- 6
              /   
HEAD ------- 1  
              \
Branch 2 ----- 3

And here is what i would have

Branch 1 ------ 4 --- 5 --- 6
              /   
HEAD ------- 1  
              \
Branch 2 ----- 2 --- 3

Is that even possible?

The commit "2" only add a file never used in that branch after but i need that file in my Branch 2.. Can I "simply" recreate the file in my Branch 2 and deal with the one my Branch 1 when I'm gonna merge them? I'm using GitHub to host my code and GitKraken on my desktop.

Thank you!

1 Answer 1

1

You have two option. Rebase or cherry-pick and revert.

Cherry-pick and revert is the simplest option and will have the least impact on your team. The history will not match your graphs exactly but will have the desired result

git checkout branch_1
git revert 2
git checkout branch_2
git cherry-pick 2

Rebase will give you the desired history but if you're working with other people could cause issues if they are working off the current history.

git checkout branch_1
git rebase -i 1
*Remove commit 2 from editor, save and quit*
git checkout branch_2
git rebase -i --root
*Remove commit 2 from editor, save and quit*

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