Search code examples
gitgit-branchgit-remote

How to list branches which are in remote A but not in remote B with git?


For example if I have a "main" upstream repository where I develop, and a side-repository where I backup my work after a finished work-day - now I want to delete branches on the side-repository which are not in the "main" repository are anymore, because they are merged (or whatever).

How to list branches which exist in the "side" remote but not in the "main" remote?


Solution

  • UPDATE - incorporated feedback from torek UPDATE 2 - small but potentially important change to suggested scripting


    Well, git doesn't think in terms of a branch existing in multiple repos. You have branches in your local repo, and you have snapshots of the branches in any given remote... and the local branch may have the same name as a branch on the remote, but it still isn't "the same branch" to git.

    Only tracking provides something of an illusion that "the same branch" is in two repos. (And only as a matter of convention is it typical to have the local branch named the same as the remote branch.) This, too, is limited in that you can't have one local branch track two remote branches...

    Which is a long way of saying, I don't think git alone can do what you're asking; you'll likely need to post-process some git output in some way.

    You can get the list of branches that are in a remote by saying

    git for-each-ref refs/remotes/<remote-name>
    

    You could use cut to get just the basic branch name (cut -d\/ -f 4-). (Note that trailing -, which is important if/only if you have branch names containing /.) Then sort. Then feed to comm to see which lines appear in both, or in only one.