使用git远程修剪原点,我可以删除不在远程上的本地分支。

但是我还想删除从这些远程分支创建的本地分支(检查它们是否未合并会很好)。

我该怎么做呢?


当前回答

以下是@wisbucky对Windows用户的回答:

for /f "tokens=1" %i in ('git branch -vv ^| findstr ": gone]"') DO git branch %i -d

我使用posh-git,不幸的是PS不喜欢裸体for,所以我创建了一个普通的'ol命令脚本,名为PruneOrphanBranches.cmd:

@ECHO OFF
for /f "tokens=1" %%i in ('git branch -vv ^| findstr ": gone]"') DO CALL :ProcessBranch %%i %1

GOTO :EOF

:ProcessBranch
IF /I "%2%"=="-d" (
    git branch %1 %2
) ELSE (
    CALL :OutputMessage %1
)
GOTO :EOF

:OutputMessage
ECHO Will delete branch [%1] 
GOTO :EOF

:EOF

不带参数调用它以查看列表,然后用“-d”调用它来执行实际的删除或对任何没有完全合并但无论如何都想删除的分支执行“-d”。

其他回答

在git帮助获取提供的信息中,有这样一个小项目:

 -p, --prune
        After fetching, remove any remote-tracking branches which no longer exist on the remote.

也许,git fetch -p是你要找的?

编辑:好吧,对于那些3年后还在争论这个答案的人,这里有一些关于我为什么给出这个答案的更多信息……

首先,OP说他们想“删除那些从远程分支创建的本地分支(这些分支不在远程上了)”。这在git中并不是完全可能的。举个例子。

假设我在中央服务器上有一个回购,它有两个分支,称为a和B。如果我将该回购克隆到我的本地系统,我的克隆将有本地引用(还不是实际的分支)称为origin/ a和origin/B。现在假设我做了以下事情:

git checkout -b A origin/A
git checkout -b Z origin/B
git checkout -b C <some hash>

这里的相关事实是,出于某种原因,我选择在我的本地回购上创建一个分支,该分支的名称与其起源不同,并且我还有一个本地分支,它(还)不存在于起源回购上。

Now let's say I remove both the A and B branches on the remote repo and update my local repo (git fetch of some form), which causes my local refs origin/A and origin/B to disappear. Now, my local repo has three branches still, A, Z, and C. None of these have a corresponding branch on the remote repo. Two of them were "created from ... remote branches", but even if I know that there used to be a branch called B on the origin, I have no way to know that Z was created from B, because it was renamed in the process, probably for a good reason. So, really, without some external process recording branch origin metadata, or a human who knows the history, it is impossible to tell which of the three branches, if any, the OP is targeting for removal. Without some external information that git does not automatically maintain for you, git fetch -p is about as close as you can get, and any automatic method for literally attempting what the OP asked runs the risk of either deleting too many branches, or missing some that the OP would otherwise want deleted.

还有其他的场景,比如,如果我在origin/A之外创建三个独立的分支,以测试三种不同的方法,然后origin/A消失了。现在我有三个分支,它们显然不能全部匹配名称,但它们是从origin/A创建的,因此OPs问题的字面解释需要删除所有三个分支。然而,如果你能找到一种可靠的方法来匹配它们,这可能并不理想……

你可以通过一些简单的操作来做到这一点:

输出你所有的分支到一个临时文件:

git branch > branches.tmp

打开文件并删除分支以排除它们从本地删除(分支如develop/master/main/…) 通过cat命令将分支名称传递给xargs并删除分支:

cat branches.tmp | xargs git branch -D

如果要删除所有已经合并到master中的本地分支,可以使用以下命令:

git branch --merged master | grep -v '^[ *]*master$' | xargs git branch -d

如果你使用main作为你的主分支,你应该相应地修改命令:

git branch --merged main | grep -v '^[ *]*main$' | xargs git branch -d

更多信息。

Windows的解决方案

适用于Microsoft Windows Powershell:

Git结帐大师;Git远程更新源——prune;git branch -vv | Select-String -Pattern ": gone]" | % {$_.toString(). trim()。Split(" ")[0]} | % {git branch -d $_}

解释

Git签出主切换到主分支

Git远程更新来源——修剪远程分支

Git分支-vv获取所有分支的详细输出(Git引用)

Select-String -Pattern ": gone]"只获取已从远程删除的记录。

% {$_.toString()。Split(" ")[0]}获取分支名称

% {git branch -d $_}删除分支

这个命令和下面的脚本可以在Linux和Windows中使用Git Bash (MinGW)。

最好使用git的内部命令,这样注释或名称就不会意外地匹配并删除您不想删除的分支。git for-each-ref的格式选项可以使用许多内部“原子”来输出所需的信息。这样,我们就不必依赖管道连接到awk或grep来检查输出中的正则表达式,因为它可能包含不必要的信息。

下面的命令只使用git for-each-ref的内部低级命令来列出孤立的本地分支。一旦你有了这些,你可以管道到git分支-D。此外,别忘了先修剪和获取远程引用,否则它将找不到任何匹配:

git fetch -p
git for-each-ref --format '%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)' 'refs/heads/**' | xargs -r git branch -D

以下是分类:

Git fetch -p -删除已删除的引用,并从远程获取新的引用

Git for-each-ref——format -列出使用特定输出格式的所有引用。

%(if: = =[gone])%(upstream:track) -仅在上游跟踪分支“[gone]”时输出。

%(then)%(refname:short)%(end) -输出分支名称(当跟踪消失时)。

Refs /heads/** -限制头部引用(为了提高效率)。

| xargs -r git分支- d管道输出作为参数删除。-r表示忽略空白输入。


就其本身而言,这个解很长,很难输入,也很难记住。幸运的是,向git添加自定义命令很容易。下面的脚本使用了上面的相同命令,但是它允许用户使用——dry-run选项查看将选择哪些分支。

我将我的文件命名为git-prune-local,并将其放在包含在PATH中的文件夹中。它还需要执行权限(chmod 755 git-prune-local)。

Git自动查找可执行文件,如Git -[command]。这样,你只需要输入git prune-local来删除正确的分支。

git-prune-local

#!/bin/sh

if [ $# -gt 1 ] || ([ ! -z $1 ] && [ $1 != "--dry-run" ])
then
    echo "Usage: git prune-local [--dry-run]"
    exit
fi

git fetch -p --quiet
branchesToDelete=$(git for-each-ref --format '%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)' 'refs/heads/**')

while read -r branch
do
    if [ ! -z $branch ]
    then
        if [ ! -z $1 ]
        then
            echo $branch
        else
            git branch -D $branch
        fi
    fi
done <<< "$branchesToDelete"