GIT 将文件从 dev 重新合并到 master
我的主 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果问题只是您签出的文件与分支不匹配,只需正常使用 git reset 即可:
这应该就是您所需要的。但是,如果您仍然想用 dev 覆盖 master,请继续阅读。
如果你想用 dev 分支的内容覆盖你的 master 分支,请使用 git reset ,如下所示:
然后如果你想将其推送到其他地方:
请注意,如果你的 dev 分支速度不快-从你的 master 分支转发(我猜它不会,因为你说你的 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: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:And then if you want to push this somewhere else:
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: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 togit push
, they shouldgit pull
first, resolve the conflicts, and thengit push
.从您的评论来看,似乎存在三种可能的情况:
首先,备份一切。然后:
在情况 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:
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.
好问题。我之前就想这样做,但从未这样做过。我最好的猜测可能是尝试以下方法。 注意我不确定这对您是否有效,因为我自己还没有这样做过。
按照上述方式进行操作很可能会引起冲突。
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.
Doing it the above way will likely cause conflicts.