与其制作一个裸克隆,我更喜欢制作一个捆绑包(参见“如何通过电子邮件发送git存储库?”),它会生成一个文件,更容易复制(例如在USB闪存驱动器上)。
好处是它具有一些裸仓库的特性:您可以从中拉取或克隆它,但只需要担心一个文件。
machineB$ git clone /home/me/tmp/file.bundle R2
origin
" 的远程,让你可以从该捆绑包中提取和拉取。在 R2
中的 $GIT_DIR/config
文件将会有如下的条目:[remote "origin"]
url = /home/me/tmp/file.bundle
fetch = refs/heads/*:refs/remotes/origin/*
machineA$ cd R1
machineA$ git bundle create file.bundle lastR2bundle..master
machineA$ git tag -f lastR2bundle master
machineB$ cd R2
machineB$ git pull
请查看这篇博客文章"Synchronizing Git repositories without a server "(作者:Victor Costan)。
本文介绍一种在没有连接两个主机的网络服务器的情况下,在两个存储库之间推送更改的方法。
首先在 USB 硬盘上创建一个存储库。
mkdir /path/to/usb/stick/repository.git
git clone --local --bare . /path/to/usb/stick/repository.git
然后将存储库注册为远程存储库到USB存储设备,并将所需的分支推送到该存储库(如果您不想推送主分支,则替换为所需的分支)。
git remote add usb file:///path/to/usb/stick/repository.git
git push usb master
将来,您可以将USB存储库视为任何其他远程存储库。只需确保它已挂载 :) 例如,以下操作将新更改推送到USB存储库。
git push usb
在接收端,挂载USB存储设备,并使用文件URL来访问代码库。
file:///path/to/usb/stick/repository.git
# 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
git clone --no-local --no-hardlinks --bare . /path/to/repository.git/
。之后,我们两个人就能像任何其他远程一样推送和拉取了。 - XML~/repo1 $ git remote add repo2 ~/repo2
~/repo1 $ git fetch repo2
~/repo1 $ git merge repo2/foo
我想为事情添加一点变化。其他帖子中的信息似乎是正确的,但我想提到一些我在实践中做的额外事情。
如果我这样做
git remote -v
USB_F file:///f/Git_repositories/projectname.git (fetch)
USB_F file:///f/Git_repositories/projectname.git (push)
USB_G file:///g/Git_repositories/projectname.git (fetch)
USB_G file:///g/Git_repositories/projectname.git (push)
基本上,我定义了几个远程设备,使用 USB 名称而不是只使用一个建议的名称,因为分配给我的 USB 设备的驱动器号取决于我将其插入的端口。
然后,我运行一个类似以下内容的脚本:
cd /path/to/projectname
if [ -d /f/Git_repositories/projectname.git ]
then
git push USB_F --all
git push USB_F --tags
fi
if [ -d /g/Git_repositories/projectname.git ]
then
git push USB_G --all
git push USB_G --tags
fi
意图是将所有分支和标签推送到USB存储库,如果存在并且无论在哪里都可以。 (-d标志检查git存储库目录的存在,并且仅在目录存在时执行条件代码。)
原始问题说:每个人都有一些本地分支。 我不想将这些分支发送到远程服务器,只需将它们保留在本地。 如何同步...
push -all和push --tags命令会进行此同步,确保将所有分支和标记推送到USB存储库,即使是USB存储库不知道的新分支和标记。 没有默认为主分支或需要知道分支名称并逐个处理它们。
我出于备份目的运行此操作,因此仅显示了推送方面,并减少了项目和存储库的数量。 实际上,我将多个项目备份到多个位置,但这里只涉及重复的USB项目。
另一个相当明显但我没有看到提到的事情是,为了同步PC A和PC B,您需要
1. sync PC A and the USB device
2. sync PC B and the USB device
3. sync PC A and the USB device again!
或者换个角度,继续
PC A -> USB -> PC B
PC B -> USB -> PC A
这样最终两台机器上的分支和标签才会相同。