Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase feature branch messes up commits in pull request to develop/master branch

I have the following scenario:

  • Master-branch: what is in production, can contain hotfixes
  • Develop-branch: the branch my developers are using to create pull requests to
  • feature-branches: the branch we create for the feature the developer is implementing.

Once the developer has finished its work, he creates a pull request on the develop branch. After approval, we squash-merge the feature branch onto the develop branch in order to not include all the commits the developer made on the feature branch. This allows us to have a clear and clean git history on the develop branch.

Sometimes the feature branch needs a rebase from the develop branch and this is where the trouble starts.. When we rebase the feature branch with the develop branch, all of the sudden a lot of commits from the develop branch are included in the pull request.

How can this be avoided so that the PR only includes the actual commits from the feature branch?

like image 994
Serafijn Avatar asked Jan 15 '20 08:01

Serafijn


People also ask

Does rebase affect master?

git rebase origin/master will merge in the requested branch ( origin/master in this case) and apply the commits that you have made locally to the top of the history without creating a merge commit (assuming there were no conflicts).

Should you rebase feature branches?

If the feature branch you are getting changes from is shared with other developers, rebasing is not recommended, because the rebasing process will create inconsistent repositories. For individuals, rebasing makes a lot of sense. If you want to see the history completely same as it happened, you should use merge.

What happens when you rebase a branch?

The Rebase Option This moves the entire feature branch to begin on the tip of the main branch, effectively incorporating all of the new commits in main . But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.


1 Answers

I suspect this happens after you rebase develop from master after a hotfix release.

Consider the following scenario:

master   A->B->C
                \
develop          D
                  \
feature A          E

You then get a hotfix in master, F, and rebase develop off it. The rebase creates a "new" commit (D') with a different hash, so from git's perspective D and D' are two separate and unrelated commits. D still exists, and C is its parent, but it is no longer on develop - only on feature A:

master   A->B->C->F
                \  \
develop          \  D'
                  \
feature A          D->E

So if you then try to rebase feature A off develop, if you don't do an interactive rebase, git won't be able to recognise that D and D' are the same commit, and you'll end up with the following:

master   A->B->C->F
                   \
develop             D'
                     \
feature A             D->E

To get around this, when rebasing feature A from develop, do it interactively and tell git to drop D, as you know that it is identical to D'.

like image 96
Pesho_T Avatar answered Oct 13 '22 15:10

Pesho_T