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

发布于 2024-08-29 08:13:38 字数 755 浏览 3 评论 0原文

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

有没有办法使用 git pull 说“完全丢弃我的东西,只需为我制作另一个远程克隆”?我仍然想保留我自己的存储库并保留它的历史记录,但我想在该命令之后拥有 someRemote 的 master 分支的 1:1 副本。

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

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

所以我创建了自己的存储库作为 RM1 的克隆。然后我快乐地发展,RM也快乐地发展,但是我们从不分享我们的工作。 MY3之后我发现我的分行不是那么好,但是RM4已经很不错了。所以我想git pull RM4 到我的。但是,我不希望 MY1-3 中的更改持续存在,我希望 MY4 是 RM4 的 1:1 副本。

然而,我想保留我的历史记录,理想情况下我想在 MY3 和 RM4 之间或在 MY3 和 RM2-4 之间设置零钱。

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

这可能吗?

(这是针对 GitHub 项目的,我可能会分叉一个项目,进行一些实验,将其单独放置几周,然后想更新它而不保留我的更改。目前,我删除了我的分叉并重新分叉,这不是'是最好的方法。)

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

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.

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

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 pull RM4 into MY. However, I don't want my changes in MY1-3 to persist, I want MY4 be a 1:1 copy of RM4.

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.

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.)

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

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

发布评论

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

评论(3

蒗幽 2024-09-05 08:13:38

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

git branch -m master my_old_master

然后,创建一个新的 master:

git checkout -b master someRemote

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

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).

能怎样 2024-09-05 08:13:38

听起来您正在执行 git checkout,这将丢弃您对路径的本地更改。

您可以使用 checkout 恢复更改:

git checkout myfile.h

将从索引恢复 myfile.h

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

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

You can revert your changes using checkout:

git checkout myfile.h

will restore myfile.h from the index

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

音盲 2024-09-05 08:13:38

如果您想保留当前的主分支但注册新版本(镜像远程分支),您可以;

  • 注册合并过滤器驱动程序(仅用于此合并:a keepTheir one)
  • 执行 git pull --no-commit
  • 检查是否没有额外的文件需要删除(master 中的文件)远程分支中不存在)
  • 提交

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

  • register a merge filter driver (just for this merge: a keepTheir one)
  • 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文