如何同步两个git存储库 [英] How to synchronize two git repositories

查看:154
本文介绍了如何同步两个git存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的PC上有两个git存储库。我每个人都有一些当地的分支机构。我不想将这些分支发送到远程服务器,只是将它们保留在本地。如何在不使用网络的情况下同步?我可以在一台PC上压缩存储库并移动到另一台PC上吗?那安全吗?也许我可以通过某种方式导出每个分支的最新变化? 解决方案

(请参阅如何给某人发送一个git仓库?),它会生成一个文件,比较容易复制(例如在U盘上)。

这样做的好处是确实具有裸回购的一些特性:您可以从中取出或克隆它。

但是只需要担心一个文件。

  machineB $ git clone /home/me/tmp/file.bundle R2 




这将在结果库中定义一个名为 origin 的远程对象,它允许您从包中获取和拉取。 R2 中的 $ GIT_DIR / config 文件将包含如下条目:



  [remoteorigin] 
url = /home/me/tmp/file.bundle
fetch = refs / heads / *:refs / remotes / origin / *




要更新产生的mine.git存储库,可以在使用增量更新替换存储在 /home/me/tmp/file.bundle 中的包后进行读取或拉取。



在原始存储库中完成更多工作后,您可以创建一个增量包来更新其他存储库:



  machineA $ cd R1 
machineA $ git bundle create file.bundle lastR2bundle..master
machineA $ git tag -f lastR2bundle master




然后,您将该包传送到另一台机器以替换 /home/me/tmp/file.bundle ,并从中拉出。




  machineB $ cd R2 
machineB $ git pull


I have two git repositories on different PCs. I have some local branches on every one of them. I don`t want to send this branches to remote server, just keep them local. How can I synchronize then without using a web? Can I just zip repository on one PC and move to another? Is that safe? Maybe I can export somehow newest changes from every branch?

解决方案

Rather than making a bare clone, I prefer making a bundle (see "How can I email someone a git repository?"), which generates one file, easier to copy around (on an USB stick for instance)

The bonus is that is does have some of the characteristics of a bare repo: you can pull from it or clone it.
But only have to worry about one file.

machineB$ git clone /home/me/tmp/file.bundle R2

This will define a remote called "origin" in the resulting repository that lets you fetch and pull from the bundle. The $GIT_DIR/config file in R2 will have an entry like this:

[remote "origin"]
    url = /home/me/tmp/file.bundle
    fetch = refs/heads/*:refs/remotes/origin/*

To update the resulting mine.git repository, you can fetch or pull after replacing the bundle stored at /home/me/tmp/file.bundle with incremental updates.

After working some more in the original repository, you can create an incremental bundle to update the other repository:

machineA$ cd R1
machineA$ git bundle create file.bundle lastR2bundle..master
machineA$ git tag -f lastR2bundle master

You then transfer the bundle to the other machine to replace /home/me/tmp/file.bundle, and pull from it.

machineB$ cd R2
machineB$ git pull

这篇关于如何同步两个git存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