修剪上游不再存在的原始分支

Prune origin branches that do not exist on upstream anymore

这些分支在上游

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/gh-pages
  remotes/origin/master
  remotes/origin/next
  remotes/origin/translation
  remotes/origin/v1.0.1
  remotes/origin/videoConverter

这些是 我的分支

上的分支
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/beta
  remotes/origin/dashboardNewContent
  remotes/origin/externalUrl
  remotes/origin/gh-pages
  remotes/origin/master
  remotes/origin/next
  remotes/origin/rc
  remotes/origin/rc-0.3.0
  remotes/origin/release-candidate
  remotes/origin/streama-node
  remotes/origin/translation
  remotes/origin/v1.0.0
  remotes/origin/videoConverter
  remotes/upstream/gh-pages
  remotes/upstream/master
  remotes/upstream/next
  remotes/upstream/translation
  remotes/upstream/v1.0.1
  remotes/upstream/videoConverter

上游仓库在我 fork 之后已经更新了。我找不到从我的分支中删除上游不再存在的分支的方法。 This GitHub tutorial 显示如何同步提交历史。

这个问题有直接的解决方案吗?

一个技巧是用 upstream url:

替换本地仓库(引用你的分支)中的 origin
git remote rename origin ori
git remote add origin $(git remote get-url upstream)

然后单个命令git fetch --prune将在本地删除所有过时的远程跟踪分支(不在upstream中的remotes/origin/xxx,暂时由'origin'引用)

最后,您可以为每个 remotes/ori/xxx 分支检查 remotes/origin/xxx 是否存在。如果不是:git push --delete ori xxx

类似于:(在 bash 中)

for f in $(git for-each-ref refs/remotes/ori --format '%(refname:short)' |
       sed 's,ori/,,') do;
   git rev-parse -q --verify refs/remotes/origin/$f >/dev/null || 
      git push --delete ori ${f}
done

该脚本可以放在 git-cleanup(或任何 git-xxx 名称中),并用 git cleanup(或 git xxx)调用。这样,即使在 Windows.

上,它也会在自己的 bash 中运行

待完成:

git remote remove origin
git remote rename ori origin