ChatGPT解决这个技术问题 Extra ChatGPT

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

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
One liner in git bash for counting number of commits: git log remotes/origin/feature --oneline | wc -l

W
Wayne Conrad

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

git cherry -v master

or

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.


Perfect! git log --no-merges master.. was exactly what I needed.
@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
How about a way to do this that doesn't require me to type/know the parent-branch? :)
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..
shouldn't not read? git log master.. --oneline --no-merges
D
Danijel

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.


--first-parent <branch_name> is the option. works !
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.
--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:
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.
I wonder if all the options are relevant to the question asked!
D
Dyaniyal Wilson

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'

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.
G
GPHemsley

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^!

Can you explain how that works? what to the leading "^" trailing "^" and "!" mean?
@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.
M
Mahdi Pedram

just run git log origin/$BRANCH_NAME


I'm not sure what all the complex queries are for above. This is the simplest and most logical solution.
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.
n
neoneye

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.

u
unhammer

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:

https://i.stack.imgur.com/cLpR9.gif


Thoses switches are not present in latest magit (Febr 20210) it seems
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/…
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.
A
Anton Tropashko

https://i.stack.imgur.com/KJCdv.png


C
CodeFarmer
git log $(git branch --show-current)

That's exactly equivalent to git log and does not filter out the common commits between the current and parent branches.