跨机器同步 Git 存储库,无需推送

发布于 2024-08-11 18:15:26 字数 728 浏览 6 评论 0原文

我有三台计算机,它们的所有应用程序都应具有相同的设置。 我登录时才拉取。

由于所有这些进程都有使用这些设置运行的进程,因此我从不想要推送,相反,我希望它们中的每一个都跟踪其他两个,并且仅在 在这方面工作了几天,我发现的所有文章似乎都假设您想要先推送到中央存储库,然后再从其他机器上的中央存储库中提取数据,但这似乎浪费空间并且(传输)时间。有没有任何指南可以清楚地解释如何做这样的事情?

编辑 2:Pat Notz 给出了纠正 .git/config 的必要提示:

[branch "master"]
  remote = machine2
  merge = refs/heads/master
[remote "machine1"]
  url = ssh://192.168.0.4/~/settings
  fetch = +refs/heads/*:refs/remotes/machine1/*
[remote "machine2"]
  url = ssh://machine2/~/settings
  fetch = +refs/heads/*:refs/remotes/machine2/*

编辑 3:非常感谢您的回答。结果可以在单独的 博客文章

I've got three computers which should have the same settings for all their applications. Since all of them have processes running using those settings, I never want to push, instead I'd like for each of them to track the other two, and pull only when I'm logged in.

After working a couple of days on this, all the articles I've found seem to assume that you'd want to push to a central repository before pulling from that at the other machines, but that seems like a waste of space and (transfer) time. Are there any guides which can explain clearly how to do something like this?

Edit 2: Pat Notz gave the necessary tip to correct .git/config:

[branch "master"]
  remote = machine2
  merge = refs/heads/master
[remote "machine1"]
  url = ssh://192.168.0.4/~/settings
  fetch = +refs/heads/*:refs/remotes/machine1/*
[remote "machine2"]
  url = ssh://machine2/~/settings
  fetch = +refs/heads/*:refs/remotes/machine2/*

Edit 3: Thank you very much for the answers. The result can be found in a separate blog post.

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

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

发布评论

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

评论(2

白龙吟 2024-08-18 18:15:26

如前所述,git pull 是一个很好的答案。 pull 命令本质上是 fetchmerge 的组合;前者会将所有远程提交作为(可能是新的)分支引入您的存储库,而后者将该分支合并到您当前的分支中。当然,确定哪个分支将得到合并是有一点技巧的。一般来说,您必须针对每个存储库进行配置。 Git 确实有一种特殊情况,即当前签出的分支正在跟踪您从中提取的存储库中的远程分支,并且该远程分支是唯一发生更改的分支,此时 Git 将简单地假设您想要将远程分支与当前分支合并并自动执行。

除了一些相当不透明的配置之外,pull 还有一些其他问题值得一提。最值得注意的是:它与合并命令相关联。换句话说,如果您拉取远程更改,并且您在本地分支中有一些自己的更改,Git 将被迫执行合并以统一两个分支。原则上,这很好,但它会对您将来可能想做的任何变基造成严重破坏。您提到您的用例是您自己的三台计算机。如果我是你,我会尽量将我的历史记录保持在三个分支的同一分支中,并尽可能保持线性。不要将机器A合并到机器B中,将B的更改重新建立在A的更改之上在该逻辑分支上生成单个线性历史记录。

为此,您必须直接使用 git fetch 命令,而不是通过 pull 进行操作。更具体地说,您需要执行以下操作:

git fetch A
git rebase A/master

将“A/master”替换为您在本地跟踪的远程分支的名称。本地存储库中的任何更改都将在 A/master 的头部重新设置父级,为您提供线性历史记录,而不是短暂分歧以稍后合并一些提交的历史记录。

As mentioned, git pull is a good answer here. The pull command is essentially a combination of fetch and merge; the former will bring all of the remote commits into your repository as a (possibly new) branch, while the latter will merge that branch into your current branch. Of course, determining which branch will get the merge is a bit of a trick. In general, you have to configure this on a per-repository basis. Git does have a special case for when the branch currently checked-out is tracking a remote branch in the repository you pull from, and that remote branch is the only one which has changes, at which point Git will simply assume you want to merge the remote branch with the current one and do it automatically.

Aside from some rather opaque configuration, pull has some other issues with are worthy of mention. Most notably: it's tied to the merge command. In other words, if you pull in the remote changes and you have some changes of your own in your local branch, Git will be forced to perform a merge in order to unify the two branches. In principle, this is just fine, but it plays havoc with any rebasing you may want to do at some point in the future. You mentioned that your use case is three of your own computers. If I were you, I would try to keep my history in the same branch across the three as linear as possible. Don't merge machine A into machine B, rebase the changes of B on top of the changes of A to produce a single, linear history on that logical branch.

In order to do this, you will have to use the git fetch command directly, rather than working through pull. More specifically, you will want to do something like this:

git fetch A
git rebase A/master

Replace "A/master" with the name of the remote branch which you are tracking locally. Any changes in your local repository will be reparented on the head of A/master, giving you a linear history rather than one which diverges briefly only to merge a few commits later.

梦明 2024-08-18 18:15:26

你可以直接拉。也就是说,你在机器 A 上有一些新的提交,现在你在机器 B 上,然后你只需从 A 上拉取,就像

git pull A

git pull 文档有很多有用的示例和用法,包括使用 遥控器

You can just pull. I.e. you have some new commits on machine A and now you are on machine B, then you just pull from A, like

git pull A

The git pull documentation has a lot of useful examples and usages including the use of remotes.

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