简体   繁体   English

Git:主复位后分支会发生什么

[英]Git: What happens to a branch after a master reset

Assume I have a master branch with some commits I want to keep for later documentation and reference but remove them from the master (because I want the master to be on the same state as the upstream repo). 假设我有一个主分支,我希望保留一些提交以供以后的文档和引用,但是从master中删除它们(因为我希望master与上游repo处于相同的状态)。

My approach now is, to 我现在的方法是,到

  1. Create a new branch from current master state 从当前主状态创建新分支
  2. Reset (--hard) the master to a state which is present in the upstream repo as well 将主服务器重置( - 硬)到上游存储库中存在的状态

Now, my question is: 现在,我的问题是:

  • What happens to the new branch when removing the reference commit from the master? 从主服务器中删除引用提交时,新分支会发生什么?
  • Or am I completely wrong in the understanding of how branches are referenced. 或者在理解如何引用分支时我完全错了。

Often branches are shown in the following way, where (to my understanding) D is the basis of the new branch. 通常以下列方式显示分支,其中(据我所知) D是新分支的基础。

A - B - C - D    (master)
             \
                 (new branch)

Is the branch "rebased" automatically or how would you call it? 分支机构是否自动“重新定位”或您将如何调用它? Would it then look like this? 它会是这样的吗?

A - B         (master)
     \
      C - D   (new branch)

Last and maybe most general question: 最后也许是最一般的问题:

  • Is my approach of keeping the state in a new branch and reset --hard the master branch the right way to achieve my goal of getting my fork back to the upstream state (commit B ) without having my commits ( C and D ) merged? 我的方法是将状态保持在一个新的分支并reset --hard主分支是否正确实现了我的目标,即在不将我的提交( CD )合并的情况下将我的分支恢复到上游状态(提交B )?

Nothing will "happen" to the branch, since a branch in git is just a lightweight, movable, disposable pointer. 没有什么会“发生”到分支,因为git中的分支只是一个轻量级,可移动的一次性指针。 Commits are the real thing. 提交是真实的。

Yes, the overall plan is good , make a new branch to keep the most recent commits, reset master to where it should (I guess origin/master ), and this ref on your C,D commits will allow them to stay permanently . 是的,整体计划是好的 ,建立一个新的分支来保持最近的提交,将master重置到它应该的位置(我猜origin/master ),并且在你的C,D提交上的这个ref 将允许它们永久保留

Without creating the branch, they would ultimately be garbage collected, even if they'd stay in the reflog for a while. 如果不创建分支,它们最终将被垃圾收集,即使它们在reflog中停留了一段时间。

And also no, your C and D commits won't be merged into your upstream master if you follow your announced course of action. 并且不,如果您遵循宣布的行动方案,您的C和D提交将不会合并到您的上游主人。 Go for it. 去吧。

What happens to the new branch when removing the reference commit from the master? 从主服务器中删除引用提交时,新分支会发生什么?

Nothing. 没有。

Your question seems based on a misunderstanding of what a branch is. 你的问题似乎是基于对分支的误解。 A branch is a type of ref - different from other refs only in that git has a few conventions about where branches point and how they move. 分支是一种ref - 与其他ref不同,只是因为git有一些关于分支指向和移动方式的约定。 A ref is a pointer to a commit[1]. ref是指向commit [1]的指针。

When you reset master , you're just changing the pointer that is the master ref so that it stops pointing at D and starts pointing at B . 当您重置master ,您只需更改作为master ref的指针,使其停止指向D并开始指向B That doesn't affect the pointer that is the new branch ref in any way. 这不会以任何方式影响作为new branch ref的指针。

Commits C and D still exist and are also unaffected by resetting master . 提交CD仍然存在,也不受重置master影响。 It's just that master is no longer pointing to a place from which they can be "reached" (whereas before D could be reached because it's what master pointed to, and C could be reached via D s parent pointer). 只是master不再指向一个可以“到达”的地方(而在达到D之前,因为它是master指向的,而C可以通过D的父指针到达)。

But new branch still points at D , so it can still reach C and D . 但是new branch仍然指向D ,所以它仍然可以达到CD

So new branch isn't rebased or anything. 所以new branch没有重新定位或任何东西。 Rebasing is rewriting and replacing commits because you want to make the same changes they made, relative to a different starting point. 重新引用是重写和替换提交,因为您希望相对于不同的起点进行相同的更改。 That isn't happening here. 这不会发生在这里。 It's commits that are rebased, and often when rebasing commits a ref goes along for the ride; 它的承诺是重新定位的,并且通常在变形提交时,ref会继续骑行; but when we say 'rebase a branch' that's kind of a shorthand for 'rebase some commits currently reachable from the branch, and then move the branch to point at the new commits'. 但是,当我们说'rebase a branch'这是一种“rebase”的简写时,可以从分支中获得一些提交,然后将分支移动到指向新提交的位置。 But here we don't need any of that; 但在这里我们不需要任何这些; we still have our original commits. 我们仍然有我们的原始提交。

The other side of 'a branch is just a pointer' - commits are not "part of" any branch. '分支的另一面只是一个指针' - 提交不是任何分支的“一部分”。 They exist independent of any branches that might reference them (though git gc will eventually try to dispose of them if it thinks nobody knows how to find them anymore). 它们的存在独立于任何可能引用它们的分支(尽管git gc最终会尝试处理它们,如果它认为没有人知道如何找到它们的话)。 The commit a branch points to, and the commits that can be reached from there via parent pointers, are said to make up the branch's history... but that's as far as the relationship goes. 分支指向的提交,以及可以通过父指针从那里到达的提交,据说构成了分支的历史......但这就是关系的结果。

So to reiterate and summarize - resetting master only moves a pointer . 所以重申并总结一下 - 重置master 只会移动一个指针 It doesn't change the commits and doesn't affect other branches. 它不会更改提交,也不会影响其他分支。


[1] Some refs can occasionally point to something other than a commit, but that's not too important to this discussion; [1]有些裁判偶尔会指出除了提交之外的东西,但这对于这个讨论来说并不重要; tl;dr - a branch is a pointer to a commit and nothing more tl; dr - 分支是指向提交的指针,仅此而已

提交链保持活着,而某些东西指向它们,或者直到它们被git gc擦除,并且您的前/后重置拓扑图是正确的。

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

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