Git diff between current branch and master but not including unmerged master commits

Viewed 334091

I want a diff of all changes in a branch that is not merged to master yet.

I tried:

git diff master
git diff branch..master
git diff branch...master

However, in each of these cases the diff contains content in master that has not been merged into my branch yet.

Is there a way to do a diff between my branch and master that excludes changes in master that have not been merged into my branch yet?

5 Answers

Here's what worked for me:

git diff origin/master...

This shows only the changes between my currently selected local branch and the remote master branch, and ignores all changes in my local branch that came from merge commits.

As also noted by John Szakmeister and VasiliNovikov, the shortest command to get the full diff from master's perspective on your branch is:

git diff master...

This uses your local copy of master.

To compare a specific file use:

git diff master... filepath

Output example:

Example usage

According to Documentation

git diff Shows changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes resulting from a merge, changes between two blob objects, or changes between two files on disk.

In git diff - There's a significant difference between two dots .. and 3 dots ... in the way we compare branches or pull requests in our repository. I'll give you an easy example which demonstrates it easily.

Example: Let's assume we're checking out new branch from master and pushing some code in.

  G---H---I feature (Branch)
 /
A---B---C---D master (Branch)
  • Two dots - If we want to show the diffs between all changes happened in the current time on both sides, We would use the git diff origin/master..feature or just git diff origin/master
    ,output: ( H, I against A, B, C, D )

  • Three dots - If we want to show the diffs between the last common ancestor (A), aka the check point we started our new branch ,we use git diff origin/master...feature,output: (H, I against A ).

  • I'd rather use the 3 dots in most circumstances.

Put git diff between branches/commitIDs to one patch file (it is ok only if no files was deleted or created) and then apply it in the new branch.

git checkout -b new-branch-name
git diff abcde123..branch-name > branch-name.patch
git apply branch-name.patch
rm branch-name.patch
git add .
git commit -m '#123 commit msg’
git push

where abcde123 is commit ID in master where no unwanted changes present yet.

Basically, you do not need master for that. You can compare current commit to any commit in your current branch, e.g. the last commit before you started changes.

git diff a4488d20406a1f26888d80eb7d57aa3b0054e307
or
git diff 553e4c7dcdcf366917d36ad8f28a273c88d92a1b..a4488d20406a1f26888d80eb7d57aa3b0054e307
Related