如何同步两个git仓库
我在不同的电脑上有两个 git 存储库。我在每一个地方都有一些分支机构。我不想将此分支发送到远程服务器,只需将它们保留在本地即可。那么如何在不使用网络的情况下进行同步呢?我可以只在一台电脑上压缩存储库并移动到另一台电脑吗?这样安全吗?也许我可以以某种方式从每个分支导出最新的更改?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与制作裸克隆相比,我更喜欢制作捆绑包< /strong> (请参阅“我如何向某人发送电子邮件git 存储库?”),它会生成一个文件,更容易复制(例如在 USB 记忆棒上)。
好处是它确实具有裸存储库的一些特征:您可以从中提取或克隆它,但您只需担心一个文件。
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 it does have some of the characteristics of a bare repo: you can pull from it or clone it, but you only have to worry about one file.
请参阅此博文“无需服务器同步 Git 存储库”(作者:维克多·科斯坦)。
通过在 USB 记忆棒上创建存储库来启动。
然后将 USB 记忆棒上的存储库注册为远程存储库,并将所需的分支推送到它(如果您不想推送 master,请替换您所需的分支)。
将来,您可以将 USB 存储库视为任何其他远程存储库。只需确保它已安装即可:) 例如,以下内容将新更改推送到 USB 存储库。
在接收端,安装 USB 记忆棒,并使用存储库的文件 URL
一些方便的命令:
See this blog post "Synchronizing Git repositories without a server " (by Victor Costan).
Start up by creating a repository on the USB stick.
Then register the repository on the USB stick as a remote repository, and push the desired branch to it (if you don't want to push master, substitute your desired branch).
In the future, you can treat the USB repository as any other remote repository. Just make sure it's mounted :) For instance, the following pushes new changes to the USB repository.
On the receiving end, mount the USB stick, and use a file URL for the repository
A few handy commands:
将存储库直接复制到其他文件系统是裸克隆或捆绑的替代方法。复制后,您可以将复制的存储库直接设置为本地远程(本地远程可能看起来不直观)以获取并合并到第一个存储库中。
即要将第二台计算机上的 repo2 合并到 ~/repo1,首先将 repo2 复制到 ~/repo2 处的 repo1 文件系统(记忆棒、网络复制等),然后您可以使用 Git 在两个本地存储库之间拉取更改:
这有效,因为 关于 git 的维基百科文章 说:“Git 存储库(数据和元数据)完全包含在其目录中,因此整个 Git 存储库的正常系统级复制(或重命名或删除)是一种安全操作,生成的副本独立于原始副本,并且不知道原始副本。”
Direct copy of a repository to the other file system is an alternative to bare clone or to bundle. After copying you can set the copied repo up directly as a local remote - unintuitive as local remote may seem - to fetch and merge into the first repository.
I.e. to merge repo2 from a second computer into ~/repo1, first copy repo2 to the repo1 file system at ~/repo2 (memory stick, network copy, etc.) and then you can use the answer to Git pulling changes between two local repositories:
This works because as the wikipedia article on git says: "A Git repository — data and metadata — is completely contained within its directory, so a normal system-level copy (or rename, or delete) of an entire Git repository is a safe operation. The resulting copy is both independent of and unaware of the original."
我只是想给事情添加一点变化。其他帖子中的信息似乎是正确的,但我想提一下我在实践中所做的一些额外的事情。
如果我这样做,
我会得到这样的信息
基本上,我已经定义了多个带有 USB 名称的遥控器,而不是仅按照建议定义一个,因为分配给我的 USB 设备的驱动器号会根据我插入的端口而变化。
然后,我运行一个包含如下内容的脚本。
目的是将所有分支和所有标签推送到 USB 存储库(如果存在)以及位置。 (-d 标志检查 git 存储库目录是否存在,并且条件代码仅在该目录存在时执行。)
最初的问题是:我在每个分支上都有一些本地分支。我不想将此分支发送到远程服务器,只需将它们保留在本地即可。 如何同步 ...
push -all 和 push --tags 命令执行此同步,确保所有分支和标签被推送到 USB 存储库,甚至是 USB 存储库不知道的新存储库。不需要默认master,也不需要知道分支的名称并一一处理。
我出于备份目的运行此程序,因此我只显示了事物的推送方面,并减少了项目和存储库的数量。实际上,我将多个项目备份到多个位置,但只有重复的 USB 项目与此处相关。
另一件相当明显但我没有提到的事情是,为了同步 PC A 和 PC B,您需要
或者以不同的方式查看,
以便最终分支和标签在两台计算机上相同。
I'd just like to add a little twist to things. The information in other posts seems right, but I'd like to mention a few extra things I do in practice.
If I do
I get information like this
Basically I have defined several remotes with USB names rather than just one as suggested since the drive letter allocated to my USB device changes depending on the port I insert it into.
I then run a script with contents like this
The intention is to push all branches and all tags to the USB repository if it exists and where ever it is. (The -d flag is checking for existence of the git repository directory and conditional code only executes if the directory exists.)
The original question said: 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 ...
The push -all and push --tags command do this synchronizing, making sure that all the branches and tags are pushed to the USB repository, even new ones that the USB repository was unaware of. There is no defaulting to master or needing to know the names of branches and handling them one by one.
I run this for backup purposes so I've only shown the push side of things, and reduced the number of projects and repositories. In reality I backup multiple projects to multiple locations, but it's only the repeated USB items that are relevant here.
Another thing that is rather obvious but that I have not seen mentioned, is that in order to sync PC A and PC B, you'd need to
Or viewed differently, go
so that ultimately the branches and tags are the same on the two machines.