Uwe Hafner
Uwe Hafner

Reputation: 4989

Easy way to switch/swap git checkins between 2 branches

I made an implementation for a feature on develop-branch. It worked out well but then I came up with an interesting alternative. I branched from the checkin of develop-branch before the feature checkin on develop to implement the alternative.
Now I decided that my alternative is the better implementation. But I wouldn't want to loose the first implementation. My branches now look like this:

branches

I would like to continue on "develop"-branch and keep the 1st implementation on "newoverview" branch.
Is there an easier way than somehow preserve the changes of each branch. Undo both branch commits. Reapply the changes to the correct branch and checkin again? That sounds like a lot of error prone tasks.

Can I just tell git to take 1st commit and put it on "newoverview" branch and 2nd commit to put it on "develop" branch?

Ann: My repository also has a master branch. But this one will get changes merged only when I am finished and only from the develop branch. All other branches are also not pushed to the server. they are only locally.

Upvotes: 0

Views: 36

Answers (1)

Nick Volynkin
Nick Volynkin

Reputation: 15099

checkout to the develop branch

git checkout develop

create a new branch on top of develop to save results. Don't checkout!

git branch alternative

Reduce develop to common ancestor with newoverview, which seems to be the previous commit.
If it's not the previous commit, use last common ancestor's SHA instead of HEAD^

git reset --hard HEAD^
git reset --hard abcd1234

merge the new feature into develop.

git merge newoverview

if anything ever goes wrong, use this to restore lost commits' SHAs

git reflog

Upvotes: 1

Related Questions