When using git, is there a way to show commits made to a branch, while ignoring all commits that were brought in by merging?
I'm trying to review the code changes made on a branch while ignoring the ones we made on other branches that were merged in. I know it's damn near impossible to show a diff in that fashion, but I'd like to be able to find out which commits I need to review.
1 answer. You just need to check the length of the `parents`. If there are two or more commits, it's a merge commit, otherwise it's a regular commit.
THE MERGE COMMIT For instance, when a branch named feature is merged with master, a new commit is created on the branch master which has two parents, the previous head of master and the head of feature. On merging the feature to master. These commands create a new merge commit 1c32600.
Merges a branch into the current branch, creating a merge commit. Use git checkout <target-branch> to switch to the branch into which you want to merge.
A "non-merge" commit is a commit that introduces an actual code change. A merge commit just moves around changes that were already introduced by non-merge commits.
--no-merges
Both parents have equal weight in many contexts in git. If you've always been consistent in merging other changes in then you may find that this gives you what you want.
git log --no-merges --first-parent
Otherwise you may be able to exclude commits from other named branches.
git log --no-merges ^other-branch-1 ^other-branch-2 ^other-branch-3
If you want to review the changes that you are going to merge back into a principal branch then the easiest thing to do is to perform the merge on a local clone and then just look at the diff with the first parent before publishing the merge.
You can use git cherry
for that, it will find you commits that were not yet merged to the upstream, or commits that are on one branch but not the other. So given two branches named "your-branch" and "master":
git cherry -v your-branch master
will present you list of commits compared with their patch id:
+ c3e441bf4759d4aa698b4a413f1f03368206e82f Updated Readme - 2a9b2f5ab1fdb9ee0a630e62ca7aebbebd77f9a7 Fixed formatting + e037c1d90b812af27dce6ed11d2db9454a6a74c2 Corrected spelling mistake
You can notice that commits prefixed by "-" are the ones that appear in both branches, whereas those prefixed with "+" are availble only on your branch.
As an alternative you can use:
git log --pretty=format:"%h %s" your-branch..master --no-merges
which will show you list of commits done on "your-branch" that are not yet present on "master"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With