DEV Community

Cover image for How to list commits from the current branch.
darker
darker

Posted on

How to list commits from the current branch.

Let's say, you're on your terminal and you want to have the list of commits from your current branch !

There is some tricks to do that, the faster one is just to git log and scroll !
But i found useful to made a simple alias to do so !

First

As you can see, this is quite simple, it's a combination of 2 main commands i set up in my ~/.gitconfig as an alias :

[alias]
    ; to list commits from the current branch
    commit-list = !git log --no-merges --oneline --decorate master..$(git branch --show-current)
Enter fullscreen mode Exit fullscreen mode

HOW

  • First, i need to get the branch where i am right now, and to do that, i use git branch --show-current
  • Second, am going to compare with git log, the difference commits from my branch to the master branch, because i don't want merges, nor too much details, i added two flags, --no-merges and --oneline + a simple --decorate. git log --no-merges --oneline --decorate

DEMO

Demo

PS: Don't worry about the "passphrase" on the demo, i added a lot of security checks for any git command on my local machine, i may do a future post about that soon !

EDIT : From Christophe Colombier, you can do the same thing with a native command, such as : git cherry -v origin

Thanks for reading, feel free to like and/or subscribe for more 🐼.

Top comments (7)

Collapse
 
ccoveille profile image
Christophe Colombier • Edited

alternatives

So here you are trying to provide an alias for something that exist in a very limited way:

git cherry

I don't know why no one use it, but I do.

It won't full achieve your need, but it provides almost the same.

you compute the current branch

you are using git branch --show-current because you want to add it to the master... thing

You could play with stack overflow to get the name of the branch from where the current branch come from.

You will find a lot of outdated thing using rev-parse, symbolic-ref or describe ... I'm not sure which one but it's around this.

But you simply doesn't need it, you can simply use master... that's it it works.

the three dots.

usually two are enough.

Then about the master

your code only works if your default branch is master (it doesn't work when it's main or devel ... )

It also doesn't work when you create a branch from another one, think about a bug fix in a feature branch.

here you can use @{upstream} alias, it works when your branch has an upstream.

it's not about a remote named upstream, but an internal thing.

Here is what I would use

git log --no-merges --oneline --decorate  @{upstream}..
Enter fullscreen mode Exit fullscreen mode

this won't work when you didn't set up an upstream to your branch.

There are way to find the first commit of your branch

conclusion

have fun, I will keep using git cherry -v no one knows

Collapse
 
sanixdarker profile image
darker

Interesting !
I didn't knew about git cherry
As i always say, there is an infinite way to solve a single problem !

I just learned, i can do the same with git cherry -v origin !

Awesome, thank you !

Collapse
 
ccoveille profile image
Christophe Colombier

about the 3 dots vs 2 dots thing

Some Git commands take commit ranges and one valid syntax is to separate two commit names with two dots .., and another syntax uses three dots ....

What are the differences between the two?

Collapse
 
ccoveille profile image
Christophe Colombier

I will make a detail feedback but please provide a code block where you will paste your alias line so people can copy paste them, an image here is frustrating. :P

no rush in fixing it, stay tuned, I will review your command

Collapse
 
sanixdarker profile image
darker

please provide a code block where you will paste your alias line so people can copy paste them, an image here is frustrating

since it's not a huge one, i specified steps commands in the list directly, but you're right !

Collapse
 
ccoveille profile image
Christophe Colombier

always keep in mind how much developers are lazy.

if it cannot copy pasted, it requires efforts.

Thread Thread
 
sanixdarker profile image
darker

😂 agreed !
I added the alias code in the post !