简体   繁体   English

批处理文件,它将删除目录的每个子文件夹中的所有本地分支(git)

[英]Batch file that will delete all local branches (git) in each subfolder of a directory

Here is my requirement: 这是我的要求:

  1. Iterate through all the subfolders of the given directory. 遍历给定目录的所有子文件夹。
  2. In each subfolder, delete all local branches except master branch. 在每个子文件夹中,删除除master分支以外的所有本地分支。

For item#2, the following statement works when manually executed in a subfolder. 对于第2项,以下语句在子文件夹中手动执行时有效。

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

I want to create a batch file that will execute the above statement in each subfolder. 我想创建一个批处理文件,该文件将在每个子文件夹中执行上述语句。

I tried the following but could not make it work. 我尝试了以下操作,但无法使其正常工作。 Please help me identify what's wrong with the statement. 请帮助我确定该语句出了什么问题。

FOR /D %%G in ("C:\parent\*") Do (echo Updating %%G ...) && (cd %%G) && git branch | grep -v " master" | xargs git branch -D

In each subfolder, delete all local branches except master branch. 在每个子文件夹中,删除除master分支以外的所有本地分支。

That is not how Git works. 那不是Git的工作方式。 That might make sense with Subversion, not Git. 这对于Subversion而不是Git可能有意义。
Once you have deleted a branch, that branch is gone for the all repo, not for a subfolder. 删除分支后,该分支将用于所有存储库,而不用于子文件夹。

The only case where you could delete a branch in a Git repo subfolder is if that subfolder is the root folder is a submodule . 唯一可以删除Git repo子文件夹中的分支的情况是,如果该子文件夹是根文件夹,则为子模块

Plus, you might consider using -d instead of -D , to avoid deleting branches not fully merged. 另外,您可以考虑使用-d而不是-D ,以避免删除未完全合并的分支。 See " Can I delete all the local branches except the current one? ". 请参阅“是否可以删除除当前分支机构以外的所有本地分支机构? ”。

thank you for all the help. 感谢您的所有帮助。

It turns out that I am encountering the following error: 原来我遇到以下错误:

'grep' is not recognized as an internal or extenal command.

I did not see it because I execute the batch file by double clicking it, then command prompt will display and immediately close so I missed the error message. 我没有看到它,因为我通过双击它来执行批处理文件,然后将显示命令提示符并立即关闭,因此我错过了错误消息。

Anyway, the batch file with the contents above worked as expected when I execute it using Git bash. 无论如何,当我使用Git bash执行它时,具有上述内容的批处理文件可以按预期工作。

Note that this is only for maintenance purposes, assuming that I only need the master branch and all local branches are merged. 请注意,这仅出于维护目的,假设我只需要master分支并且所有本地分支都已合并。

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

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