简体   繁体   English

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

[英]Can I delete all the local branches except the current one?

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?

 $ 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)

first (switch to the branch you want to keep > ex : master ):首先(切换到您要保留的分支 > ex : master ):

 git checkout master

second (make sure you are on master )第二(确保你在master

 git branch -D $(git branch)

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 -d won't delete a "non fully merged" branch (see the documentation).请注意,小写-d不会删除“未完全合并”的分支(请参阅文档)。 Using -D will delete such branches , even if this causes commits to become "lost";使用-D将删除此类分支,即使这会导致提交“丢失”; 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.使用它时要非常小心,因为这也会删除分支引用日志,因此通常的“从意外删除中恢复”的东西也不起作用。

Basically, never use the -force version if you're not 300% sure you won't lose anything important.基本上,如果你不能 300% 确定你不会丢失任何重要的东西,就永远不要使用-force版本。 Because it's lost forever .因为它永远失去了。

For Windows, in Powershell use:对于 Windows,在 Powershell 中使用:

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

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

Delete all branches except a specific branch :删除除特定分支之外的所有分支

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

Delete all local branches except develop and master删除除developmaster之外的所有本地分支

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

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-ref to 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 HEAD is 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 * .更方便的是,您可以使用git branch ,它打印您的本地分支名称,前面有两个空格或(对于当前分支)有一个星号* 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 -d

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

or:或者:

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

Note that lowercase -d won't delete a "non fully merged" branch (see the documentation).请注意,小写-d不会删除“未完全合并”的分支(请参阅文档)。 Using -D will delete such branches, even if this causes commits to become "lost";使用-D将删除此类分支,即使这会导致提交“丢失”; 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.使用它时要非常小心,因为这也会删除分支引用日志,因此通常的“从意外删除中恢复”的东西也不起作用。

To remove all merged branches(except current -v '*' ):要删除所有合并的分支(当前-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 .取自这里

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 develop and master删除除developmaster之外的所有本地分支

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

I use this because I get to be more selective in what I do not want to delete.我使用它是因为我可以更有选择性地选择不想删除的内容。 This below command removes every branch except master, develop and the current branch.下面的命令会删除除 master、develop 和 current 分支之外的所有分支。

 BRANCHES=$(git branch | egrep -v "(master|develop|\*)" | xargs git branch -D) echo $BRANCHES

So I put this in my ~/.zshrc所以我把它放在我的~/.zshrc

 delete_branches() { BRANCHES=$(git branch | egrep -v "(master|develop|\*)" | xargs git branch -D) echo $BRANCHES } alias cleanup_branches=delete_branches

I once created this construct for my Windows environment.我曾经为我的 Windows 环境创建了这个结构。 Maybe it'll help someone else.也许它会帮助别人。 During execution, the master and current branch are not deleted .在执行过程中, master 和 current 分支不会被删除 All other merged branches will be deleted regardless.无论如何,所有其他合并的分支都将被删除。

 @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

Assuming git branch shows the current branch prefixed with * ;假设git branch显示当前分支以*为前缀; Using Powershell the following one liner will delete all branches that don't start with * .使用 Powershell 以下一行将删除所有不以*开头的分支。

git branch |? { $_ -lt "*" } | % { git branch -D $_.Trim() }

? = Where-Object = Where-对象

% = Foreach-Object % = Foreach-对象

Assuming you seek to delete all except the master branch and the current feature branch(lets call it 'team/features/dev-branch-name'), I'd like to condense it down to say:假设您试图删除主分支当前功能分支之外的所有内容(让我们称之为“团队/功能/开发分支名称”),我想将其浓缩为:

  1. First view the branches that you intend to delete首先查看您要删除的分支
  2. Then delete them然后删除它们

To view the branches:查看分支:

 git branch | grep -v -Fx "$(git branch --show-current)\|master"

I see that some answers suggest grep -v "develop" | grep -v "master"我看到一些答案建议grep -v "develop" | grep -v "master" grep -v "develop" | grep -v "master" , but this is an AND condition ie "give me all branch names that dont have "develop" in them and of those, find those that dont have "master" in them". grep -v "develop" | grep -v "master" ,但这是一个AND条件,即“给我所有没有“develop”的分支名称,其中找到那些没有“master”的分支名称。 Personally, I was looking for an OR condition.就个人而言,我正在寻找OR条件。

Note to avoid accidental deletion of dev branch: although this looks great (due to no hardcoding of branch names), beware that you may not be checked out on the correct branch while you run this alongside a delete command.注意避免意外删除 dev 分支:虽然这看起来很棒(由于分支名称没有硬编码),但请注意,当您与 delete 命令一起运行时,您可能不会在正确的分支上签出。 If your current branch checked out is master, then the above will list all except master only , whereas your intent was to list all except master and dev branch.如果您当前签出的分支是 master,那么上面将列出除 master only之外的所有分支,而您的意图是列出除 masterdev 分支之外的所有分支。

Thus I think it is best to make this foolproof by actually writing out the name of the branch you want to keep like so:因此,我认为最好通过实际写出您要保留的分支的名称来做到这一点,如下所示:

 git branch | grep -v -Fx 'team/features/dev-branch-name\|master'

This has the benefit that you do not need to be checked out on a specific branch for it to correctly list.这样做的好处是您无需在特定分支上签出即可正确列出。

Once you have successfully seen the branches you are about to delete, delete them by issuing:成功查看要删除的分支后,通过发出以下命令删除它们:

 git branch | grep -v -Fx 'team/features/dev-branch-name\|master' | xargs git branch -D

The above only adheres to deleting local branches.以上仅坚持删除本地分支。 To delete remote branches too (which you'd want to do in cases like editing/trimming a cloned over repo for example), use:要删除远程分支(例如,在编辑/修剪克隆的 repo 等情况下,您希望这样做),请使用:

 git branch -r | grep -v -Fx 'team/features/dev-branch-name\|master' | cut -d'/' -f 2- | xargs -L 1 git push origin --delete

Couple of things going on here: -r is to list all remote branches(if you've already deleted all the necessary local branches, then only the remote ones remain and you need to query for those specifically).这里发生了几件事: -r是列出所有远程分支(如果您已经删除了所有必要的本地分支,那么只剩下远程分支,您需要专门查询这些分支)。 -Fx gets all the exact matches and -v is a NOT condition. -Fx得到所有完全匹配,而-v是一个 NOT 条件。 As per the man page for cut , to cut -d'/' -f 2- means to divvy up the result by '/' and then take field 2 until the end of the line (-f 2-).根据 cut 的手册页, cut cut -d'/' -f 2-表示将结果按 '/' 划分,然后取字段 2 直到行尾 (-f 2-)。 This is useful if you wish to pipe team/features/dev-branch-name instead of origin/team/features/dev-branch-name to xargs .如果您希望 pipe team/features/dev-branch-name而不是origin/team/features/dev-branch-namexargs ,这将非常有用。 If you don't cut -d'/' -f 2- , then you'd be saying git push origin --delete origin/branch instead of git push origin --delete branch .如果您不cut -d'/' -f 2- ,那么您会说git push origin --delete origin/branch而不是git push origin --delete branch The former doesn't work.前者不起作用。 The -L 1 tell xargs to provide the left hand side input to the git push command one line at a time. -L 1告诉xargsgit push命令提供左侧输入,一次一行。

Even with the above, if you have remote branches that contain single quotation ( ' ) marks, it wont succeed on those.即使有上述情况,如果您有包含单引号 ( ' ) 标记的远程分支,它也不会成功。 For that, you could try:为此,您可以尝试:

 git branch -r | grep -v "'" | cut -d'/' -f 2- | xargs -L 1 git push origin --delete

I did have to delete these funky single quotation riddled branches manually due to a fatal: invalid spec error.由于fatal: invalid spec错误,我确实不得不手动删除这些时髦的单引号充满的分支。

So I see a lot of hard coded branch names here... And I think my answer here is more accurate to the "current branch" part of the question whilst keeping it single line and readable to bash newbies like me.所以我在这里看到了很多硬编码的分支名称......而且我认为我的回答更准确于问题的“当前分支”部分,同时保持单行并且像我这样的 bash 新手可读。 Just to place credit where it's due, the answer is rather obviously also based on @pankijs's answer.只是为了将功劳放在应有的地方,答案显然也基于@pankijs的答案。

git branch | grep -v $(git branch --show-current) | xargs git branch -d

and I have it aliased on one line in my.bash_aliases in debian aswell.我在 debian 的 my.bash_aliases 的一行中也有它的别名。

alias gitbclean='git branch | grep -v $(git branch --show-current) | xargs git branch -d'

(Though I thinks some bash features need to be enabled for the sub command to run on some command lines) (虽然我认为需要启用一些 bash 功能才能使子命令在某些命令行上运行)

Easiest of all of the above:

  1. Get a list of all the branches by using - "git branch" command in your project folder
  2. Copy the list of branches which you get
  3. Remove "master" from the list and the ones which you want to keep
  4. Write commands to delete all branches together separated by "&&" (see example below)
  5. If you want to delete even those branches which have not been fully merged use "-D" instead of "-d" in below command. "-D" deletes branches forcefully

Example:

  • git branch -d branch1 && git branch -d branch2 && git branch -d branch3 && git branch -d branch4 && git branch -d branch5 && ...

This works for me and I find it super easy to remove many branches together and I also get a message for each branch which gets deleted and error messages for those branches which fail to get deleted.

The current branch is marked with a * eg当前分支标有*例如

git branch branchA branchB * mybranch otherthing

Just do做就是了

for b in $(git branch | grep -v \*); do git branch -D $b; done

Do delete all branches NOT marked with a *删除所有未标有*的分支

IMHO, the safest way of removing local branches is:恕我直言,删除本地分支机构的最安全方法是:

 git branch -av | grep "\[gone\]" | awk '{print $1}' | xargs git branch -d

Also, more info related to this topic you can find Delete all local git branches此外,您可以找到与此主题相关的更多信息删除所有本地 git 分支

You can just keep checked out branch you need to keep - checkout master in the current git worktree then use:您可以只保留需要保留的已签出分支-当前 git 工作树中的签出主节点,然后使用:

git branch | xargs git branch -D

You'll see couple of errors about impossibility to remove the checked out branch and "*" branch (the symbol which is placed in the branches list before checked out branch).您会看到一些关于无法删除签出分支和“*”分支(在签出分​​支之前放置在分支列表中的符号)的错误。 If you have multiple worktrees for the local git repository other branches which are checked out there will be also kept with the same error (and additional errors while removing "+" branch accordingly to the symbol shown in the branches list before branches which are checked out in other worktrees of the current repository.如果您有多个本地 git 存储库的工作树,则其他签出的分支也将保持相同的错误(以及根据分支列表中显示的符号删除“+”分支时的其他错误在签出的分支之前在当前存储库的其他工作树中。

In Linux, the command you are looking for is:在 Linux 中,您要查找的命令是:

 git branch --list | grep -v "^* $(git branch --show-current)$" | xargs git branch -D

Here, git branch --show-current is used to get the current branch instead of hard coding a branch name.在这里, git branch --show-current用于获取当前分支,而不是硬编码分支名称。

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

相关问题 列出除当前分支之外的所有合并分支? - List all merged branches except the current one? 如何删除除master之外的所有本地分支并在一个没有别名的命令中开发? - How to delete all local branches except master and develop in one command without aliases? 在 git 中,如何删除除最新标签之外的所有标签(本地和远程) - In git how can I delete all tags(local and remote) except the latest one Git:如何删除所有本地分支(合并与否),除了master和develop - Git : How to delete all local branches (merged or not), except master and develop 如何隐藏所有分支中的新文件(创建分支除外)? - How can I hide new files from all branches except the one it was created in? 删除所有本地 git 分支 - Delete all local git branches 如何删除所有本地分支,如果合并为主分支将导致无变化? - How can I delete all local branches which would result in no changes if merged into master? 如何将本地 master 合并到所有本地分支? - How can I merge local master into all local branches? 如何将本地git仓库中的所有远程分支转换为本地跟踪分支 - How can I convert all the remote branches in a local git repo into local tracking branches 如何清除(删除)所有合并的本地分支? - How to clear (delete) all merged local branches?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM