Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing commits made directly to a branch, ignoring merges in Git

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.

like image 718
Channel Cat Avatar asked Dec 15 '11 21:12

Channel Cat


People also ask

How do I know if a commit is merged?

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.

What happens to commits after merge?

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.

Can you commit to a merged branch?

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.

What is a non merge commit?

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.


2 Answers

--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.

like image 101
CB Bailey Avatar answered Oct 12 '22 13:10

CB Bailey


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"

like image 35
jbochniak Avatar answered Oct 12 '22 13:10

jbochniak