我可以告诉 git pull 覆盖而不是合并吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2665045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:16:04  来源:igfitidea点击:

Can I tell git pull to overwrite instead of merge?

gitgit-pull

提问by Michael Stum

As far as I see, git pull someRemote mastertries to merge the remote branch into mine.

据我所知,git pull someRemote master尝试将远程分支合并到我的分支中。

Is there a way to say "Completely discard my stuff, just make me another clone of the remote" using git pull? I still want to keep my own repository and keep it's history, but I want to have a 1:1 copy of someRemote's master branch after that command.

有没有办法使用 git pull 说“完全丢弃我的东西,让我再克隆一个遥控器”?我仍然想保留我自己的存储库并保留它的历史记录,但是我想在该命令之后拥有 someRemote 主分支的 1:1 副本。

To clarify, imagine there are 2 repositories, RM and MY. Numbers are commits, and this assumes only one branch (master).

澄清一下,假设有 2 个存储库,RM 和 MY。数字是提交,这假设只有一个分支(主)。

RM1 --- RM2 --- RM3 --- RM4 --- RM5 --- RM6 ...
|                        |
+-> MY1 --- MY2 --- MY3 -+-> MY4 --- MY5 --- MY6 ...

So I start my own repository as a clone of RM1. Then I develop happily and RM develops happily, but we never share our work. After MY3 I realize that my branch isn't that great but that RM4 is pretty good. So I want to git pullRM4 into MY. However, I don't want my changes in MY1-3 to persist, I want MY4 be a 1:1 copy of RM4.

所以我开始我自己的存储库作为 RM1 的克隆。然后我快乐地发展,RM快乐地发展,但我们从不分享我们的工作。在 MY3 之后,我意识到我的分店不是那么好,但 RM4 相当不错。所以我想把git pullRM4 换成 MY. 但是,我不希望 MY1-3 中的更改持续存在,我希望 MY4 是 RM4 的 1:1 副本。

However, I want to keep my history, ideally I would like to have a change set between MY3 and RM4 or between MY3 and RM2-4.

但是,我想保留我的历史记录,理想情况下,我希望在 MY3 和 RM4 之间或 MY3 和 RM2-4 之间设置更改。

It should still stay my repository.

它应该仍然保留我的存储库。

Is that possible?

那可能吗?

(This is for GitHub projects where I may fork a project, experiment a bit, leave it alone for a few weeks but then want to update it without keeping my changes. At the moment I delete my fork and re-fork, which isn't the best approach.)

(这是针对 GitHub 项目的,我可能会在其中 fork 一个项目,进行一些实验,将其放置几周,然后想在不保留更改的情况下更新它。目前我删除了我的 fork 并重新 fork,这不是t 最好的方法。)

回答by Greg Hewgill

First, rename your master branch to something else:

首先,将您的主分支重命名为其他名称:

git branch -m master my_old_master

Then, create a new master:

然后,创建一个新的主:

git checkout -b master someRemote

The great thing about Git's branch names is that they aren't actual places themselves, they're just pointers to places (where a "place" is a SHA1 commit id).

Git 的分支名称的伟大之处在于它们本身并不是实际的位置,它们只是指向位置的指针(其中“位置”是 SHA1 提交 ID)。

回答by Michael Shimmins

Sounds like you're after git checkout, which will discard your local changes to a path.

听起来像你在 git checkout 之后,这将丢弃你对路径的本地更改。

You can revert your changes using checkout:

您可以使用 checkout 还原更改:

git checkout myfile.h

will restore myfile.h from the index

将从索引中恢复 myfile.h

http://git-scm.com/docs/git-checkout

http://git-scm.com/docs/git-checkout

回答by VonC

If you want to keep your current master branch but register a new version (mirroring the remote branch), you can;

如果你想保留当前的 ​​master 分支但注册一个新版本(镜像远程分支),你可以;

  • register a merge filter driver (just for this merge: a keepTheirone)
  • do a git pull --no-commit
  • check if there aren't extra files that need to be removed (files in your master that were not present in the remote branch)
  • commit
  • 注册一个合并过滤器驱动程序(仅用于此合并:a keepTheirone
  • 做一个 git pull --no-commit
  • 检查是否没有需要删除的额外文件(远程分支中不存在的主文件中的文件)
  • 犯罪