567

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Highway of Life
  • 22,803
  • 16
  • 52
  • 80

10 Answers10

736

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
Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
  • 130
    Perfect! `git log --no-merges master..` was exactly what I needed. – Highway of Life Jan 10 '11 at 17:13
  • 6
    @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 merge – rynmrtn Apr 08 '13 at 20:16
  • 14
    How about a way to do this that doesn't require me to type/know the parent-branch? :) – ELLIOTTCABLE May 20 '13 at 22:40
  • 4
    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..` – Noah Sussman Aug 08 '13 at 00:57
  • 2
    shouldn't not read? git log master.. --oneline --no-merges – Alex Nolasco Jun 04 '14 at 21:31
  • 1
    `git log upstream/master..` worked for me - it shows exactly the commits GitHub shows for a PR (on the Commits tab in the UI). – flow2k Jul 25 '17 at 00:07
  • what does .. means? – Mohammad Salehi Aug 13 '18 at 10:29
  • 2
    @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 `..` – Wayne Conrad Aug 13 '18 at 13:52
  • Why can't I see the HEAD when I do this? I've checked out the branch I'm looking at. – Adrian Apr 18 '19 at 00:36
  • @Adrian 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. – Wayne Conrad Apr 18 '19 at 02:37
  • How do I have the log include changed files for each commit? – Scorb Mar 03 '20 at 17:17
  • @ScottF Add the `--name-status` switch to the `git log` command. – Wayne Conrad Mar 03 '20 at 17:32
  • @ELLIOTTCABLE https://stackoverflow.com/questions/68415444/locate-starting-point-of-git-branch. – young_souvlaki Jul 16 '21 at 21:32
  • I tried "git cherry -v development", since my branches are created from development, and nothing was printed – moodymudskipper Jan 19 '23 at 10:45
192

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.

Danijel
  • 8,198
  • 18
  • 69
  • 133
yerlilbilgin
  • 3,041
  • 2
  • 26
  • 21
  • 41
    `--first-parent ` is the option. works ! – parasrish Dec 01 '16 at 05:41
  • 3
    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. – Ed Randall Feb 13 '17 at 12:12
  • 3
    `--first-parent ` 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: – rachel Oct 12 '17 at 19:30
  • 2
    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. – rachel Oct 12 '17 at 19:35
  • I wonder if all the options are relevant to the question asked! –  Sep 21 '21 at 12:20
40

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'
Dyaniyal Wilson
  • 1,012
  • 10
  • 14
  • 2
    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. – Yordan Grigorov Apr 14 '22 at 09:16
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^!
GPHemsley
  • 535
  • 1
  • 7
  • 18
  • 1
    Can you explain how that works? what to the leading "^" trailing "^" and "!" mean? – Sam Hasler Oct 25 '21 at 12:03
  • 1
    @SamHasler Caret prefix and carat/exclamation point suffix are described in the [gitrevisions documentation](https://git-scm.com/docs/gitrevisions#_specifying_ranges). In this case, `^master^!` means exclude the commit at the head of the master branch as well as all its parents/ancestors. – GPHemsley Jun 02 '22 at 21:49
13

just run git log origin/$BRANCH_NAME

Mahdi Pedram
  • 689
  • 6
  • 12
  • I'm not sure what all the complex queries are for above. This is the simplest and most logical solution. – GHOST-34 Mar 03 '21 at 17:22
  • 1
    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. – Alex Mar 12 '21 at 07:06
  • 1
    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. – Marcos Buarque Sep 22 '22 at 20:08
1

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

unhammer
  • 4,306
  • 2
  • 39
  • 52
  • Thoses switches are not present in latest magit (Febr 20210) it seems – michieljoris Mar 01 '21 at 03:41
  • 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: https://github.com/magit/magit/blob/3db9e0a9821e5d9d9c6aacf5cc1d8ae1a6546ba7/lisp/magit-log.el#L414..L415 – unhammer Mar 01 '21 at 14:44
  • 1
    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. – michieljoris Mar 02 '21 at 21:24
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.
neoneye
  • 50,398
  • 25
  • 166
  • 151
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

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
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

NeX
  • 1
-3
git log $(git branch --show-current)
CodeFarmer
  • 2,644
  • 1
  • 23
  • 32
  • 4
    That's exactly equivalent to `git log` and does not filter out the common commits between the current and parent branches. – joanis Dec 03 '21 at 15:04