186

Let's say I created a new branch my_experiment from master and made several commits to my_experiment. If I do a git log when on my_experiment, I see the commits made to this branch, but also the commits made to master before the my_experiments branch was created.

I would find it very useful to see the history of all commits to the my_experiments branch until it hits the creation of that branch - effectively a true history of just that branch. Otherwise it's not clear to me when looking through the log whether the commits were on the my_experiments branch or not.

Is there a way to do this with Git?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marplesoft
  • 6,030
  • 4
  • 38
  • 46

6 Answers6

221

You can use a range to do that.

git log master..

If you've checked out your my_experiment branch. This will compare where master is at to HEAD (the tip of my_experiment).

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
alex
  • 479,566
  • 201
  • 878
  • 984
  • 3
    Ok spoke too soon. That did it for my simple example. But now I'm looking at somebody else's real repo and it occurs to me that in order to use this command properly, I'd need to know what branch my current branch was created from. Maybe I should be able to tell this from gitk but it's not obvious to me. Any thoughts? – Marplesoft Jun 07 '13 at 00:20
  • @Marplesoft It can be complicated to figure that out. See [this question](http://stackoverflow.com/q/1527234/31671). – alex Jun 07 '13 at 00:48
  • 1
    Could you explain briefly how does it work? What does `git log master..` tell to Git? – tonix Nov 16 '17 at 09:51
  • 2
    @tonix Follow the link in the answer for more information on how a *range* works (the `master..`) part. – alex Nov 16 '17 at 10:45
  • If your current branch was created from develop, use `git log develop..` ; if it was created from main, use `git log main..`, and so on. – kotchwane Oct 21 '21 at 10:20
18

Note: if you limit that log to the last n commit (last 3 commits for instance, git log -3), make sure to put a space between 'n' and your branch:

git log -3 master..

Before Git 2.1 (August 2014), this mistake: git log -3master.. would actually show you the last 3 commits of the current branch (here my_experiment), ignoring the master limit (meaning if my_experiment contains only one commit, 3 would still be listed, 2 of them from master)

See commit e3fa568 by Junio C Hamano (gitster):

revision: parse "git log -<count>" more carefully

This mistyped command line simply ignores "master" and ends up showing two commits from the current HEAD:

$ git log -2master

because we feed "2master" to atoi() without making sure that the whole string is parsed as an integer.

Use the strtol_i() helper function instead.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • last command gives `fatal: '2master': not a non-negative integer`, so I think there is no need to mention it anymore.. – Bersan Dec 06 '22 at 10:18
  • @Bersan I mention it because that was the case for Git older than 2.1. – VonC Dec 06 '22 at 12:05
10

The git merge-base command can be used to find a common ancestor. So if my_experiment has not been merged into master yet and my_experiment was created from master you could:

git log --oneline `git merge-base my_experiment master`..my_experiment
cforbish
  • 8,567
  • 3
  • 28
  • 32
  • If you want to include the parent of your first commit, add **^** and **--first-parent** : `git log --oneline \`git merge-base my_experiment master\`^..my_experiment --first-parent` – Eric Lavoie Dec 12 '17 at 19:02
  • Quick note: You don't really need ` --oneline` unless there are too many commits and you want to condense the total number of lines in the output. – Tapa Dipti Sitaula Jul 06 '23 at 08:01
3

I think an option for your purposes is git log --oneline --decorate. This lets you know the checked commit, and the top commits for each branch that you have in your story line. By doing this, you have a nice view on the structure of your repo and the commits associated to a specific branch. I think reading this might help.

B--rian
  • 5,578
  • 10
  • 38
  • 89
2

You can use only git log --oneline

Manoj Rana
  • 3,068
  • 1
  • 24
  • 34
  • 2
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – undetected Selenium Mar 28 '19 at 15:24
0

I know it's very late for this one... But here is a (not so simple) oneliner to get what you were looking for:

git show-branch --all 2>/dev/null | grep -E "\[$(git branch | grep -E '^\*' | awk '{ printf $2 }')" | tail -n+2 | sed -E "s/^[^\[]*?\[/[/"
  • We are listing commits with branch name and relative positions to actual branch states with git show-branch (sending the warnings to /dev/null).
  • Then we only keep those with our branch name inside the bracket with grep -E "\[$BRANCH_NAME".
  • Where actual $BRANCH_NAME is obtained with git branch | grep -E '^\*' | awk '{ printf $2 }' (the branch with a star, echoed without that star).
  • From our results, we remove the redundant line at the beginning with tail -n+2.
  • And then, we finally clean up the output by removing everything preceding [$BRANCH_NAME] with sed -E "s/^[^\[]*?\[/[/".
MensSana
  • 521
  • 1
  • 5
  • 16
  • This doesn't work if the branch has been merge into master. It only reports history back to the merge point, not all the way back to the creation of the branch. I believe it's not possible to get back beyond the closest merge point, given the reflog can be gc'd. – user98761 Jan 17 '21 at 23:41
  • well, you can always use this [amazing perl script](https://github.com/jwiegley/git-scripts/blob/master/git-forest) from [Jan Engelhardt](https://github.com/jengelh). I use it very often with `--all --reverse` params... – MensSana Feb 16 '21 at 14:09