GIT 将文件从 dev 重新合并到 master

发布于 2024-10-08 13:12:31 字数 113 浏览 5 评论 0原文

我的主 GIT 分支接缝有一些错误,因此我想重新检查、重新合并或可能将我的 dev 分支克隆到 master 分支上,这样 master 分支将是 dev 的副本。

我怎样才能做到这一点? 谢谢。

My master GIT branch seams to have some errors thus I'd like to recheck, re-merge or possibly clone my dev branch over the master branch so the master branch would be a copy of dev.

How can I do that?
Thx.

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

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

发布评论

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

评论(3

撩动你心 2024-10-15 13:12:31

如果问题只是您签出的文件与分支不匹配,只需正常使用 git reset 即可:

git reset --hard HEAD

这应该就是您所需要的。但是,如果您仍然想用 dev 覆盖 master,请继续阅读。


如果你想用 dev 分支的内容覆盖你的 master 分支,请使用 git reset ,如下所示:

$ git checkout master
$ git reset --hard dev

然后如果你想将其推送到其他地方:

$ git push origin master

请注意,如果你的 dev 分支速度不快-从你的 master 分支转发(我猜它不会,因为你说你的 master 分支中有一些搞砸的东西),你需要添加 --force标记推送以在远程覆盖它:

$ git push origin master --force

但是请注意,这可能涉及重写历史记录的所有正常警告 - 如果其他人使用此远程,他们将需要处理相当于上游变基的情况。


为了避免将来出现此问题,请建议您的朋友几乎不需要使用 --force。如果他们在尝试 git Push 时遇到冲突,他们应该首先 git pull,解决冲突,然后再 git Push

If the problem is simply that your checked-out files don't match up with the branch, just use git reset normally:

git reset --hard HEAD

That should be all you need. However, if you still want to overwrite master with dev, read on.


If you want to overwrite your master branch with the contents of your dev branch, use git reset like so:

$ git checkout master
$ git reset --hard dev

And then if you want to push this somewhere else:

$ git push origin master

Note that if your dev branch doesn't fast-forward from your master branch (which I'm guessing it won't, since you said that your master branch has some screwed up stuff in it), you'll need to add the --force flag to the push to overwrite it on a remote:

$ git push origin master --force

Note, however, that this can involve all of the normal caveats of rewriting history a la git rebase - if anyone else uses this remote, they'll need to deal with the equivalent of an upstream rebase.


To avoid this problem in the future, advise your friend that using --force is almost never necessary. If they're getting conflicts when they try to git push, they should git pull first, resolve the conflicts, and then git push.

蘑菇王子 2024-10-15 13:12:31

从您的评论来看,似乎存在三种可能的情况:

  1. 主索引包含正确的代码,而工作副本已损坏。
  2. 工作副本很好,但索引现在已损坏。
  3. 两者都坏了。

首先,备份一切。然后:

在情况 1 中,使用 git reset --hard HEAD 丢弃损坏的工作副本。

在情况 2 中,添加并提交工作副本中的所有内容。

在情况 3 中,使用 git reset --hard dev 丢弃索引和工作副本。或者,git reset --hard SOME_COMMIT_ID_THAT_ISN'T_BROKEN

在这三种情况下,告诉你的朋友永远不要使用--force,除非他们真的知道自己在做什么。

另外,最好不要推送到非裸存储库(即具有工作副本的存储库)。我建议您在某个地方设置一个裸存储库,您可以从中推送和拉取,而不是直接推送到具有工作副本的存储库中。使用 git init --bare 来创建一个裸存储库。

我有没有提到备份所有内容?好的。做吧。

From you comments it seems there are three possible cases:

  1. The master index contains correct code and the working copy is broken.
  2. The working copy is fine and the index is now broken.
  3. Both are broken.

First, back everything up. Then:

In case 1, use git reset --hard HEAD to throw away the broken working copy.

In case 2, add and commit everything in the working copy.

In case 3, use git reset --hard dev to throw away both the index and the working copy. Alternatively, git reset --hard SOME_COMMIT_ID_THAT_ISN'T_BROKEN

In all three cases tell your friend to never use --force unless they really know what they're doing.

Also, it's probably best not to push into a non-bare repository (i.e. one with a working copy). I'd suggest you set up a bare repository somewhere that you both push and pull from rather than pushing directly into a repo that has a working copy. Use git init --bare to create a bare repo.

Did I mention back everything up? Good. Do it.

街角卖回忆 2024-10-15 13:12:31

好问题。我之前就想这样做,但从未这样做过。我最好的猜测可能是尝试以下方法。 注意我不确定这对您是否有效,因为我自己还没有这样做过。

git checkout master

git pull origin dev

git commit -a -m "reverted to dev"

按照上述方式进行操作很可能会引起冲突。

Good question. I have wanted to do this before myself and I never did. My best guess might be to try the below. NOTE I'm not sure how well this will work for you since I have not done it myself.

git checkout master

git pull origin dev

git commit -a -m "reverted to dev"

Doing it the above way will likely cause conflicts.

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