简体   繁体   English

从 git 存储库中删除分离的分支

[英]Remove detached branches from git repository

I played with another remote called tmp .我玩了另一个名为tmp的遥控器。 I added the remote and pushed the branches master and new-branch .我添加了遥控器并推送了分支masternew-branch Then I deleted the the tmp remote.然后我删除了tmp遥控器。

Now the branches tmp/master and tmp/new-branch are detached.现在分支tmp/mastertmp/new-branch已分离。 I don't need them anymore.我不再需要它们了。 How do I delete them along with their commits?我如何将它们连同它们的提交一起删除?

Here is graph on my repo now:这是我的回购协议上的图表:

* a856b8c - (HEAD -> master, tag: abc, origin/master) Update                                                                                                                                                                                                  
| * 8a142e4 - (tmp/master) Update                                                                                                                                                                                                                             
|/                                                                                                                                                                                                                                                            
| * 5d43564 - (tmp/new-branch) New commit                                                                                                                                                                                                                     
| * f9f33d9 - New commit                                                                                                                                                                                                                                      
| * 9869631 - New commit                                                                                                                                                                                                                                      
| * 03cdb15 - New commit                                                                                                                                                                                                                                      
| * 7a24a77 - Test                                                                                                                                                                                                                                            
|/                                                                                                                                                                                                                                                            
* 97b8dcd - Skip lines

So I want to delete commits: 8a142e4, 5d43564 and so on from the repo's history.所以我想从 repo 的历史记录中删除提交:8a142e4、5d43564 等等。

Use:采用:

git remote remove tmp

(and you can stop here if you like; the rest is merely commentary). (如果你愿意,你可以停在这里;rest 只是评论)。 If that refuses to run because tmp is gone, use git remote add tmp https://any.url.here and then git remote remove tmp .如果因为tmp消失而拒绝运行,请使用git remote add tmp https://any.url.here然后git remote remove tmp

It's not entirely clear what you mean to say with the phrase "detached branch".还不完全清楚你用“分离分支”这个词的意思。 You did, however, have a tag, , which I snipped as it is not relevant to this particular setup.但是,您确实有一个标签 ,我剪掉了它,因为它与此特定设置无关。 These are not "detached HEAD" cases.这些不是“分离的 HEAD”案例。 Instead, these are simply stale names.相反,这些只是陈旧的名称。

One thing that is important here is that these are (presumably) remote-tracking names , or as Git calls them, remote-tracking branch names.这里重要的一件事是这些(大概)是远程跟踪名称,或者 Git 称它们为远程跟踪分支名称。 That means git branch -delete won't do the trick, but git branch -r --delete would (though as SebDieBln notes in a comment , you would also need --force ).这意味着git branch -delete不会解决问题,但git branch -r --delete会(尽管正如SebDieBln 在评论中指出的那样,您还需要--force )。

A remote , in Git, is simply a name—like origin or, in your case, tmp —that we use to have Git be able to refer to some other Git repository by name, rather than having to type in some long and tedious URL repeatedly. Git 中的remote只是一个名称,如origin或在您的情况下为tmp ,我们使用它来让 Git 能够通过名称引用其他一些 Git 存储库,而不必输入一些冗长乏味的 URL反复。 But besides just storing the URL, the existence of a remote provides another ability:但除了存储 URL 之外,遥控器的存在还提供了另一种能力:

  • Each repository has its own branch names.每个存储库都有自己的分支名称。
  • Each of these branch names stores one (1) commit hash ID.这些分支名称中的每一个都存储一 (1) 个提交 hash ID。

The fact that the name stores the hash ID gives us—and Git—an easy way to find the commit hash ID, and that's generally the first way we find commits: by some name.名称存储 hash ID 的事实为我们和 Git 提供了一种查找提交 hash ID 的简单方法,这通常是我们查找提交的第一种方式:通过某个名称。 (The second way is by finding a commit—usually through a name—and then working backwards from there to earlier commits, and the third way is by just somehow magically knowing the hash ID, perhaps because we saw it in git log earlier, which used the second method.) (第二种方法是找到一个提交——通常是通过一个名字——然后从那里向后工作到更早的提交,第三种方法是通过某种方式神奇地知道 hash ID,也许是因为我们之前在git log中看到了它,这使用了第二种方法。)

