How do I run git log to see changes only for a specific branch?
Asked Answered
R

10

586

I have a local branch tracking the remote/master branch. After running git-pull and git-log, the log will show all commits in the remote tracking branch as well as the current branch. However, because there were so many changes made to the remote branch, I need to see just the commits made to the current local branch.

What would be the Git command to use to only show commits for a specific branch?

Notes:

Configuration information:

[branch "my-branch"]
  remote = origin
  merge = refs/heads/master
Rau answered 10/1, 2011 at 17:4 Comment(1)
One liner in git bash for counting number of commits: git log remotes/origin/feature --oneline | wc -lBikaner
T
758

Using git log

Assuming that your branch was created off of master, then while in the branch (that is, you have the branch checked out):

git log master..

If you are not in the branch, then you can add the branch name to the "git log" command, like this:

git log master..branchname

If your branch was made off of origin/master, then say origin/master instead of master.

Goofy alternative using "cherry"

You can make the "cherry" command do this as well, although the output is not as useful. While in a branch, do this to find out what commits are in the branch that are not in master:

git cherry -v master
Threat answered 10/1, 2011 at 17:7 Comment(14)
Perfect! git log --no-merges master.. was exactly what I needed.Rau
@HighwayofLife: --no-merges may appear that it's only showing commits from a specific branch, but it's really only showing commits that did not result in a mergePyrogallol
How about a way to do this that doesn't require me to type/know the parent-branch? :)Fluoro
To get a sense of rate-of-change, I used the following incantation :) which produces a one-line log format with the author's name shown first, followed by the relative age of the commit: git log --no-merges --pretty='%C(yellow)%h%d %Creset%an %Cgreen%ar:%Creset %s' --graph master..Recoil
shouldn't not read? git log master..<your branch> --oneline --no-mergesPejoration
git log upstream/master.. worked for me - it shows exactly the commits GitHub shows for a PR (on the Commits tab in the UI).Vinaigrette
what does .. means?Flameproof
@MohammadSalehi .. is being used as a range operator. It is used when one wants to specify not a single commit, but instead a range of commits. man gitrevisions will show you more about how git revisions can be specified, including using ..Threat
Why can't I see the HEAD when I do this? I've checked out the branch I'm looking at.Missilery
@Missilery I don't know. If you can create a formal question with an appropriate level of detail, perhaps someone will be able to figure out why.Threat
How do I have the log include changed files for each commit?Zito
@ScottF Add the --name-status switch to the git log command.Threat
@Fluoro #68415944.Suanne
I tried "git cherry -v development", since my branches are created from development, and nothing was printedMidsection
A
202

Use:

git log --graph --abbrev-commit --decorate  --first-parent <branch_name>

It is only for the target branch (of course --graph, --abbrev-commit --decorate are more polishing).

