28

I am using Git for my project and trying to follow best practice:

  1. I work on a topic branch
  2. When ready, I merge the topic branch into my dev branch using git merge --squash. This keeps my dev branch clean.
  3. Whenever the dev branch is stable and the team decides it's time for a release, we merge the dev branch into the master branch, without using squash, and tag that commit as a version release.

This should keep our history, and using gitk, we can see where all of the commits come in. However, I want to be able to see only the commits applied to the master branch. I have tried:

git log master
git show-branch

None of these show just the history of the master branch. Is there a way to easily do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Recarey
  • 20,178
  • 4
  • 25
  • 22

3 Answers3

24

If I'm understanding you correctly, you want to see the merges back into master, but not the history of those merges. I believe that:

git log --merges

will give you what you want.

UPDATE: Adding --first-parent should fix this from the sounds of it.

git log --merges --first-parent

--first-parent

Follow only the first parent commit upon seeing a merge commit.

This option can give a better overview when viewing the evolution of a particular topic branch, because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual commits brought in to your history by such a merge.

James Kovacs
  • 11,549
  • 40
  • 44
  • 1
    Almost, but unfortunately this also shows me merges that occurred in the *dev* branch. I only want to see merges from *dev* into *master* – Alex Recarey Jan 07 '11 at 19:29
  • 2
    first parent isn't necessarily where the branch has been before, it's determined during the merge (the branch in which the merge happened). If you fast-forward to a merge, the first parent is the other branch. – steabert Feb 13 '13 at 14:35
10

Unfortunately, Git does not store branch information for a commit, and commits do not belong to a branch. Branches in Git are just "moving tags" in a course of commits and NOT a sequence of commits as one would expect.

So basically you cannot show commits that belong to a branch since there is no such concept in Git.

Kostas
  • 1,292
  • 1
  • 13
  • 20
  • Then what is the --branches option for the log command? (Git stores history, and the parentage history from a commit is always linear. So, effectively, the concept of branch exists.) – Peter Constable Sep 25 '20 at 17:57
3

Since Git does not store information about which branch is nascent from which other, there is no automatic way to guess which branch you could possibly want to be shown.

In that regard, --first-parent will not ultimately help, especially since it is easy to have more than one master, for example. Consider:

wc1$ git clone git://shared.com/repo
wc1$ (hack code, git commit)
wc2$ git clone git://shared.com/repo
wc2$ (hack code, git commit, git push somewhere)
wc1$ git fetch origin; git merge origin/master; git push somewhere master;

(Feel free to take a random project and do this exercise.) Graph it. So you cannot meaningfully graph "just one branch", even if the commits were tagged with the name of the branch they were made on (because both are master).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user562374
  • 3,817
  • 1
  • 22
  • 19
  • 1
    Suppose in situations like this where multiple branches were called 'master', that you wanted to see all of them. (In fact, when I've wanted something similar to the original question here, that's usually exactly what I've wanted - there have been multiple histories, and I've wanted to see all of them.) So the answer to "which branch you could possibly want" would be "all of them" - there's no need to resolve it to just one. – Ian Griffiths Nov 19 '12 at 08:49