git 移动时,计算机之间的git存储库同步?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4948190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git repository sync between computers, when moving around?
提问by Johan
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?
来回移动 git 存储库的最简单方法是什么?
I want the git repositories to be identical, so that I can continue where I left of at the other computer.
我希望 git 存储库是相同的,这样我就可以继续我在另一台计算机上离开的地方。
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.
注意:我知道如何用 SubVersion 做到这一点,但我很好奇这将如何与 git 一起工作。如果更容易,我可以使用第三台 PC 作为经典服务器,两台 pc:s 可以同步。
Note: Both computers are running Linux.
注意:两台计算机都运行 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.
因此,让我们在服务器上使用裸 git 存储库以及来自 KingCrunch 的 push 命令语法来尝试 XANI:s 的想法。在此示例中,有两个客户端和一个服务器。
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:
因此,然后从其他计算机中的一台我尝试使用克隆获取 repo 的副本:
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.
现在服务器已更新为 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.
然后我们回到第一个客户端并执行 git pull 以查看更新的文件。现在我可以在两台计算机之间来回移动,如果我愿意,还可以添加第三台。
采纳答案by KingCrunch
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.
我有一台上网本作为 24/7 服务器,它包含多个 git 存储库。从/到那里,我通过 SSH 推送和拉取更改。对于从外部访问,我使用 dyndns.org。它工作正常,特别是因为我有两个以上的系统,需要访问一些存储库。
Update: A little example. Lets say my netbook is called "netbook". I create a repository there
更新:一个小例子。假设我的上网本被称为“上网本”。我在那里创建了一个存储库
$ ssh [email protected]
$ cd ~/git
$ mkdir newThing
$ cd newThing
$ git init --bare
On my desktop I will than create a clone of it. Maybe I will add some files also
在我的桌面上,我将创建它的克隆。也许我也会添加一些文件
$ git clone [email protected]:/home/username/git/newThing
$ git add .
$ git commit -m "Initial"
$ git push origin master
On my portables I will (first) do the same, but for remote access (from outside my LAN), I will also add the external address.
在我的便携式设备上,我将(首先)这样做,但对于远程访问(从我的 LAN 外部),我还将添加外部地址。
$ git clone [email protected]:/home/username/git/newThing
$ git remote add externalName [email protected]:/home/username/git/newThing
$ git pull externalName master
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.
它只是 git (/git 工作流) 的工作方式。您可以根据需要添加任意数量的远程存储库。如果两个或更多引用相同的“物理”存储库,这并不重要。您不需要自己的本地“服务器”,您可以使用任何具有 ssh 访问权限的公共服务器。当然,如果您不需要从外部访问,您根本不需要公共服务器。裸存储库也可以在桌面系统上,然后您可以在本地文件系统中创建一个工作副本存储库。
$ mkdir myRepo; cd myRepo
$ git init --bare
$ cd /path/to/myProject
$ git remote add origin /path/to/myRepo
$ git add .; git commit -m "Initial"; git push origin master
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.-
值得阅读的东西:http: //progit.org/ 非常好的书。-
回答by ndim
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.
我会将 repo 从一个盒子克隆到另一个盒子,然后设置两个 repos,以便我可以git fetch
从另一个盒子。
Renaming the remote from origin
to the name of the other box makes the remote branches easier to read.
将远程重命名为origin
另一个框的名称使远程分支更易于阅读。
Note that by just using git fetch
(and not git push
), this works well with non-bare repositories:
请注意,仅使用git fetch
(而不是git push
),这适用于非裸存储库:
[user@foo repo]$ git fetch -v bar
[user@bar repo]$ git fetch -v foo
回答by XANi
Easiest way: central repo created with --bare
(so no checked out files, only .git stuff), or github
最简单的方法:使用--bare
(因此没有签出文件,只有 .git 内容)或 github创建的中央存储库
"Distributed" will look like that:
“分布式”看起来像这样:
Setup:
设置:
- On laptop:
git remote add desktop ssh://user@desktop/home/user/repo/path
- On desktop:
git remote add laptop ssh://user@laptop/home/user/repo/path
- 在笔记本电脑上:
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)
git pull laptop/desktop
(推送在非裸仓库上不会很好地工作,因为 git 在推送到远程仓库时不会修改签出的文件)
Or, make repo on pendrive ;)
或者,在 pendrive 上制作 repo ;)
回答by College Student
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 do git pull
to pull your changes from work onto your home machine. Likewise, when you finish at home, do git push
and then when you return to work, do git pull
.
你不能在 GitHub、BitBucket 或 GitLab 上创建一个远程存储库吗?(后两家公司提供无限制的免费私人存储库)。当您完成一天的工作后,只需使用git push
将您的更改推送到远程存储库即可。当您回到家时,只需git pull
将您的更改从工作中提取到您的家用机器上即可。同样,当你在家完成时,做git push
,然后当你回到工作时,做git pull
。
回答by seriyPS
How about simply using rsync
?
简单地使用rsync
怎么样?
回答by Gabriel Staples
Sync from PC1 to PC2 in < 1 minute over a wifi hotspot while using < 25 MB of data:
在使用 < 25 MB 数据的同时,通过 wifi 热点在 < 1 分钟内从 PC1 同步到 PC2:
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:
我在旅行时随身携带的一台性能较弱的计算机(笔记本电脑)上工作,但在位于其他地方的更强大的计算机上工作。我一直使用 git 使用脚本从我的笔记本电脑同步到另一台计算机。我只需键入此命令即可运行它:
sync_git_repo_from_pc1_to_pc2
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 gigabytesin size. I'm ssh'ed into PC2, so I do git log
on PC2 to verify the sync worked, then I run the build command. Works perfectly. Give it a shot.
就是这样!它通常需要大约 25 MB 的数据和大约 30 秒到 1 分钟的时间,即使在使用手机 wifi 热点并处理数十 GB大小的存储库时也是如此。我通过 ssh 连接到 PC2,所以我git log
在 PC2 上验证同步工作,然后我运行构建命令。完美运行。试一试。
Links:
链接:
- Full setup and installation instructions here:
Work on a remote project with Eclipse via SSH - Readme, documentation, & repo here:https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles#sync_git_repo_from_pc1_to_pc2sh-description--details.
- Exact script in question is here:
https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/sync_git_repo_from_pc1_to_pc2.sh
回答by Kerhong
You could make the repository on any of your computers, probably the desktop one and push/pull to it from both laptop and itself.
您可以在您的任何计算机上创建存储库,可能是台式机,然后从笔记本电脑和它本身推/拉到它。
回答by mipadi
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.
好吧,您可以推送和拉取(通过 Git)到您可能设置的服务器。或者您可以将您的存储库存储在 GitHub 上并将其用作同步桥梁。