The key option is --first-parent: "Follow only the first parent commit upon seeing a merge commit" (https://git-scm.com/docs/git-log)

It prevents the commit forks from being displayed.


The <branch_name> part doesn't have to be a branch name. It can be a commit ref, a tag, any reference, (e.g. HEAD or HEAD~1..)

Annulet answered 20/9, 2016 at 6:19 Comment(6)
--first-parent <branch_name> is the option. works !Jere
Where branch-name is what, the branch whose history I only want to see? On a branch with only 3 changes, this is showing me hundreds.Eklund
--first-parent <branch_name> also worked for me! I ended up with an alias to git log --first-parent $current_branch_name --no-merges. In response to @EdRandall, it will show the commits on the branch + those from where it was branched. For example:Stalinist
Oops, I submitted to soon, here's the example: You have commits A and B on master. You branch new_feature from master. You add commits C and D to it. Then you add E and F to master. You then merge master to new_feature. Using git log on new_feature, you'll see "merge master", F, E, D, C, A, B. Using git log --first-parent new_feature --no-merges, you'll see D, C, A, B.Stalinist
I wonder if all the options are relevant to the question asked!Bandicoot
git log --first-parent origin/<branch_name> to be more explicit.Cryobiology
P
42

If you want only those commits which are done by you in a particular branch, use the below command.

git log branch_name --author='Dyaniyal'
Peden answered 3/2, 2018 at 10:10 Comment(1)
if you are in a hurry, you could type: git log --author=Dya assuming that you are the only author whose name starts with Dya.Schoonmaker
A
15

just run git log origin/$BRANCH_NAME

Adair answered 10/10, 2019 at 21:1 Comment(3)
I'm not sure what all the complex queries are for above. This is the simplest and most logical solution.Thormora
But this doesn't show clean history of commits - it show all the commits that ended in origin/$BRANCH_NAME. I do not think this is what author of the question meant.Mou
This is useful if you merged master into target branch locally, and still haven't pushed it to the repo (origin). In this scenario, yes, this looks like the simplest solution.Joni
P
14

The problem I was having, which I think is similar to this, is that master was too far ahead of my branch point for the history to be useful. (Navigating to the branch point would take too long.)

After some trial and error, this gave me roughly what I wanted:

git log --graph --decorate --oneline --all ^master^!
Pyxidium answered 17/9, 2017 at 8:41 Comment(2)
Can you explain how that works? what to the leading "^" trailing "^" and "!" mean?Drees
@SamHasler Caret prefix and carat/exclamation point suffix are described in the gitrevisions documentation. In this case, ^master^! means exclude the commit at the head of the master branch as well as all its parents/ancestors.Pyxidium
E
2

For those using Magit, hit l and =m to toggle --no-merges and =pto toggle --first-parent.

Then either just hit l again to show commits from the current branch (with none of commits merged onto it) down to end of history, or, if you want the log to end where it was branched off from master, hit o and type master.. as your range:

enter image description here

Endomorph answered 26/11, 2020 at 12:23 Comment(3)
Thoses switches are not present in latest magit (Febr 20210) it seemsAleasealeatory
I'm running magit-20210215.908 from melpa and they're still there, and they show up in the source of master as of today: github.com/magit/magit/blob/…Endomorph
As in indeed they are in the version I'm running. However, as it turns out, doomemacs set transient-default-level to 5, hiding a number of switches. (setq transient-default-level 7) restores all switches.Aleasealeatory
Z
1

On the develop branch, and wanting to see a list of recent PR's.

PROMPT> git log --first-parent --pretty=oneline 0a805f46957a957161c5ed4e08081edeed9b91e7..6aae236484dc1881f5dbdef0f529eb95c6ef7bd5
0a805f46957a957161c5ed4e08081edeed9b91e7 Merged PR 1984/3: Fixed crash 2.
8d51abb53403e10e6484dc3c0569a5792f1x3734 Merged PR 1984/2: Fixed crash 1.
6aae236484dc1881f5dbdef0f529eb95c6efcbd5 Merged PR 1984/1: Downgraded from windows 11 to windows 3.11.
Zaratite answered 28/6, 2021 at 13:53 Comment(0)
Y
0

This is a weasel new age GUI sideways answer, but anyways, in Xcode there is apparently a visual tool for that.enter image description here

Yaron answered 27/4, 2022 at 15:48 Comment(0)
M
0

I modified the proposed answer to include lg1 look-and-feel.

[alias]
lg1b = "!cd -- \"${GIT_PREFIX:-.}\" && CURRENT_BRANCH=$(git branch --show-current) && git log $CURRENT_BRANCH --graph --abbrev-commit --pretty='format:%C(bold blue)%h%C(reset) %C(bold green)(%ar)%C(reset) %s %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --decorate --not $(git for-each-ref --format='%(refname)' refs/heads/ | grep -v refs/heads/$CURRENT_BRANCH) #"

refer to this for lg1 command : Pretty Git branch graphs

Moneymaking answered 4/8, 2022 at 17:12 Comment(0)
Z
-3
git log $(git branch --show-current)
Zelmazelten answered 2/12, 2021 at 5:24 Comment(1)
That's exactly equivalent to git log and does not filter out the common commits between the current and parent branches.Committeewoman

© 2022 - 2024 — McMap. All rights reserved.