Search code examples
gitgithubbranchbranching-and-merginggit-diff

How to Create a Tertiary Feature Branch With a Correct Git Diff


If I want to create a new feature branch off master, I can do:

$ git checkout master
$ git checkout -b myFirstFeature

But suppose I now have that branch up in a PR that is waiting to be merged, and I want to work on something else that depends on the changes that are in that branch. In that case I might create a tertiary branch by doing:

$ git checkout myFirstFeature
$ git checkout -b mySecondFeature

So now I've set myFirstFeature as upstream of mySecondFeature and I have access to all the changes there and can start building on them, which is what I wanted. Nice!

However, the issue is that when I put up a PR for mySecondFeature, Git includes all the changes from myFirstBranch in the diff for mySecondFeature, rather than just what I actually committed to mySecondFeature.

It even includes the changes that resulted from any time I pulled master, ie any other PRs that were merged during the time myFirstFeature has existed.

So now mySecondFeature has a diff on GitHub of like 200 files, even though I personally only edited 3 or 4 on that branch.

How can I achieve my goal of creating a new tertiary feature branch off of an existing un-merged feature branch without totally borking my git diff?

thanks!


Solution

  • Here's the solution:

    git checkout master

    git checkout -b new_branch (create a new branch)

    git merge --squash old_branch (merge in the dependant feature branch)

    if you did this wrong the first time and now have work on another branch you want to pull in to new-branch, do:

    git cherry-pick <oldest_commit>^..<newest_commit>