How to get commit history for just one branch?

GitGit Log

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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.

Solution 3 - Git

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

Solution 4 - Git

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.

Solution 5 - Git

You can use only git log --oneline

Solution 6 - Git

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 fianlly clean up the output by removing everything preceding [$BRANCH_NAME] with sed -E "s/^[^\[]*?\[/[/".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarplesoftView Question on Stackoverflow
Solution 1 - GitalexView Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - GitcforbishView Answer on Stackoverflow
Solution 4 - GitDaniel Casas-OrozcoView Answer on Stackoverflow
Solution 5 - GitManoj RanaView Answer on Stackoverflow
Solution 6 - GitMensSanaView Answer on Stackoverflow