172

Creating a branch for various topics, and not regularly deleting them when I don't need them any more, I have now ended up with about 50 branches.

I tried deleting branches but some of them have unmerged changes.

What I want is the ability to see exactly what changes are there in any branch on my repo that are not in master. Is there a way to do that?

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
lprsd
  • 84,407
  • 47
  • 135
  • 168

5 Answers5

301

To list branches with commits not merged into master:

git branch --no-merged master

To list the relevant commits:

git cherry -v master <branch>
gawi
  • 13,940
  • 7
  • 42
  • 78
  • 9
    "git branch --no-merged master" does not show remote branches - clues to see those too? – Peter Toft Nov 05 '14 at 23:03
  • 34
    Add the `-a` flag to include remote branches. – gawi Nov 06 '14 at 02:38
  • If it's giving u error, try to replace `master` by `origin` as upstream. – CodeFarmer Dec 21 '17 at 02:08
  • 2
    note that branches merged by squash & merge will still show differences, even if there are none. – Michael Scott Asato Cuthbert Dec 20 '18 at 03:21
  • 1
    Adding `-a` parameter will show local and remote branches and with `-r` will show only remote branches. – pafivi Feb 21 '20 at 23:34
  • What do the + and - signs before the commit hash mean? – Robert Niestroj Feb 07 '22 at 16:06
  • @RobertNiestroj (+) Means it needs to be applied to the other branch. (-) Means it can be dropped (because it is already there). See https://git-scm.com/docs/git-cherry: "Here, we see that the commits A and C (marked with -) can be dropped from your topic branch when you rebase it on top of origin/master, while the commit B (marked with +) still needs to be kept so that it will be sent to be applied to origin/master." – sampa Aug 29 '23 at 07:55
40

I came across this question when I was trying to remember the syntax of...

git log <branch> --not master --stat

This will show commits to <branch> that have not been merged to master. The --stat will include the files that were changed with the commits. You can also use this to compare any two branches by replacing master with a different branch name.

Matt
  • 401
  • 4
  • 3
6

This question is already well answered, but there is one more answer I think is worth documenting:

List all commits on any branch not already merged with master:

git log --all --not master

or, equivalently:

git log --all ^master

The --all picks up all branches, so you don't have to list them, then --not master or ^master removes master from the selection.

joanis
  • 10,635
  • 14
  • 30
  • 40
  • 2
    + some formatting for a more elegant output `git log --all --not master --graph --pretty=format:"%C(auto)%h%d%Creset %C(cyan)(%ci)%Creset %C(green)%cn <%ce>%Creset %s" --name-status --date=short` – pavol.kutaj Jul 24 '21 at 04:27
0

For the PowerShell folk a little combination of what has been said above. If needed, replace master with main. Test by pasting into your shell.

$notMergedList = (git branch --no-merged master) -replace " ", ""
ForEach($branchItem in $notMergedList) {
  write-host "~~> branch: $branchItem" -Foregroundcolor DarkCyan
  git cherry -v master $branchItem }
pavol.kutaj
  • 401
  • 1
  • 7
  • 14
-6

It is quite easy to get an overview of your branches with gitk.

tamasd
  • 5,747
  • 3
  • 25
  • 31