git 如何在本地删除分支?

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

How to remove a branch locally?

gitgithub

提问by Nate

I have a master and a dev branch in my repository. I want to remove the master branch from my computer so that I don't accidentally commit to it (it's happened..).

我的存储库中有一个 master 和一个 dev 分支。我想从我的计算机中删除 master 分支,这样我就不会不小心提交到它(它发生了..)。

There are questions on here about how to delete branches locally andremotely, but I haven't been able to find out how to only delete a branch locally.

这里有关于如何在本地远程删除分支的问题,但我一直无法找到如何只在本地删除分支。

One answer said to use this:

一个答案说使用这个:

git branch -d local_branch_name

But I tried that and the branch still shows up in the GitHub application.

但是我试过了,这个分支仍然出现在 GitHub 应用程序中。

采纳答案by torek

I think (based on your comments) that I understand what you want to do: you want your local copy of the repository to have neither the ordinary local branch master, northe remote-tracking branch origin/master, even though the repository you cloned—the github one—has a local branch masterthat you do notwant deleted from the github version.

我认为(根据您的评论)我理解您想要做什么:您希望存储库的本地副本既没有普通的本地分支master也没有远程跟踪分支origin/master,即使您克隆的存储库 - github 一个都具有一个本地分支master,你希望从GitHub的版本中删除。

You can do this by deleting the remote-tracking branch locally, but it will simply come back every time you ask your git to synchronize your local repository with the remote repository, because your git asks their git "what branches do you have" and it says "I have master" so your git (re)creates origin/masterfor you, so that your repository has what theirs has.

您可以通过在本地删除远程跟踪分支来完成此操作,但是每次您要求 git 将本地存储库与远程存储库同步时,它都会返回,因为您的 git 询问他们的 git “您有哪些分支”并且它说“我有master”,所以你的 git(重新)origin/master为你创建,这样你的存储库就拥有他们拥有的。

To delete your remote-tracking branch locally using the command line interface:

要使用命令行界面在本地删除远程跟踪分支:

git branch -d -r origin/master

but again, it will just come back on re-synchronizations. It ispossible to defeat this as well (using remote.origin.fetchmanipulation), but you're probably better off just being disciplined enough to not create or modify masterlocally.

但同样,它只会在重新同步时恢复。也可能(使用remote.origin.fetch操纵)来解决这个问题,但是您最好保持足够的纪律性,不要master在本地创建或修改。

回答by Benyamin Jafari

Force Delete the Local Branch:

强制删除本地分支:

$ git branch -D <branch_name>


[NOTE]:

[注意]:

"-D"is force delete option.

"-D"是强制删除选项。



回答by Eugene Kaurov

As far I can understand the original problem, you added commits to local master by mistakeand did not push that changes yet. Now you want to cancel your changes and hope to delete your local changes and to create a new master branch from the remote one.

据我所知,最初的问题是 您错误地向本地主服务器添加了提交,但尚未推送该更改。现在您想取消您的更改并希望删除您的本地更改并从远程创建一个新的主分支。

You can just reset your changes and reload master from remote server:

您可以重置您的更改并从远程服务器重新加载主服务器:

git reset --hard origin/master

回答by Eugene Kaurov

The Github application for Windows shows all remote branches of a repository. If you have deleted the branch locally with $ git branch -d [branch_name], the remote branch still exists in your Github repository and will appear regardless in the Windows Github application.

适用于 Windows 的 Github 应用程序显示存储库的所有远程分支。如果您使用 删除了本地分支$ git branch -d [branch_name],远程分支仍然存在于您的 Github 存储库中,并且无论在 Windows Github 应用程序中都会出现。

If you want to delete the branch completely (remotely as well), use the above command in combination with $ git push origin :[name_of_your_new_branch]. Warning:this command erases all existing branches and may cause loss of code. Be careful, I do not think this is what you are trying to do.

如果要完全删除分支(也可以远程删除),请结合使用上述命令$ git push origin :[name_of_your_new_branch]警告:此命令会擦除所有现有分支并可能导致代码丢失。小心,我认为这不是你想要做的。

However every time you delete the local branch changes, the remote branch will still appear in the application. If you do not want to keep making changes, just ignore it and do not click, otherwise you may clone the repository. If you had any more questions, please let me know.

但是每次删除本地分支更改时,远程分支仍会出现在应用程序中。如果您不想继续进行更改,请忽略它并且不要单击,否则您可能会克隆存储库。如果您还有其他问题,请告诉我。

回答by Qamar Stationwala

By your tags, I'm assuming your using Github. Why not create some branch protection rules for your master branch? That way even if you do try to push to master, it will reject it.

根据你的标签,我假设你使用 Github。为什么不为您的主分支创建一些分支保护规则?这样,即使您确实尝试推动掌握,它也会拒绝它。

1) Go to the 'Settings' tab of your repo on Github.

2) Click on 'Branches' on the left side-menu.

3) Click 'Add rule'

4) Enter 'master' for a branch pattern.

5) Check off 'Require pull request reviews before merging'

1) 转到 Github 上存储库的“设置”选项卡。

2) 点击左侧菜单中的“Branches”。

3)点击“添加规则”

4) 为分支模式输入“master”。

5) 勾选“合并前需要请求”

I would also recommend doing the same for your dev branch.

我还建议为您的开发分支做同样的事情。

回答by Kumar Vaduganathan

you need switch into another branch and try the same.

您需要切换到另一个分支并尝试相同的操作。

git branch -d

git 分支 -d