使用 Git 删除本地分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41492254/
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 04:44:21  来源:igfitidea点击:

Deleting a local branch with Git

git

提问by Bob Wakefield

I've googled and there are several very long threads on this topic and none of them seem to help. I think I'm doing something wrong. I have a branch called Test_Branch. When I try to delete it using the recommend method, I get the following error:

我在谷歌上搜索过,关于这个话题有几个很长的话题,但似乎没有一个有帮助。我想我做错了什么。我有一个名为Test_Branch. 当我尝试使用推荐方法删除它时,出现以下错误:

Cannot delete branch 'Test_Branch' checked out at '[directory location]'.

无法删除在“[目录位置]”签出的分支“Test_Branch”。

I get no other information besides that. I can blow away the remote branch easy but the local branch won't go away.

除此之外,我没有得到其他信息。我可以轻松地吹走远程分支,但本地分支不会消失。

回答by Arpit Aggarwal

Switch to some other branch and delete Test_Branch, as follows:

切换到其他一些分支并删除Test_Branch,如下所示:

$ git checkout master
$ git branch -d Test_Branch

If above command gives you error - The branch 'Test_Branch' is not fully merged. If you are sure you want to delete itand still you want to delete it, then you can force delete it using -Dinstead of -d, as:

如果上面的命令给你错误 -The branch 'Test_Branch' is not fully merged. If you are sure you want to delete it并且你仍然想删除它,那么你可以使用-D而不是强制删除它-d,如:

$ git branch -D Test_Branch

To delete Test_Branchfrom remote as well, execute:

要从Test_Branch远程删除,请执行:

git push origin --delete Test_Branch

回答by Randy Leberknight

You probably have Test_Branch checked out, and you may not delete it while it is your current branch. Check out a different branch, and then try deleting Test_Branch.

您可能已签出 Test_Branch,并且当它是您的当前分支时,您可能无法将其删除。签出不同的分支,然后尝试删除 Test_Branch。

回答by jinxcat2008

Ran into this today and switching to another branch didn't help. It turned out that somehow my worktree information had gotten corrupted and there was a worktree with the same folder path as my working directory with a HEADpointing at the branch (git worktree list). I deleted the .git/worktree/folder that was referencing it and git branch -dworked.

今天遇到了这个问题,切换到另一个分支没有帮助。事实证明,不知何故我的工作树信息已损坏,并且有一个工作树与我的工作目录具有相同的文件夹路径,并HEAD指向分支 ( git worktree list)。我删除了.git/worktree/引用它并git branch -d工作的文件夹。

回答by dkniffin

If you have created multiple worktrees with git worktree, you'll need to run git prunebefore you can delete the branch

如果您使用git worktree创建了多个工作,则需要运行git prune才能删除分支

回答by Vasu

This worked for me...
I have removed the folders there in .git/worktrees folder and then tried "git delete -D branch-name".

这对我
有用...... 我已经删除了 .git/worktrees 文件夹中的文件夹,然后尝试“git delete -D branch-name”。

回答by Sankar Natarajan

In my case there were uncommitted changes from the previous branch lingering around. I used following commands and then delete worked.

就我而言,前一个分支中有未提交的更改。我使用了以下命令,然后删除工作。

git checkout *

git 结帐 *

git checkout master

git 结帐大师

git branch -D

git 分支 -D

回答by Praveen Mitta

Like others mentioned you cannot delete current branch in which you are working.

像其他人提到的那样,您不能删除您正在工作的当前分支。

In my case, I have selected "Test_Branch" in Visual Studioand was trying to delete "Test_Branch" from Sourcetree (Git GUI). And was getting below error message.

就我而言,我在Visual Studio 中选择了“Test_Branch”,并试图从 Sourcetree(Git GUI)中删除“Test_Branch”。并收到以下错误消息。

Cannot delete branch 'Test_Branch' checked out at '[directory location]'.

无法删除在“[目录位置]”签出的分支“Test_Branch”。

Switched to different branch in Visual Studioand was able to delete "Test_Branch" from Sourcetree.

切换到Visual Studio 中的不同分支,并能够从 Sourcetree 中删除“Test_Branch”。

I hope this helps someone who is using Visual Studio& Sourcetree.

我希望这对使用Visual StudioSourcetree 的人有所帮助。