Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git compare two branches in relation to a third

Working with github/gitlab, using feature branches in many cases pull requests go out of date while waiting for a review and need to be rebased on top of the new target branch (master) after a while.

This will sometimes create conflicts that need to be solved.

After a rebase I would like to compare the old and the new branch in relation to the target branch, to verify no errors have been made while solving the conflicts. Meaning I would like to verify which lines are going to change after merging the new version of the PR.

So basically I would like to diff the result of git diff branch origin/master and git diff origin/branch origin/master - what changed in the diff output on the PR view of github/gitlab between the old and new versions.

Is there a command to do this?

like image 696
Ákos Vandra-Meyer Avatar asked Jan 01 '26 08:01

Ákos Vandra-Meyer


1 Answers

Yes, you can use the form of git-diff that takes multiple commits:

git diff <commit> <commit>…​ <commit>

From the documentation:

This form is to view the results of a merge commit. The first listed must be the merge itself; the remaining two or more commits should be its parents.

In your case, you want to compare the merge commit between the rebased branch and origin/master with the merge commit between the old origin/branch and origin/master; the comparison then becomes:

git diff branch origin/master origin/branch

where branch is the "merge commit" and origin/master and origin/branch are its "parents".

In case of a conflict, Git is going to produce the following diff:

- line in origin/master (the first "parent" commit)
- line in origin/branch (the second "parent" commit)
+ line in branch (the merge commit)
like image 91
Enrico Campidoglio Avatar answered Jan 03 '26 07:01

Enrico Campidoglio