But in general, the names in some other Git repository aren't accessible to us all the time.但总的来说,我们无法一直访问其他Git 存储库中的名称。 We'd have to run git ls-remote or some similar command to find the hash IDs that go with their branch names.我们必须运行git ls-remote或一些类似的命令来找到 hash ID 和 go及其分支名称。 In ancient days (early 2005ish), people actually did this sort of thing.在古代(2005 年初),人们确实做过这种事情。 That got really tedious really quickly, like typing in long URLs, so Git was taught the idea of saving not only the URL, but also their branch names and hash IDs, via these remotes.这很快就变得非常乏味,就像输入长 URL 一样,所以 Git 被教导不仅要通过这些遥控器保存 URL,还要保存它们的分支名称和 hash ID。 So if you have a remote named origin and they , whoever they are over at origin , have a branch named xyzzy , you end up with an origin/xyzzy in your repository.因此,如果您有一个名为origin的远程服务器,而他们,无论他们在origin的谁,都有一个名为xyzzy的分支,您最终会在您的存储库中得到一个origin/xyzzy

These remote-tracking names essentially serve as a way for your Git repository to remember the branch names in some other Git repository .这些远程跟踪名称本质上用作 Git 存储库记住其他 Git 存储库中的分支名称的一种方式。 That's their reason to exist.这就是他们存在的理由。 The git fetch command builds these remote-tracking names, creating them and updating them whenever it gets a chance. git fetch命令构建这些远程跟踪名称,创建它们并在有机会时更新它们。 (The git push command will also update a name on successful push. Note that git pull means run git fetch , then run a second Git command and it's the fetch step of pull that updates remote-tracking names.) git push命令也会在成功推送时更新名称。请注意, git pull意味着运行git fetch ,然后运行第二个 Git 命令,它是更新远程跟踪名称的 pull 的 fetch 步骤。)

The git remote command is, in general, the user-facing command for manipulating remotes like origin or (here) tmp . git remote命令通常是面向用户的命令,用于操作像origin或(这里) tmp这样的遥控器。 A git remote add adds a new remote. git remote add添加一个新的遥控器。 A git remote remove removes the remote, and since the remote-tracking names exist on behalf of the remote, it also removes the remote-tracking names. git remote remove删除远程,并且由于远程跟踪名称代表远程存在,因此它也会删除远程跟踪名称。 1 1个

Last:最后的:

So I want to delete commits: 8a142e4, 5d43564 and so on from the repo's history.所以我想从 repo 的历史记录中删除提交:8a142e4、5d43564 等等。

Deleting the names by which Git finds those hash IDs will stop the commits from showing up in git log , but won't actually delete the commits.删除 Git 找到那些 hash ID 的名称将阻止提交显示在git log中,但实际上不会删除提交。 If you memorize their hash IDs and feed them to Git, it's very likely that you can still access those commits by their hash IDs, for at least 30 and perhaps 90 or more days.如果您记住他们的 hash ID 并将它们提供给 Git,您很可能仍然可以通过他们的 hash ID 访问这些提交至少 30 天,也许 90 天或更长时间。

Normally, deleting the names, so that you don't see the commits any more, is sufficient.通常,删除名称就足够了,这样您就不会再看到提交了。 If not, well, it's actually fairly difficult to truly delete a commit—not impossible, but not easy, and the methods don't really belong in this answer, so I will stop here.如果不是,好吧,真正删除提交实际上相当困难——不是不可能,但不容易,而且这些方法并不真正属于这个答案,所以我会在这里停下来。


1 I vaguely recall that in some very early Git version, git remote remove did not remove the corresponding remote-tracking names. 1依稀记得在某个很早的Git版本中, git remote remove并没有删除对应的remote-tracking names。 However, these versions of Git should no longer be in use by now.但是,这些版本的 Git 现在应该不再使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM