简体   繁体   English

删除所有已推送的git分支

[英]Delete all git branches already pushed

My need 我的需要

Hi, I'm looking for a command to delete all local branches already pushed to origin. 嗨,我正在寻找一个命令来删除所有已经推送到原始位置的本地分支。

I especially want to keep all branches with commits not pushed yet to their respective remote branches. 我特别想保留所有尚未提交的分支到它们各自的远程分支。

Reason 原因

git prune does a part of the job by clearing branches while the remote is deleted, but as I have many feature branches I would need to keep only the branches which have not been fully pushed to remote, to avoid having a long list of local branches in my repo, restricting them to those actually in works. git prune通过删除远程站点时清除分支来完成一部分工作,但是由于我有很多功能分支,因此我需要仅保留尚未完全推送到远程的分支,以避免本地分支的列表过长在我的仓库中,将它们限制为实际工作中的对象。

Thank you! 谢谢!

git remote update # `git fetch --all` but it allows configurable skipping, see the docs
git for-each-ref refs/heads --format='
        [[ "%(upstream)" != "" ]] &&    # there is an upstream branch
        git merge-base --is-ancestor %(refname) %(upstream) &&  # which has this tip
        echo delete %(refname)
' # | sh # | git update-ref --stdin

Generating commands for the shell is useful and fun -- if you c&p the above it won't actually do anything to your repo, its output is instead the commands to check for whether a branch is in the upstream; 为shell生成命令是有用且有趣的-如果对上面的命令进行c&p操作,它实际上不会对您的存储库做任何事情,而是输出命令来检查分支是否在上游; delete a # and it'll execute the checks instead of just printing them, try it. 删除# ,它将执行检查,而不仅仅是打印检查,然后尝试。

Although it looks like a blind elephant in regards to the nimble and smart solution suggested by jthill, I share here the brutal approach I tend to use so far for the same need you describe : 尽管就jthill提出的灵活而明智的解决方案而言,它看起来像是一头盲目大象,但在这里,我还是分享我到目前为止为满足您所描述的需求而倾向于使用的残酷方法:

# nuke all branches* (which have no unique commit)
git branch -d $(git for-each-ref --format="%(refname:short) refs/heads")

# for "main" branches (I mean permanent, like master), recreate them if needed from remote
git checkout master

This is the one-shot version, but an alias can easily be made from it for convenience. 这是一次性版本,但为方便起见,可以轻松地为其创建别名。

Note the -d flag which, as opposed to -D , means "soft deletion" and refuses to delete branches with unmerged commits (safecheck ignored by -D ). 注意-d标志,该标志,而不是-D ,意思是“软删除”,并拒绝删除与未合并的提交(由safecheck忽略分支-D )。


* here, git might (depending on the state of the current branch) complain about not being able to delete the branch you're on. * 在这里,git可能(取决于当前分支的状态)抱怨无法删除您所在的分支。 Not a big deal in direct command mode, but a bit annoying in an alias. 在直接命令模式下没什么大不了的,但是在别名方面有点烦人。 Just checkout a mock branch beforehand and delete it afterwards. 只需事先检出模拟分支,然后再将其删除。 Since we're nuking everything to be sure, I tend to name the branch from-orbit . 由于我们在确定一切,因此我倾向于将其命名为from-orbit

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

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