Home > Software engineering >  Show all commits without upstream branch
Show all commits without upstream branch

Time:09-16

I want to show all my local commits on my branch even when I don't have an upstream branch. If I have an upstream in place I usually use:

$ git log @{u}..$(git branch --show-current)

But this does not work if there is no upstream branch

I've also tried this answer from Aleksander Monk:

$ git log --branches --not --remotes

Which is close to what I want but this shows me all the local commits on all branches. I'd want to filter this to show my commits only for current branch.

Is there a way to pipe this or is there another way?

CodePudding user response:

Short version : try

git log HEAD --not --remotes

More details :

The trick here is that without an upstream set, it's hard to know when to stop. The usual version—for which you don't need $(git branch --show-current)—is to use @{u}.. or @{u}..HEAD (whichever you prefer), which means start from HEAD, i.e., the current commit and stop when reaching any commit reachable from the upstream. But that uses "the upstream".

The trick there—how to "know when to stop"—is to recast your formulation, just as you did:

I've also tried this answer from Aleksander Monk:

$ git log --branches --not --remotes

Which is close to what I want but this shows me all the local commits on all branches. I'd want to filter this to show my commits only for current branch.

The problem with this version is that its positive reference is --branches, which means all branch names. Its negative reference is --remotes (all remote-tracking names) and that's what you want; but you want HEAD as the positive reference. So just swap in HEAD for --branches:

git log HEAD --not --remotes

and you're set.

(Note that when using @{u}.. you can completely omit HEAD as it's implied by the two dots, but for this particular command, you can't omit HEAD at all. You can, however, use the one-character short name @ to mean HEAD, if you prefer.)

CodePudding user response:

It sounds like you're looking for the default behavior of git log (without any flags) on your branch.

  • Related