git 我可以删除除当前分支之外的所有本地分支吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28572293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:56:27  来源:igfitidea点击:

Can I delete all the local branches except the current one?

gitgit-branch

提问by sensorario

I want to delete all branches that get listed in the output of ...

我想删除在...的输出中列出的所有分支

$ git branch

... but keeping current branch, in one step. Is that possible? If so, how?

...但保持当前分支,一步。那可能吗?如果是这样,如何?

采纳答案by Vadorequest

Based on @pankijs answer, I made two git aliases:

根据@pankijs 的回答,我创建了两个 git 别名:

[alias]
    # Delete all local branches but master and the current one, only if they are fully merged with master.
    br-delete-useless = "!f(){\
        git branch | grep -v "master" | grep -v ^* | xargs git branch -d;\
    }; f"
    # Delete all local branches but master and the current one.
    br-delete-useless-force = "!f(){\
        git branch | grep -v "master" | grep -v ^* | xargs git branch -D;\
    }; f"

To be added in ~/.gitconfig

待添加 ~/.gitconfig



And, as @torek pointed out:

而且,正如@torek 指出的那样:

Note that lowercase -dwon't delete a "non fully merged" branch (see the documentation). Using -Dwill delete such branches, even if this causes commits to become "lost"; use this with great care, as this deletes the branch reflogs as well, so that the usual "recover from accidental deletion" stuff does not work either.

请注意,小写-d不会删除“非完全合并”分支(请参阅文档)。使用-D将删除这些分支,即使这会导致提交变得“丢失”;小心使用它,因为这也会删除分支引用日志,因此通常的“从意外删除中恢复”的东西也不起作用。

Basically, never use the -forceversion if you're not 300% sure you won't lose anything important. Because it's lost forever.

基本上,-force如果您不是 300% 确定您不会丢失任何重要内容,请不要使用该版本。因为它永远失去

回答by pankijs

$ git branch | grep -v "master" | xargs git branch -D 

will delete all branches except master (replace master with branch you want to keep, but then it will delete master)

将删除除 master 之外的所有分支(将 master 替换为您要保留的分支,但随后它将删除 master)

回答by torek

git branch -d(or -D) allows multiple branch names, but it's a bit tricky to automatically supply "all local branches excluding the one I'm on now" without writing at least a little bit of code.

git branch -d(或-D) 允许多个分支名称,但是在不编写至少一点代码的情况下自动提供“除我现在所在的分支之外的所有本地分支”有点棘手。

The "best" (formally correct) method is to use git for-each-refto get the branch names:

“最佳”(形式上正确)方法是git for-each-ref用于获取分支名称:

git for-each-ref --format '%(refname:short)' refs/heads

but then it's even harder to figure out which branch you're on (git symbolic-ref HEADis the "formally correct" method for this, if you want to write a fancy script).

但是然后更难弄清楚你在哪个分支上(git symbolic-ref HEAD如果你想写一个花哨的脚本,这是“正式正确”的方法)。

More conveniently, you can use git branch, which prints your local branch names preceded by two spaces or (for the current branch) by an asterisk *. So, run this through something to remove the *version and you're left with space-separated branch names, which you can then pass to git branch -d:

更方便的是,您可以使用git branch,它打印您的本地分支名称,前面有两个空格或(对于当前分支)一个星号*。所以,通过一些东西来删除*版本,你会留下以空格分隔的分支名称,然后你可以传递给git branch -d

git branch -d $(git branch | grep -v '^*')

or:

或者:

git branch | grep -v '^*' | xargs git branch -d

Note that lowercase -dwon't delete a "non fully merged" branch (see the documentation). Using -Dwill delete such branches, even if this causes commits to become "lost"; use this with great care, as this deletes the branch reflogs as well, so that the usual "recover from accidental deletion" stuff does not work either.

请注意,小写-d不会删除“非完全合并”分支(请参阅文档)。使用-D将删除这些分支,即使这会导致提交变得“丢失”;小心使用它,因为这也会删除分支引用日志,因此通常的“从意外删除中恢复”的东西也不起作用。

回答by Stepan Suvorov

To remove all merged branches(except current -v ‘*'):

删除所有合并的分支(除了 current -v ‘*'):

git branch --merged | grep -v '*' | xargs git branch -D

also I made such command for repo complete clean up:

我还为 repo 完全清理做了这样的命令:

alias git-clean="git branch  | grep -v '*' | grep -v 'master' | xargs git branch -D  && git reset --hard && git clean -d -x -f"

taken from here.

取自这里

回答by Asad Manzoor

Delete all branches except a specific branch:

删除除特定分支之外的所有分支

git branch | grep -v "branch name" | xargs git branch -D

Delete all local branches except developand master

删除除developmaster之外的所有本地分支

git branch | grep -v "develop" | grep -v "master" | xargs git branch -D

回答by KhogaEslam

first (switch to the branch you want to keep > ex: master):

首先(切换到您要保留的分支 > ex: master):

git checkout master

second (make sureyou are on master)

第二(确保你在master 上

git branch -D $(git branch)

回答by Robert Corvus

For Windows, in Powershell use:

对于 Windows,在 Powershell 中使用:

git branch | %{ $_.Trim() } | ?{ $_ -ne 'master' } | %{ git branch -D $_ }

回答by Dan Dye

To delete all branches except for the current branch in one step:

一步删除除当前分支外的所有分支:

git branch | grep -v $(git rev-parse --abbrev-ref HEAD) | xargs git branch -D

git branch | grep -v $(git rev-parse --abbrev-ref HEAD) | xargs git branch -D

回答by Asad Manzoor

Delete all merged branch locally:

在本地删除所有合并的分支:

git branch -D `git branch --merged | grep -v \* | xargs`

Delete all branches except a specific branch:

删除除特定分支之外的所有分支

git branch | grep -v "branch name" | xargs git branch -D

Delete all local branches except developand master

删除除developmaster之外的所有本地分支

git branch | grep -v "develop" | grep -v "master" | xargs git branch -D

回答by George Maharis

I once created this construct for my Windows environment. Maybe it'll help someone else. During execution, the master and current branch are not deleted. All other merged branches will be deleted regardless.

我曾经为我的 Windows 环境创建了这个构造。也许它会帮助别人。在执行过程中,不会删除 master 和 current 分支。无论如何,所有其他合并的分支都将被删除。

@echo off
cd PATH_TO_YOUR_REPO

REM -- Variable declerations
set "textFile=tempBranchInfo.txt"
set "branchToKeep=master"
set "branchToReplaceWith="
git branch --merged > %textFile%

REM -- remove "master" from list to keep the branch
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    >>"%textFile%" echo(!line:%branchToKeep%=%branchToReplaceWith%!
    endlocal
)

REM -- execute branch delete commands
for /f "delims=" %%a in (%textFile%) do (
    git branch -D %%a
)

REM -- remove temp-file with branch information inside
DEL %textFile%

REM -- show local branches after the cleaning
echo Local branches:
git branch

pause 
exit