Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git diff of a branch, excluding commits in other branch

Tags:

git

diff

To do code review, I often make a git difftool hash_of_parent_of_first_commit_in_branch last_commit_in_branch of a feature branch. The feature_branch was created from its source (let's call it master branch) and work was done on it. But in the middle of this time, the master branch received some commits, and these commits were merged in the feature_branch.

To clarify, if I was using git log, this command would be simple: git log feature_branch ^master (maybe with a --no-merges or not, depending on the case). With this I would have the master commits filtered out of the feature_branch.

I did some research but was not able to figure out how could I achieve this. Maybe using cherry-pick? Any ideas on how could I achieve something similar on the diff/difftool command?

like image 539
jademcosta Avatar asked Sep 16 '25 19:09

jademcosta


1 Answers

Firstly, you can get a list of suitable commits like so:

git rev-list --reverse --no-merges feature_branch ^master

this gets every non-merge commit on feature_branch that isn't reachable from master. The reverse puts them in chronological order, suitable for application.

Secondly, you can cherry-pick these to a temporary branch:

git branch review $(git merge-base feature_branch master)
git checkout review
git rev-list --reverse --no-merges feature_branch ^master | xargs git cherry-pick

and then your review will be on

git diff $(git merge-base feature_branch master)..review

Note the xargs may make it hard to resolve cherry-pick problems interactively. You could write a wrapper script to do all this using a for loop instead of xargs to keep everything connected nicely to the terminal though.

like image 155
Useless Avatar answered Sep 19 '25 11:09

Useless