1

I'm working with git on two different branches at the same time (ex feature/feature_1 and feature/feature_2)

On feature/feature_1, I started from the master, and I already pushed some commits (but not merged).

For feature/feature_2, I wanted to start from master as well, but unfortunately, I started from feature/feature_1, and I didn't notice it at the beginning, and I pushed some commits.

So on feature/feature_2 I have every commit from feature/feature_1 (that I don't want on this branch), plus some other commits that I want to keep.

How can I "remove" the commit from feature_1 that I have on feature_2, without deleting the work I have already done on feature_2.

I would point out that both branches are already pushed but are not merged yet, and have nothing in common.

Thanks

Krishna Sony
1,36917 silver badges27 bronze badges
asked Jun 3, 2020 at 15:20

2 Answers 2

4

The easiest way:

git rebase --onto master feature/feature_1 feature/feature_2

That's it

answered Jun 3, 2020 at 15:26
Sign up to request clarification or add additional context in comments.

3 Comments

This is the best answer, but I can never remember the order to put the commits, so I always fall back to interactive rebasing.
It's very simple: git rebase --onto new-base drop-this rebase-this
Yeah I know it every time I see it explained. Then I forget again.
1

Checkout feature_2:

$ git checkout feature/feature_2

Do an interactive rebase against master:

$ git rebase -i master

Delete all of the commits you don't want on feature_2 in the editor that comes up, then save and exit.

You'll need to force push your branch if you need others to see the changes - this operation is changing history.

answered Jun 3, 2020 at 15:22

1 Comment

Anytime you edit history on a pushed branch you need to force push and then inform any other users of that branch so that they know to force pull when they update.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.