如何删除(或合并)我当前所在的本地 Git 分支?

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

How can I delete (or merge) a local Git branch that I'm currently on?

gitgit-branchrepository

提问by user496854

I'm pretty new to using git, and I use it to contribute to the AOKP Android ROM. I've successfully created a few branches, modified the code, and uploaded the commits that have gotten merged on the remote end. But in my local repository, those branches are still showing up (even though they show up as having no changes). The problem is that when I created those branches, I did so right from the subfolder that needed to be modified, so I don't have any "higher" branches to go to. And because of that, I can't delete those branches -- git tels me error: Cannot delete the branch 'MyMods' which you are currently on.

我对使用 git 很陌生,我用它来为 AOKP Android ROM 做出贡献。我已经成功创建了几个分支,修改了代码,并上传了在远程端合并的提交。但是在我的本地存储库中,这些分支仍在显示(即使它们显示为没有更改)。问题是,当我创建这些分支时,我是从需要修改的子文件夹中这样做的,所以我没有任何“更高”的分支可以去。正因为如此,我无法删除那些分支——git 告诉我error: Cannot delete the branch 'MyMods' which you are currently on.

So what can I do to get rid of those branches?

那么我该怎么做才能摆脱这些分支呢?

回答by poke

Checkout a different branch first, before deleting it:

在删除之前先检出一个不同的分支:

git checkout master
git branch -d MyMods

Also, branches have nothing to do with folders. Git always tracks the whole repository at once, with all its folders and files. A branch is nothing else than a pointer to a single commit, or snapshot, in the history of the repository.

此外,分支与文件夹无关。Git 总是同时跟踪整个存储库及其所有文件夹和文件。分支只不过是指向存储库历史记录中的单个提交或快照的指针。

回答by slash28cu

Yes just checkout another branch(maybe master) and then:

是的,只需签出另一个分支(可能是主分支),然后:

git checkout master
git branch -d thebran

回答by gitlinggun

But in my local repository, those branches are still showing up (even though they show up as having no changes).

但是在我的本地存储库中,这些分支仍在显示(即使它们显示为没有更改)。

Branches will show up in your local repo unless you delete them. They don't disappear when you push them.

除非您删除它们,否则分支将显示在您的本地存储库中。当你推动它们时,它们不会消失。

The answers above tell you how to delete a branch. However, I would add that using the -D option is a bit more powerful. This option deletes the branch regardless of its (un)merged status:

上面的答案告诉您如何删除分支。但是,我要补充一点,使用 -D 选项会更强大一些。无论其(未)合并状态如何,此选项都会删除分支:

git branch -D branchName

It's the atomic bomb of branch deletion. Use with care.

它是分支删除的原子弹。小心使用。

回答by 101

If the branch you cannot delete is a worktree then you may need to delete that worktree first:

如果您无法删除的分支是工作树,那么您可能需要先删除该工作树:

rm -r MyMods
git worktree list    # shows MyMods
git worktree prune
git worktree list    # doesn't show MyMods
git branch -d MyMods