In Git, list names of branches with unpushed commits

GitBranch

Git Problem Overview


Given a project with several local branches, each tracking some remote branch, is there a command that lists all branches that have unpushed commits? (That is, even if none of those branches are checked out.)

I don't want to see the commits themselves, nor do I want to see branches that are up-to-date, I just want to see which branches are ahead of their remotes.

I have tried git log --branches --not --remotes --simplify-by-decoration --decorate --oneline, but it doesn't seem to show what I need. Running it on my current repo gives no output, but running git status on my current branch shows Your branch is ahead of 'origin/branchname' by 2 commits.

git for-each-ref --format="%(refname:short) %(push:track)" refs/heads and git branch -v both show branches that are up to date as well as ones that need pushing. However, they do both show my current branch as [ahead 2].

Other commands I have found eg. git log @{u}.., git cherry -v list the commits themselves, not the branches.

Side question: why would the output from git log --branches --not --remotes --simplify-by-decoration --decorate --oneline not include branches that git branch -v shows as ahead? Isn't the former command just looking at which refs/heads do not correspond to a known remote; so wouldn't a branch listed as [ahead 2] meet this criteria?

Git Solutions


Solution 1 - Git

The --no-walk option to log seems to do a better job of what I need than --simplify-by-decoration. My full command is:

git log --branches --not --remotes --no-walk --decorate --oneline

...which I've aliased to unpushed.

Solution 2 - Git

git for-each-ref --format="%(refname:short) %(push:track)" refs/heads

That remain the most precise answer that you can easily parse/grep to get the desired output (like removing up-to-date branches)

You can do so in a bash script that you will call git-xxx (no extension), somewhere in your $PATH or %PATH%.
That script can then be called with git xxx, and will use git bash.
That is portable and will work across platforms (meaning even on Windows, where <Git For Windows>/usr/bin includes 200+ linux commands (grep, sed, awk, xargs, ...)

Solution 3 - Git

You can also see what branches are not yet merged to master

git checkout master

and then

git branch --no-merged

Solution 4 - Git

although the answers above are very helpful and show alot of data but for others coming here looking for a solution to find local branches which are ahead i.e. have not been pushed yet. can get the exact list by executing:

git branch -v | grep ahead

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondetlyView Question on Stackoverflow
Solution 1 - GitdetlyView Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - GitDanonView Answer on Stackoverflow
Solution 4 - GitZain BalochView Answer on Stackoverflow