git 如何从只有 1 个分支的 sourcetree 中的 repo 中删除一个分支?

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

how to delete a branch from a repo in sourcetree with only 1 branch?

gitatlassian-sourcetree

提问by Greg Brattle

I have SourceTree installed on my local environment. I'm working within a shared collection of git repos for at an enterprise. Looks like the admin created a new repo RepoY and we previously only had 1 repo (RepoX). I accidentally created BranchZ in RepoY and I need to delete it.

我在本地环境中安装了 SourceTree。我在一个企业的 git repos 共享集合中工作。看起来管理员创建了一个新的 repo RepoY,而我们之前只有 1 个 repo (RepoX)。我不小心在 RepoY 中创建了 BranchZ,我需要删除它。

I selected BranchZ through SourceTree under RepoY, right-clicked and I was able to "Remove" it but now it displays with a red minus sign to the left. I tried to "Delete" the branch but SourceTree returns the following error: "error: Cannot delete the branch 'BranchZ' which you are currently on."

我通过 RepoY 下的 SourceTree 选择了 BranchZ,右键单击,我能够“删除”它,但现在它在左侧显示一个红色减号。我试图“删除”分支,但 SourceTree 返回以下错误:“错误:无法删除您当前所在的分支‘BranchZ’。”

I'm not able to switch branches within the repo because BranchZ is the only branch in the repo. If I double-click a different repo in SourceTree then I can only delete RepoY but not BranchZ within RepoY. Is there a way to do what I'm trying to do through SourceTree or is this a limitation of SourceTree?

我无法在 repo 中切换分支,因为 BranchZ 是 repo 中的唯一分支。如果我在 SourceTree 中双击一个不同的 repo,那么我只能删除 RepoY 而不能删除 RepoY 中的 BranchZ。有没有办法通过 SourceTree 做我想要做的事情,或者这是 SourceTree 的限制吗?

回答by Msp

When you create a git repository using git init, master branch will be created by default. I think in your case master branch may be deleted by some mistake, or you accidently renamed it. So, in this case, what you can do from SourceTree is,

当你使用 git 创建一个仓库时git init,默认会创建 master 分支。我认为在您的情况下, master 分支可能被某些错误删除,或者您不小心重命名了它。所以,在这种情况下,你可以从 SourceTree 做的是,

  1. Create a branch called master
  2. Checkout masterbranch
  3. Delete BranchZbranch
  1. 创建一个名为的分支 master
  2. 结账master分行
  3. 删除BranchZ分支

Or from command line,

或者从命令行,

cd <repo_dir>
git branch master
git checkout master
git branch -d BranchZ

回答by FatmaT

You are not allow to delete the branch which you are on, firstly you have checkout to another branch since it is the only branch you can just simply checkout master branch(every repo has master branch if it wasn't deleted). Then you are able to delete but be sure you are in correct repo.

你不允许删除你所在的分支,首先你要结帐到另一个分支,因为它是唯一一个你可以简单地结帐主分支的分支(如果没有删除,每个仓库都有主分支)。然后您可以删除,但请确保您在正确的回购中。

git checkout master
git push origin :<branchName> (it deletes local and remote branch in case of you already pushed commits)

回答by wp78de

This can be done in Sourcetree's UI nowadays.

如今,这可以在 Sourcetree 的 UI 中完成。

Go to the (remote) branch view, select the node you want to delete, right click, select Branch...
In the Branch Window switch to the Delete Branchestab..

转到(远程)分支视图,选择要删除的节点,右键单击,选择分支...
在分支窗口中切换到删除分支选项卡。

enter image description here

在此处输入图片说明

Delete or force delete branches as needed, then checkout again.

根据需要删除或强制删除分支,然后再次结帐。