使用Git,两个常规(非裸露)存储库无法直接来回推/拉文件。必须有一个中间的裸仓库。显然,这就像一对有孩子的已婚夫妇,并且这对夫妇离婚了。父母不会互相交谈,但他们会通过孩子进行交流。
因此,您有一个存储库,将此存储库克隆到一个裸存储库,然后将其克隆到第三个存储库。第一个和第三个可以通过第二个存储库(裸仓库)交换信息。我想这是有道理的,因为您不希望有人未经您的同意就可以将内容检入存储库,因为这可能会导致合并冲突等。
所以,这是一个例子:
在PC上,在〜/ workspace中
git init
echo "line 1" > afile.txt
git add .
git commit -m ‘initial import’
git clone --bare . ../remote-repository.git
git remote add origin ../remote-repository.git
git push --set-upstream origin master
在笔记本电脑上,在〜/ workspace中(请勿执行git init等)
git clone //LJZ-DELLPC/remote-repository.git/ .
//然后进行各种提交,并推送它们:
echo "line 2" > afile.txt
git add afile.txt
git commit -m 'added line 2'
git push
然后回到PC,在〜/ workspace中
git pull
//然后进行各种提交,并推送它们:
git push
在笔记本电脑上git pull
等等。
这是一个完全具体的示例,所有示例都在一台机器上,直接从命令窗口复制,这样我们就知道没有遗漏任何步骤,它确实起作用了,等等:
lylez@LJZ-DELLPC ~
$ cd gitdir
/home/lylez/gitdir
lylez@LJZ-DELLPC ~/gitdir
$ ls
lylez@LJZ-DELLPC ~/gitdir
$ mkdir repo1
lylez@LJZ-DELLPC ~/gitdir
$ cd repo1
/home/lylez/gitdir/repo1
lylez@LJZ-DELLPC ~/gitdir/repo1
$ git init
Initialized empty Git repository in /home/lylez/gitdir/repo1/.git/
lylez@LJZ-DELLPC ~/gitdir/repo1
$ echo "line 1" > afile.txt
lylez@LJZ-DELLPC ~/gitdir/repo1
$ git add afile.txt
lylez@LJZ-DELLPC ~/gitdir/repo1
$ git commit -m 'initial import'
[master (root-commit) f407e12] initial import
1 file changed, 1 insertion(+)
create mode 100644 afile.txt
lylez@LJZ-DELLPC ~/gitdir/repo1
$ git clone --bar . ../repo1-bare-clone
Cloning into bare repository '../repo1-bare-clone'...
done.
lylez@LJZ-DELLPC ~/gitdir/repo1
$ git remote add origin ../repo1-bare-clone
lylez@LJZ-DELLPC ~/gitdir/repo1
$ git push --set-upstream origin master
Branch master set up to track remote branch master from origin.
Everything up-to-date
lylez@LJZ-DELLPC ~/gitdir/repo1
$ cd ..
lylez@LJZ-DELLPC ~/gitdir
$ ls
repo1 repo1-bare-clone
lylez@LJZ-DELLPC ~/gitdir
$ mkdir repo1-remote
lylez@LJZ-DELLPC ~/gitdir
$ cd repo1-remote
/home/lylez/gitdir/repo1-remote
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git clone ../repo1-bare-clone .
Cloning into '.'...
done.
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ ls
afile.txt
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ cat afile.txt
line 1
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ echo "line 2" >> afile.txt
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git add afile.txt
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git commit -m 'added line 2'
[master 5ad31e0] added line 2
1 file changed, 1 insertion(+)
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 260 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /home/lylez/gitdir/repo1-remote/../repo1-bare-clone
f407e12..5ad31e0 master -> master
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ cd ../repo1
lylez@LJZ-DELLPC ~/gitdir/repo1
$ ls
afile.txt
lylez@LJZ-DELLPC ~/gitdir/repo1
$ cat afile.txt
line 1
lylez@LJZ-DELLPC ~/gitdir/repo1
$ git pull
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ../repo1-bare-clone
f407e12..5ad31e0 master -> origin/master
Updating f407e12..5ad31e0
Fast-forward
afile.txt | 1 +
1 file changed, 1 insertion(+)
lylez@LJZ-DELLPC ~/gitdir/repo1
$ cat afile.txt
line 1
line 2
lylez@LJZ-DELLPC ~/gitdir/repo1
$ echo "line 3" >> afile.txt
lylez@LJZ-DELLPC ~/gitdir/repo1
$ git add afile.txt
lylez@LJZ-DELLPC ~/gitdir/repo1
$ git commit -m 'added line 3'
[master 3fa569e] added line 3
1 file changed, 1 insertion(+)
lylez@LJZ-DELLPC ~/gitdir/repo1
$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 265 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ../repo1-bare-clone
5ad31e0..3fa569e master -> master
lylez@LJZ-DELLPC ~/gitdir/repo1
$ cd ../repo1-remote/
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ ls
afile.txt
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ cat afile.txt
line 1
line 2
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git pull
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/lylez/gitdir/repo1-remote/../repo1-bare-clone
5ad31e0..3fa569e master -> origin/master
Updating 5ad31e0..3fa569e
Fast-forward
afile.txt | 1 +
1 file changed, 1 insertion(+)
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ cat afile.txt
line 1
line 2
line 3
lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git --version
git version 2.1.1
lylez@LJZ-DELLPC ~/gitdir/repo1-remote