Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git diff on topic branch, excluding merge commits that happened in the meantime?

Let's say I have the following situation:

    B---D---F---G topic
   /       /
--A---C---E master

For code review purposes, I would like to pull out a diff from commit A to commit G, but not including commits E and C which happened on the master branch, and also not including commit F which is a merge commit.

In other words, I would like to generate a diff that contains changes from F to G and aggregate those changes with changes from A to D.

In other-other words, I want the review diff to contain only my changes from the topic branch, not including a bunch of code from master that happened in the meantime.

Is this possible? If git cannot handle such "diff aggregations", I would be very thankful if someone can provide some pointers as to how some external command can do that (so that I can try writing a bash script which would do the trick).

like image 293
Vladimir Mitrovic Avatar asked Sep 20 '11 17:09

Vladimir Mitrovic


People also ask

How do you squash commits when merging?

To enable commit squashing as the default option in your repository: Navigate to your chosen repository and open the Settings sub-tab. Open the General Settings page. Check the box for Squash commits on merge default enabled.

Should you keep merge commits?

Some people keep a merge commit, even for a fast forward, because it keeps a very good record of when a branch was completed. These people prefer "complete and accurate" history over a "clean" history. Some people avoid merge commits, even rebasing their branches, because it keeps the history easier to read.

What does git diff head do?

The git diff HEAD [filename] command allows you to compare the file version in your working directory with the file version last committed in your remote repository. The HEAD in the git command refers to the remote repository.

Does merging change both branches?

Merging Branches. Once you've completed work on your branch, it is time to merge it into the main branch. Merging takes your branch changes and implements them into the main branch. Depending on the commit history, Git performs merges two ways: fast-forward and three-way merge.


1 Answers

The answer from Karl is misleading:

git diff never works on proper ranges (i.e. multiple commits), only between two commits!

The range syntax has special meaning for git diff.

git diff E..G is always equal to git diff E G (and
git diff E...G is equal to git diff E G in this case, because E is the merge-base between the two).

git diff master...topic is what you want in this and in the general case: it shows you all changes of the topic branch, comparing it to the last merge-base (i.e. contrasting it to master when it was merged the last time, ignoring all later changes in master which are not in your topic branch anyway).

Note: unfortunately (by coincidence?) the effect of x..y in git log, etc. is best represented by x...y in git diff and vice versa!

like image 157
Robert Siemer Avatar answered Oct 13 '22 00:10

Robert Siemer