移动时,计算机之间的 git 存储库同步?
假设我有一台台式电脑和一台笔记本电脑, 有时我在台式机上工作,有时我在笔记本电脑上工作。
来回移动 git 存储库的最简单方法是什么?
我希望 git 存储库是相同的, 这样我就可以在另一台计算机上继续我离开的地方。
我想确保两台计算机上都有相同的分支和标签。
谢谢 Johan
注意:我知道如何使用 SubVersion 执行此操作,但我很好奇这如何与 git 一起使用。如果更容易,我可以使用第三台电脑作为两台电脑可以同步的经典服务器。
注意:两台计算机都运行 Linux。
更新:
所以让我们在服务器上使用裸 git 存储库来尝试 XANI:s 的想法, 以及来自 KingCrunch 的推送命令语法。 在此示例中,有两个客户端和一台服务器。
因此,让我们首先创建服务器部分。
ssh user@server
mkdir -p ~/git_test/workspace
cd ~/git_test/workspace
git --bare init
然后,我尝试从其他一台计算机上使用克隆获取存储库的副本:
git clone user@server:~/git_test/workspace/
Initialized empty Git repository in /home/user/git_test/repo1/workspace/.git/
warning: You appear to have cloned an empty repository.
然后进入该存储库并添加一个文件:
cd workspace/
echo "test1" > testfile1.txt
git add testfile1.txt
git commit testfile1.txt -m "Added file testfile1.txt"
git push origin master
现在服务器已使用 testfile1.txt 进行更新。
无论如何,让我们看看是否可以从另一台计算机获取该文件。
mkdir -p ~/git_test/repo2
cd ~/git_test/repo2
git clone user@server:~/git_test/workspace/
cd workspace/
git pull
现在我们可以看到测试文件。
此时我们可以编辑更多内容并再次更新服务器。
echo "test2" >> testfile1.txt
git add testfile1.txt
git commit -m "Test2"
git push origin master
然后我们回到第一个客户端并执行 git pull 来查看更新的文件。 现在我可以在两台计算机之间来回移动, 如果我愿意的话,可以添加第三个。
Let's say that I have a desktop pc and a laptop,
and sometimes I work on the desktop and sometimes I work on the laptop.
What is the easiest way to move a git repository back and forth?
I want the git repositories to be identical,
so that I can continue where I left of at the other computer.
I would like to make sure that I have the same branches and tags on both of the computers.
Thanks
Johan
Note: I know how to do this with SubVersion, but I'm curious on how this would work with git. If it is easier, I can use a third pc as classical server that the two pc:s can sync against.
Note: Both computers are running Linux.
Update:
So let's try XANI:s idea with a bare git repo on a server,
and the push command syntax from KingCrunch.
In this example there is two clients and one server.
So let's create the server part first.
ssh user@server
mkdir -p ~/git_test/workspace
cd ~/git_test/workspace
git --bare init
So then from one of the other computers I try to get a copy of the repo with clone:
git clone user@server:~/git_test/workspace/
Initialized empty Git repository in /home/user/git_test/repo1/workspace/.git/
warning: You appear to have cloned an empty repository.
Then go into that repo and add a file:
cd workspace/
echo "test1" > testfile1.txt
git add testfile1.txt
git commit testfile1.txt -m "Added file testfile1.txt"
git push origin master
Now the server is updated with testfile1.txt.
Anyway, let's see if we can get this file from the other computer.
mkdir -p ~/git_test/repo2
cd ~/git_test/repo2
git clone user@server:~/git_test/workspace/
cd workspace/
git pull
And now we can see the testfile.
At this point we can edit it with some more content and update the server again.
echo "test2" >> testfile1.txt
git add testfile1.txt
git commit -m "Test2"
git push origin master
Then we go back to the first client and do a git pull to see the updated file.
And now I can move back and forth between the two computers,
and add a third if I like to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为,有多种方法。我将描述我如何处理这个问题
我有一台上网本作为 24/7 服务器,其中包含多个 git 存储库。从/到那里,我通过 SSH 推送和拉取更改。对于从外部的访问,我使用 dyndns.org。它工作得很好,特别是因为我有两个以上的系统,需要访问某些存储库。
更新:一个小例子。
假设我的上网本称为“上网本”。我在那里创建一个存储库,
然后在我的桌面上创建它的克隆。也许我也会添加一些文件
在我的便携式设备上,我将(首先)执行相同的操作,但对于远程访问(从 LAN 外部),我还将添加外部地址。
这就是 git(/git 工作流程)的工作方式。您可以根据需要添加任意数量的远程存储库。如果两个或多个引用相同的“物理”存储库,这并不重要。您不需要自己的本地“服务器”,您可以使用任何具有 ssh 访问权限的公共服务器。当然,如果您不需要从外部访问,那么您根本不需要公共服务器。裸存储库也可以位于桌面系统上,然后您可以在本地文件系统中创建工作副本存储库。
这就是我处理这个问题的方式,对我来说它工作得很好(如果不是完美的话;))
值得阅读的东西:http://progit。组织/
真是一本好书。-
I think, there are multiple approaches. I will just describe, how I handle this
I have one netbook as a 24/7 server, that holds multiple git-repositories. From/To there I push and pull changes via SSH. For access from outside I use dyndns.org. It works fine, especially because I have more than two systems, that needs access to some of the repositories.
Update: A little example.
Lets say my netbook is called "netbook". I create a repository there
On my desktop I will than create a clone of it. Maybe I will add some files also
On my portables I will (first) do the same, but for remote access (from outside my LAN), I will also add the external address.
Its just the way git (/git workflows) works. You can add as many remote repositories as you like. It doesnt matters, if two or more refers to the same "physical" repositories. You dont need an own local "server", you can use any public server, to which you have ssh access. And of course you dont need a public server at all, if you dont need access from outside. The bare repository can also be on the desktop system and you can then create a working-copy-repository within the local filesystem.
This is the way, how I handle this, and I for me it works quite fine (if not perfect ;))
Something to read: http://progit.org/
Really good book.-
最简单的方法:使用
--bare
创建中央存储库(因此没有签出文件,只有 .git 内容),或者 github“分布式”将如下所示:
设置:
git remote添加桌面 ssh://user@desktop/home/user/repo/path
git 远程添加笔记本电脑 ssh://user@laptop/home/user/repo/path
同步:
git pulllaptop/desktop
(推送在非裸存储库上不能很好地工作,因为推送到远程存储库时 git 不会修改签出的文件)或者,在 pendrive 上创建存储库;)
Easiest way: central repo created with
--bare
(so no checked out files, only .git stuff), or github"Distributed" will look like that:
Setup:
git remote add desktop ssh://user@desktop/home/user/repo/path
git remote add laptop ssh://user@laptop/home/user/repo/path
Syncing:
git pull laptop/desktop
(push won't work very well on non-bare repos because git won't modify checked out files when pushing to remote repo)Or, make repo on pendrive ;)
我会将存储库从一个盒子克隆到另一个盒子,然后设置两个存储库,以便我可以从另一个盒子中
git fetch
。将远程从
origin
重命名为另一个盒子的名称可以使远程分支更易于阅读。请注意,仅使用
git fetch
(而不是git push
),这适用于非裸存储库:I would clone the repo from one box to the other, and then set up the two repos so that I can just
git fetch
from the other box.Renaming the remote from
origin
to the name of the other box makes the remote branches easier to read.Note that by just using
git fetch
(and notgit push
), this works well with non-bare repositories:简单地使用 rsync 怎么样?
How about simply using
rsync
?难道你不能在 GitHub、BitBucket 或 GitLab 上创建一个远程存储库吗? (后两家公司提供无限的免费私人存储库)。当您完成一天的工作时,只需使用 git push 将更改推送到远程存储库。当您回到家时,只需执行 git pull 即可将工作中的更改拉取到您的家用计算机上。同样,当你在家完成工作时,执行 git push ,然后当你返回工作时,执行 git pull 。
Couldn't you just create a remote repository on GitHub, BitBucket or GitLab? (The latter two companies offer unlimited free private repositories). When you finish the day at work, simply use
git push
to push your changes to the remote repo. When you get home, just dogit pull
to pull your changes from work onto your home machine. Likewise, when you finish at home, dogit push
and then when you return to work, dogit pull
.场景 1:我专门在本地 PC1 上工作(编辑代码和文件),但希望在远程 PC2 上也有文件的副本(例如:构建整个代码库)。
在 << 中从 PC1 同步到 PC2使用 WiFi 热点时需要 1 分钟 < 25 MB 数据:
我在旅行时携带的一台性能较弱的计算机(笔记本电脑)上工作,但在其他地方的一台功能更强大的计算机上进行构建。我一直使用 git 使用脚本从我的笔记本电脑同步到另一台计算机。我只需输入以下命令即可运行它:
就是这样!即使使用手机 WiFi 热点并处理数十 GB大小的存储库,通常也需要大约 25 MB 的数据和约 30 秒到 1 分钟的时间。我已通过 ssh 连接到 PC2,因此我在 PC2 上执行 git log -1 来验证同步是否有效,然后运行构建命令。工作完美。试一试。有关详细信息,请参阅下面的链接。
注意:PC2 上克隆的存储库将位于名为
somename_SYNC
的 git 分支上。如果您希望脚本具有相同的分支名称而不是始终使用“SYNC 分支”,请适当修改脚本。如果需要,可以修改脚本以获得更像下面场景 2 的效果。不过,手动执行场景 2 并不困难,因此您可能只想继续手动执行场景 2。在场景 1 中,自动化脚本最有益且最节省时间,因为它允许简单快速的“修改、同步、构建”工作流程,其中“修改”发生在 PC1 上,“同步”从 PC1 运行,但也会影响 PC2,并且“构建”发生在 PC2 上。链接:
通过 SSH 使用 Eclipse 处理远程项目< /a>
https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/ use_scripts/sync_git_repo_from_pc1_to_pc2.sh
场景 2:我在多台计算机上工作(编辑代码和文件),并且希望能够从世界上的任何计算机编辑相同的代码存储库:
前往 https://github.com 并创建一个帐户,并可选择(推荐)设置 ssh 密钥。
现在使用他们的 Web 界面创建一个新的存储库。
查找新的存储库 ssh 或 https 克隆 URL。例如: [email protected]:ElectricRCAircraftGuy/eRCaGuy_dotfiles.git 或 https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git。
将项目克隆到 PC1 上。例如:
并在 PC2 上重复完全相同的克隆命令。
现在在 PC1 上进行一些更改,提交它们,然后将它们推送到 github 上的“远程”存储库:
现在在 PC2 上,提取您的更改:
现在您可以在 PC2 上编辑文件,提交它们,然后使用上面 2 个步骤所示的命令将它们推送到 github,然后从 PC1 上您可以运行
git pull< /code> 从 PC2 引入这些更改。
根据需要继续执行此过程,在 PC1 或 PC2 上工作,并轻松共享文件并在两台计算机之间分配工作。请记住,您的所有更改必须在一台 PC 上提交并推送到 github,然后才能签出(拉取它们)并继续在另一台 PC 上工作。
如果你遇到两台电脑之间的文件有点不同步的情况,你可能不得不使用一些额外的分支,进行一些合并,解决冲突等。然后,它变得更加类似于与一个小团队合作,大家都在同一个存储库上工作。谷歌是你的朋友。 Git 非常非常非常强大,并且有一个命令、一组命令或几乎所有内容的工作流程。
Scenario 1: I work (edit code and files) exclusively on local PC1 but want to have a duplicate copy of the files (ex: to build the whole code base) also on remote PC2.
Sync from PC1 to PC2 in < 1 minute over a wifi hotspot while using < 25 MB of data:
I work on one weak computer I travel with (a laptop), but build on a more powerful computer located elsewhere. I use git all the time to sync from my laptop to the other computer using a script. I just type this command to run it:
That's it! It usually takes about 25 MB of data and ~30 sec to 1 min, even when using a cell phone wifi hotspot and working on a repo that is dozens of gigabytes in size. I'm ssh'ed into PC2, so I do
git log -1
on PC2 to verify that the sync worked, then I run the build command. Works perfectly. Give it a shot. See the links below for details.Note: the cloned repo on PC2 will be on a git branch named
somename_SYNC
. Modify the script appropriately if you'd like it to have the same branch name instead of always using a "SYNC branch". It is possible to modify the script to get an effect more like Scenario 2 below if desired. Nevertheless, doing Scenario 2 manually isn't hard, so you might just want to continue to do Scenario 2 manually. It's Scenario 1 where the automated script is the most beneficial and time-saving, as it allows an easy and rapid "modify, sync, build" workflow where "modify" takes place on PC1, "sync" is run from PC1 but affects also PC2, and "build" takes place on PC2.Links:
Work on a remote project with Eclipse via SSH
https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/sync_git_repo_from_pc1_to_pc2.sh
Scenario 2: I work (edit code and files) on multiple computers, and want to be able to edit the same code repository from any computer in the world:
Go to https://github.com and create an account and optionally (recommended) set up ssh keys.
Now use their web interface to create a new repository.
Find the new repository ssh or https clone URL. Ex: [email protected]:ElectricRCAircraftGuy/eRCaGuy_dotfiles.git or https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git.
Clone the project onto PC1. Ex:
And repeat that exact same clone command on PC2.
Now on PC1, make some changes, commit them, and push them to your "remote" repository on github:
Now on PC2, pull in your changes:
Now you can edit files on PC2, commit them, and push them to github using the commands shown just 2 steps above, and then from PC1 you can run
git pull
to pull in those changes from PC2.Keep doing this process, as required, working on PC1 OR PC2, and easily sharing the files and splitting your work between the two computers. Just remember that all your changes must be committed and pushed to github on one PC before you can check them out (pull them) and continue working on the other PC.
If you ever get into a situation where files are a bit out-of-sync between the two PCs, you'll have to maybe use some extra branches, do some merges, resolve conflicts, etc. Then, it becomes more similar to working with a small team where you are all working on the same repo. Google is your friend. Git is very very very powerful, and has a command, set of commands, or workflow for just about everything.
好吧,您可以(通过 Git)推送和拉取您可能设置的服务器。或者,您可以将存储库存储在 GitHub 上并将其用作同步桥。
Well, you can push and pull (via Git) to the server you could potentially set up. Or you could store your repos at GitHub and use that as a syncing bridge.
您可以在任何计算机(可能是台式机)上创建存储库,然后从笔记本电脑及其本身推/拉到它。
You could make the repository on any of your computers, probably the desktop one and push/pull to it from both laptop and itself.