How to cut old commits from a feature branch?

Viewed 84

I have feature2 branch, that was created this way:

develop -> feature1(branched from develop) -> feature2(branched from feature1)

feature1 has already merged, Now I'm trying to merge feature2 branch. But in the commit history and the "changes" tab, I see the changes from the feature1 branch:

1. Commit to feature2
2. Commit to feature2
3. Commit to feature1
4. Commit to feature1

So, how can I drop everything before second commit? Like if feature2 branch was branched from develop? It makes review a bit difficult, because I see old code in Changes tab

2 Answers

You don't need to drop anything. Git is smart enough to know that those commits for feature1 have already been merged to master, and won't try to merge them again when you merge the feature2 branch.

Rebase feature2 on to develop and drop the commits you do not want:

# while on feature2:
git rebase -i develop

The interactive rebase will open a text editor allowing you to specify which commits to keep and which to drop:

pick Commit 1
pick Commit 2
pick Commit 3
pick Commit 4

Comments in the text editor tell you what you can do, but in your case:

pick Commit 1
pick Commit 2
drop Commit 3
drop Commit 4

If feature2 has already been pushed, you'll need to force-push the branch:

git push origin HEAD --force

Warning: this will orphan anyone else's branch if they based it on feature2. Be careful and talk to your teammates first!

Related