简体   繁体   English

Git:如何列出跟踪不再存在的远程分支的本地分支?

[英]Git: How do I list local branches that are tracking remote branches that no longer exist?

How can I list any local branches that appear (as per .git/config ) to be tracking remote branches that no longer exist? 如何列出出现的任何本地分支(根据.git/config )来跟踪不再存在的远程分支? Remote branches are on GitHub in this case but I suspect their location has no relevance. 在这种情况下,远程分支机构在GitHub上,但我怀疑它们的位置没有相关性。

For example: 例如:

  1. I have local branches, a , b , c and d . 我有本地分支, abcd
  2. a is tracking origin/a and c is tracking origin/c . a是跟踪origin/ac是跟踪origin/c
  3. b and d are not tracking remote branches. bd不跟踪远程分支。
  4. origin/a has been been merged back into master and was deleted during a repository clean-up; origin/a已合并回master,并在存储库清理期间被删除; I no longer need to keep local branch a . 我不再需要保留本地分支a
  5. If local branch a is checked out to the working tree, running git fetch or git pull results in the error Your configuration specifies to merge with the ref 'a' from the remote, but no such ref was fetched. 如果将本地分支a签出到工作树,则运行git fetchgit pull导致错误。 Your configuration specifies to merge with the ref 'a' from the remote, but no such ref was fetched.

How would I produce the list containing only a and any other local branches that appear to be tracking remote branches that no longer exist? 如何生成仅包含a和任何其他本地分支的列表,这些分支似乎跟踪不再存在的远程分支?

I would like to identify these so that I can delete obsolete local branches I no longer need. 我想识别这些,以便我可以删除我不再需要的过时的本地分支。

The list should not include local branches b or d that are not tracking remote branches, and also not c that is tracking origin/c , which still exists. 该列表不应包括本地分支bd中未跟踪远程分支机构,并且也不c被跟踪origin/c ,其仍然存在。

If your local branches are tracking the remote branch you can filter the list of branches so show the ones that do not have a tracking branch with: 如果您的本地分支机构正在跟踪远程分支,您可以过滤分支列表,以便显示没有跟踪分支的分支:

git branch -vv | grep -v origin

This will provide some extra information about the last commit that is on the branch but you can filter that out 这将提供有关分支上最后一次提交的一些额外信息,但您可以对其进行过滤

git branch -vv | grep -v origin | awk '{print $1}'

This will only print the name of the branch that isn't tracking a remote branch. 这将仅打印未跟踪远程分支的分支的名称。

git for-each-ref refs/heads --format='%(refname:short) %(upstream)' \
| awk 'NF==1'

will do it. 会做的。 NF is awk's "number of fields", and its default action is print. NF是awk的“字段数”,其默认操作是print。

To list your local branches which track deleted remote branches you can use git remote prune --dry-run 要列出跟踪已删除远程分支的本地分支,您可以使用git remote prune --dry-run

For example (assuming your remote repository is named origin ): 例如(假设您的远程存储库名为origin ):

git fetch
git remote prune origin --dry-run

You can remove the --dry-run option to delete the branches from your local repository 您可以删除--dry-run选项以从本地存储库中删除分支

LANG=en git branch --format='%(if:equals=gone)%(upstream:track,nobracket)%(then)%(refname:short)%(end)' | grep '.'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM