git 存储库在计算机之间同步,移动时? [英] git repository sync between computers, when moving around?

查看:14
本文介绍了git 存储库在计算机之间同步,移动时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一台台式电脑和一台笔记本电脑,有时我在台式机上工作,有时我在笔记本电脑上工作.

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.

来回移动 git 存储库的最简单方法是什么?

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.

我想确保两台计算机上的分支和标签相同.

I would like to make sure that I have the same branches and tags on both of the computers.

谢谢约翰

注意:我知道如何用 SubVersion 做到这一点,但我很好奇这将如何与 git 一起工作.如果方便的话,我可以使用第三台 PC 作为经典服务器,两台 PC:s 可以同步.

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.

注意:两台电脑都运行 Linux.

Note: Both computers are running Linux.

更新:

所以让我们在服务器上使用裸 git 存储库尝试 XANI:s 的想法,以及来自 KingCrunch 的 push 命令语法.在这个例子中有两个客户端和一个服务器.

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

现在服务器已更新为 testfile1.txt.

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

然后我们回到第一个客户端并执行 git pull 以查看更新的文件.现在我可以在两台电脑之间来回移动,如果我愿意,还可以添加第三个.

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.

推荐答案

我认为,有多种方法.我只是描述一下,我是如何处理这个问题的

I think, there are multiple approaches. I will just describe, how I handle this

我有一台上网本作为 24/7 全天候服务器,其中包含多个 git 存储库.从/到那里,我通过 SSH 推送和拉取更改.对于从外部访问,我使用 dyndns.org.它工作正常,特别是因为我有两个以上的系统,需要访问一些存储库.

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

$ ssh username@netbook.local
$ 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 username@netbook.local:/home/username/git/newThing
$ git add .
$ git commit -m "Initial"
$ git push origin master

在我的便携式设备上,我(首先)会这样做,但对于远程访问(从我的 LAN 外部),我还将添加外部地址.

On my portables I will (first) do the same, but for remote access (from outside my LAN), I will also add the external address.

$ git clone username@netbook.local:/home/username/git/newThing
$ git remote add externalName username@mydyndns.home-ip.org:/home/username/git/newThing
$ git pull externalName master

这正是 git (/git 工作流) 的工作方式.您可以根据需要添加任意数量的远程存储库.如果两个或更多引用相同的物理"存储库,这并不重要.您不需要自己的本地服务器",您可以使用任何具有 ssh 访问权限的公共服务器.当然,如果您不需要从外部访问,您根本不需要公共服务器.裸存储库也可以在桌面系统上,然后您可以在本地文件系统中创建一个工作副本存储库.

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.

$ 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 ;))

阅读内容:http://progit.org/真是好书.-

Something to read: http://progit.org/ Really good book.-

这篇关于git 存储库在计算机之间同步,移动时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