git Branch -m 对其他开发人员有副作用吗?

发布于 2024-07-16 08:05:18 字数 484 浏览 8 评论 0原文

我们已经已经学会了如何使用git切换哪个分支指向什么分支-m。 如果我这样做,是否会让其他从我的存储库中提取数据的人的生活变得困难?

假设我在分支 topic1 上做了很多事情,然后做了 a

git branch -m master old_master
git branch -m topic1 master
git push origin master

,然后其他人从我的远程存储库中提取了 master,他们需要做什么才能完成所有工作指向正确的地方吗? 我需要告诉每个人重复我的步骤吗?

这是否类似于在推送提交后重新确定提交并让其他开发人员留下悬空对象的问题?

We've already learned how to switch which branch points to what using git branch -m. If I do this, is it going to make life difficult for the other people pulling from my repository?

Say I do a bunch of stuff on a branch topic1 and then do a

git branch -m master old_master
git branch -m topic1 master
git push origin master

and then somebody else pulls master from the my remote repository, what will they have to do to make everything point to the right place? Will I have to tell everybody to repeat my steps?

Is this akin to the problem of rebasing commits after pushing them and leaving other developers with dangling objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘落散花 2024-07-23 08:05:18

我不太确定你的仓库是什么样子,但这是最坏的情况。

假设您的 origin 存储库如下所示

origin:
o---o---A---B---C  master

并且您的本地存储库如下所示,

JimPuls:
o---o---A---B---C  master, origin/master
         \
          D---E---F topic1

然后,在您的分支重命名后,您的本地存储库如下所示:

JimPuls:
o---o---A---B---C  old_master, origin/master
         \
          D---E---F master

现在,当您将 master 推送到 < code>origin 这将是一个非快进更新。 推送后,origin 存储库将如下所示:

origin:
o---o---A...B...C  (B & C are orphaned commits)
         \
          D---E---F master

这对于可能在 C 上完成提交的朋友来说可能很残酷。 例如,如果 Sally 正在与您一起工作,她的存储库可能如下所示:

Sally:
o---o---A---B---C  origin/master
                 \
                  G---H---I master

现在,如果您执行非快进推送并且 Sally 执行 fetch,她的存储库将如下所示:

Sally:
          D---E---F  origin/master
         /
o---o---A---B---C  
                 \
                  G---H---I  master

现在 Sally 有找出如何将她的工作(G、H、I)放回到存储库中。 如果她只是与 origin/master 进行合并,那么 B 和 C 中的更改将返回到存储库中(哎呀!)。 相反,她必须cherry-pickrebase她的GHI更改到origin/master

Git 允许你这样做很酷,但这有点自找麻烦。 你真的希望莎莉注意到这种情况。 这就是为什么您在执行此操作时应该警告所有其他贡献者,以便他们能够适当地处理更改。

注意:以上是最坏的情况。 如果您的 topic1 分支在 C 处从 master 出发,那么该更改是快进,不会有任何问题。

I'm not exactly sure what your repo looks like but here's the worst-case scenario.

Suppose your origin repository looks like this

origin:
o---o---A---B---C  master

And your local repository looks like this,

JimPuls:
o---o---A---B---C  master, origin/master
         \
          D---E---F topic1

Then, after your branch renames your local repository looks like this:

JimPuls:
o---o---A---B---C  old_master, origin/master
         \
          D---E---F master

Now, when you push master to origin that'll be a non-fast-forward update. After the push, the origin repository will look like this:

origin:
o---o---A...B...C  (B & C are orphaned commits)
         \
          D---E---F master

This can be cruel to your friends who may have done commits on top of C. For example, if Sally was working with you her repository may look like this:

Sally:
o---o---A---B---C  origin/master
                 \
                  G---H---I master

Now, if you do your non-fast-forward push and Sally does a fetch her repository will look like this:

Sally:
          D---E---F  origin/master
         /
o---o---A---B---C  
                 \
                  G---H---I  master

Now Sally has to figure out how to get her work (G, H, I) back into the repository. If she simply does a merge with origin/master then the changes in B and C will be back in the repository (oops!). Instead, she'll have to cherry-pick or rebase her G-H-I changes onto origin/master.

It's cool that Git lets you do that but it's kind of asking for trouble. You're really hoping that Sally notices the situation. This is why you should warn all the other contributors when you do this so they can deal with the change appropriately.

NOTE: the above is a worst-case scenario. If your topic1 branch departed from master at C then that change is a fast-forward and there are no problems.

吻泪 2024-07-23 08:05:18

基本上,您的操作与以下内容相同:

# git checkout master
# git reset --hard topic1
# git push origin master

并且它们将具有完全相同的效果:其他人都将获得 topic1 分支(不过,它被命名为 master )及其祖先直到 mastertopic1 第一次出现分歧。 旧的 master 分支就躺在他们的存储库中,并将在将来的某个时候被垃圾收集,因为没有任何东西指向它了。

如果 topic1 是源自 master 当前 HEAD 的分支,那么您在这里就可以了。 否则,您将陷入“重写历史”的情况,这可能会弄乱您的标签。 您需要仔细考虑您真正想要实现的目标。 也许一个简单的 git merge 会更好地为您服务?

Basically your operations are the same as:

# git checkout master
# git reset --hard topic1
# git push origin master

And they will have exactly that effect: Everybody else will get the topic1 branch (it’s named master for them, though) and its ancestry up to the point where master and topic1 first diverged. The old master branch is then lying around in their repositories and will be garbage collected at some point in the future because nothing points to it anymore.

If topic1 is a branch that originated from the current HEAD of master you will be fine here. Otherwise you will get into the “rewriting history” situation which can make a mess of your e.g. your tags. You need to think carefully about what you’re really trying to achieve. Maybe a simple git merge will serve you better?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文