如何同步两个git存储库

sza*_*man 34 git offline-mode

我在不同的PC上有两个git存储库.我每个人都有一些当地的分支机构.我不想将这些分支发送到远程服务器,只需将它们保存在本地.如何在不使用网络的情况下进行同步?我可以在一台PC上压缩存储库并转移到另一台PC吗?这样安全吗?也许我可以从每个分支导出某种最新的变化?

Von*_*onC 25

而不是制作一个裸克隆,我更喜欢制作一个(参见" 我如何通过电子邮件向某人发送一个git存储库? "),它生成一个文件,更容易复制(例如在USB记忆棒上)

奖金是确实具有裸仓库的一些特征:您可以从中拉出或克隆它.
但只需要担心一个文件.

machineB$ git clone /home/me/tmp/file.bundle R2
Run Code Online (Sandbox Code Playgroud)

这将origin在结果存储库中定义一个名为" " 的远程,它允许您从捆绑中获取和提取.该$GIT_DIR/config文件R2将具有如下条目:

[remote "origin"]
    url = /home/me/tmp/file.bundle
    fetch = refs/heads/*:refs/remotes/origin/*
Run Code Online (Sandbox Code Playgroud)

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

在原始存储库中进行更多工作之后,您可以创建增量包以更新其他存储库:

machineA$ cd R1
machineA$ git bundle create file.bundle lastR2bundle..master
machineA$ git tag -f lastR2bundle master
Run Code Online (Sandbox Code Playgroud)

然后将捆绑包转移到另一台机器进行更换/home/me/tmp/file.bundle,并从中拉出.

machineB$ cd R2
machineB$ git pull
Run Code Online (Sandbox Code Playgroud)


mpe*_*kov 22

请参阅此博客文章"在没有服务器的情况下同步Git存储库"(作者Victor Costan).

本文描述了一种在两个存储库之间推送更改的方法,而不使用具有与具有存储库的主机的网络连接的服务器

通过在USB记忆棒上创建存储库来启动.

mkdir /path/to/usb/stick/repository.git
git clone --local --bare . /path/to/usb/stick/repository.git
Run Code Online (Sandbox Code Playgroud)

然后将USB存储库上的存储库注册为远程存储库,并将所需的分支推送到它(如果您不想推送主服务器,请替换所需的分支).

git remote add usb file:///path/to/usb/stick/repository.git
git push usb master
Run Code Online (Sandbox Code Playgroud)

将来,您可以将USB存储库视为任何其他远程存储库.只需确保它已安装:)例如,以下内容将新更改推送到USB存储库.

git push usb
Run Code Online (Sandbox Code Playgroud)

在接收端,安装USB记忆棒,并使用存储库的文件URL

file:///path/to/usb/stick/repository.git
Run Code Online (Sandbox Code Playgroud)

一些方便的命令:

# cloning the repository on the USB stick
git clone file:///path/to/usb/stick/repository.git
# updating a repository cloned from the USB stick using the above command
git pull origin
# adding the USB stick repository as a remote for an existing repository
git remote add usb file:///path/to/usb/stick/repository.git
# updating from a remote repository configured using the above command
git pull usb master
Run Code Online (Sandbox Code Playgroud)


sag*_*age 5

将存储库直接复制到其他文件系统是裸克隆或捆绑的替代方法.复制后,您可以将复制的repo直接设置为本地远程 - 本地远程可能看起来不直观 - 获取并合并到第一个存储库.

即将repo2从第二台计算机合并到〜/ repo1,首先将repo2复制到repo1文件系统〜/ repo2(记忆棒,网络副本等),然后你可以使用Git的答案在两个本地存储库之间拉动更改:

~/repo1 $ git remote add repo2 ~/repo2
~/repo1 $ git fetch repo2
~/repo1 $ git merge repo2/foo
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为关于git的维基百科文章说:"Git存储库 - 数据和元数据 - 完全包含在其目录中,因此整个Git存储库的正常系统级复制(或重命名或删除)是安全的操作由此产生的副本既独立又不知道原件."

  • @nutty - 直接复制可以很好地将repo转移到它的确切状态,我经常这样做.但是,对于某些用例,我发现最好能够从一个磁盘位置拉到/合并到另一个磁盘位置(例如,当我编辑了两个repo位置并且我想要同步它们时).Git在这方面非常乐于助人.:-) (2认同)