Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Git, show all commits that exist *only* on one specific branch, and not *any* others

Tags:

git

Given a branch, I'd like to see a list of commits that exist only on that branch. In this question we discuss ways to see which commits are on one branch but not one or more specified other branches.

This is slightly different. I'd like to see which commits are on one branch but not on any other branches.

The use case is in a branching strategy where some branches should only be merged to, and never committed directly on. This would be used to check if any commits have been made directly on a "merge-only" branch.

EDIT: Below are steps to set up a dummy git repo to test:

git init echo foo1 >> foo.txt git add foo.txt git commit -am "initial valid commit" git checkout -b merge-only echo bar >> bar.txt git add bar.txt git commit -am "bad commit directly on merge-only" git checkout master echo foo2 >> foo.txt  git commit -am "2nd valid commit on master" git checkout merge-only  git merge master 

Only the commit with message "bad commit directly on merge-only", which was made directly on the merge-only branch, should show up.

like image 741
jimmyorr Avatar asked Apr 19 '11 17:04

jimmyorr


People also ask

Which command is used to show all commits?

The git log command displays all of the commits in a repository's history.

Which git command show all commits in the current branch history?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.

How do I find commits between two branches?

In order to see the commit differences between two branches, use the “git log” command and specify the branches that you want to compare. Note that this command won't show you the actual file differences between the two branches but only the commits.


2 Answers

We just found this elegant solution

git log --first-parent --no-merges 

In your example of course the initial commit still shows up.

this answer does not exactly answer the question, because the initial commit still shows up. On the other hand many people coming here seem to find the answer they are looking for.

like image 143
wrtsprt Avatar answered Oct 11 '22 23:10

wrtsprt


Courtesy of my dear friend Redmumba:

git log --no-merges origin/merge-only \     --not $(git for-each-ref --format="%(refname)" refs/remotes/origin |     grep -Fv refs/remotes/origin/merge-only) 

...where origin/merge-only is your remote merge-only branch name. If working on a local-only git repo, substitute refs/remotes/origin with refs/heads, and substitute remote branch name origin/merge-only with local branch name merge-only, i.e.:

git log --no-merges merge-only \     --not $(git for-each-ref --format="%(refname)" refs/heads |     grep -Fv refs/heads/merge-only) 
like image 33
jimmyorr Avatar answered Oct 12 '22 00:10

jimmyorr