IntelliJ - GIT 删除 GUI 中的本地孤立分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38508550/
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
IntelliJ - GIT delete local orphan branch in GUI
提问by Guillaume M
I use IntelliJ (2016.1.3) and GIT integration plugin (8.1). During my use of IntelliJ I checked out multiple branches. Now those branches are deleted from GIT (after each pull request we delete them).
我使用 IntelliJ (2016.1.3) 和 GIT 集成插件 (8.1)。在我使用 IntelliJ 的过程中,我检查了多个分支。现在这些分支从 GIT 中删除(在每个拉取请求之后我们删除它们)。
From now on every branch I checked out once is present in my GIT view from Intellij (by VCS > Git > Branches...), see under :
从现在开始,我检查过的每个分支都出现在 Intellij 的 GIT 视图中(通过 VCS > Git > Branches...),请参阅以下内容:
There is a way to delete them : do it ... one by one! But if there are a lot of branches (let say a hundred ...), it certainly getting tedious...
有一种方法可以删除它们:做...一个一个!但是如果有很多分支(比如说一百个......),它肯定会变得乏味......
The question is :Do you know if there is a simple way to delete all the orphan local branches from this view in IntelliJ ?
问题是:您是否知道是否有一种简单的方法可以从 IntelliJ 中的此视图中删除所有孤立的本地分支?
Thanks :)
谢谢 :)
EDIT : I have found a way to delete them but in another tool : "Git Gui". When you delete it from there, it's deleted in IntelliJ too. And you can delete multiple branches at one time.
编辑:我找到了一种删除它们的方法,但是在另一个工具中:“Git Gui”。当您从那里删除它时,它也会在 IntelliJ 中删除。并且您可以一次删除多个分支。
回答by Colin D Bennett
Remote tracking branches are easily confused with normal Git branches. A remote tracking branch begins with remotes/
, as in remotes/origin/branch1
, although Git allows you to omit remotes/
as a shorthand, so you can also refer to remotes/origin/branch1
as simply origin/branch1
. But these are not normal branches. You can't commit to them or check them out directly. If you try to check them out, Git will either check out a detached HEAD or it will automatically create a new local branch with the same name as the remote branch, e.g. branch1
. This new local branch will never be deleted unless to tell Git to delete it explicitly.
远程跟踪分支很容易与普通的 Git 分支混淆。远程跟踪分支以 开头remotes/
,如remotes/origin/branch1
,尽管 Git 允许您将其省略remotes/
为简写,因此您也可以remotes/origin/branch1
简单地引用为origin/branch1
。但这些都不是正常的分支。您不能承诺或直接检查它们。如果您尝试检出它们,Git 将检出分离的 HEAD,或者它会自动创建一个与远程分支同名的新本地分支,例如branch1
. 这个新的本地分支永远不会被删除,除非明确告诉 Git 删除它。
What is happening is twofold:
正在发生的事情是双重的:
You need to make sure Git is pruningthe remote tracking branches (
remotes/origin/branch1
, etc.). By default it does not. See this question on pruning remote tracking branchesfor how to do this. Basically it'sgit fetch -p origin
orgit remote prune origin
.When you checked out a branch to work on it, you created a local branch with the same name as the remote tracking branch. The local branch
branch1
will never be deleted unless you do so manually, even if the remote tracking branchremotes/origin/branch1
is pruned, which is done ifbranch1
was deleted on the remote server.
您需要确保 Git 正在修剪远程跟踪分支(
remotes/origin/branch1
等)。默认情况下不会。有关如何执行此操作,请参阅有关修剪远程跟踪分支的问题。基本上它是git fetch -p origin
orgit remote prune origin
。当您签出一个分支以对其进行处理时,您创建了一个与远程跟踪分支同名的本地分支。
branch1
除非您手动删除本地分支,否则永远不会删除本地分支,即使远程跟踪分支remotes/origin/branch1
被修剪,如果branch1
在远程服务器上被删除,就会完成。
You can clean up these branches manually with git branch -d
(or force it with -D
, which may be necessary if the branch was never merged), or look at answers to Remove old remote branches from Gitfor an automated way to do it.
您可以手动清理这些分支git branch -d
(或强制使用-D
,如果分支从未合并,这可能是必要的),或者查看从 Git 中删除旧远程分支的答案以自动执行此操作。
回答by Madhavi Ambati
Git tracks its local and remote repositories details in .git folder under the git project.
Git 在 git 项目下的 .git 文件夹中跟踪其本地和远程存储库的详细信息。
To remove local orphan branches, go to:
要删除本地孤立分支,请转到:
.git -> refs -> heads (in this location you see all your local branches)
.git -> refs -> head(在这个位置你可以看到你所有的本地分支)
Then delete the branches whith you do not need.
然后删除不需要的分支。
Note: If you are using mac os clear your trash bin as well.
注意:如果您使用的是 mac os,请清除您的垃圾箱。
This clears your local orphan branches in one shot.
这可以一次性清除您本地的孤儿分支。
回答by sanjeevjha
I Think another way to delete local git branch is as bellow and it might be helful to you as per your requirement,
我认为另一种删除本地 git 分支的方法如下,根据您的要求,它可能对您有帮助,
git branch -d branch1 branch2 branch3 branch4
git branch -d branch1 branch2 branch3 branch4
if you execute above command on the terminal from project directory then it will delete all the branch that you have passed i.e branch1 branch2 ... branch_n
如果您从项目目录在终端上执行上述命令,那么它将删除您通过的所有分支,即 branch1 branch2 ... branch_n
I hope it will help you out. Thanks.
我希望它能帮助你。谢谢。
回答by Chaithu Narayana
I tried the updated answer from @Guillaume M and works fine for me. I had lots of local branches (on my IntelliJ) which are hanging loose as their remote counterparts were merged and deleted remotely. There wasn't any housekeeping done over a period of time and the dangling references kept piling up. So, basically these are orphans not tracking any remote branches.
我尝试了@Guillaume M 的更新答案,对我来说效果很好。我有很多本地分支(在我的 IntelliJ 上),因为它们的远程分支被远程合并和删除,所以它们处于松散状态。一段时间内没有进行任何清理工作,悬空的参考资料不断堆积。所以,基本上这些都是孤儿,没有跟踪任何远程分支。
Here's what I did to clean them up - used 'Git Gui' to delete the orphan local branches and refresh/restart (not mandatory though) IntelliJ. Git gui appears slightly convenient for this kind of batch deletion of the branches.
这是我为清理它们所做的工作 - 使用“Git Gui”删除孤立的本地分支并刷新/重新启动(虽然不是强制性的)IntelliJ。对于这种批量删除分支的操作,Git gui 显得稍微方便一些。
- Launch 'Git Gui' tool
- 'Open Existing Repository'. Choose path to the repository that you are working with your IntelliJ IDE.
- On the top menu, choose 'Branch' -> 'Delete'
- A pop-up appears with all the local branches in your repository.
- Choose all the local branches that you want to delete and click 'Delete'. You may add preconditions to prevent any accidental deletions. In my case they were all loosely hanging references with no real knowledge about them being merged to some other branch. Hence I chose 'Do not perform merge checks' to proceed with the operation which otherwise would fail on the preconditions.
- 启动“Git Gui”工具
- '打开现有存储库'。选择您使用 IntelliJ IDE 的存储库的路径。
- 在顶部菜单上,选择“分支”->“删除”
- 将出现一个弹出窗口,其中包含存储库中的所有本地分支。
- 选择要删除的所有本地分支,然后单击“删除”。您可以添加先决条件以防止任何意外删除。在我的情况下,它们都是松散的引用,对它们被合并到其他分支一无所知。因此,我选择了“不执行合并检查”来继续操作,否则会在前提条件下失败。
Hopefully, IntelliJ comes up with an option to automatically housekeep these kind of branches. Or it already exists and I'm missing it. In any case, this approach saved me some time.
希望 IntelliJ 提供一个选项来自动管理这些类型的分支。或者它已经存在而我错过了它。无论如何,这种方法为我节省了一些时间。
回答by axiac
Open a console in the project's directory and run:
在项目目录中打开一个控制台并运行:
git remote prune origin
It removes any local references to remote branches that do not exist any more on the remote repository named origin
.
它删除了对名为 的远程存储库中不再存在的远程分支的任何本地引用origin
。
Replace origin
in the command above with the name of your remote repository (as it is known by the local repository) in case it is not origin
.
替换origin
在上面的命令与远程仓库的情况下,域名(因为它是由本地资源库知道)它不是origin
。
As far as I know there is no way to accomplish this goal using the IntelliJ Git plugin.
据我所知,使用 IntelliJ Git 插件无法实现这一目标。
Read more at: https://git-scm.com/docs/git-remote
阅读更多信息:https: //git-scm.com/docs/git-remote