2016-01-07 22 views
0

這看起來很愚蠢,但我無法弄清楚。用於克隆或添加遠程服務器的指令,或推送到克隆的存儲庫。但我試圖找出如何在服務器上的共享目錄中設置git存儲庫,並讓其他人在其個人目錄中的克隆進行協作。在單臺服務器上使用git,即沒有真正的遠程原點

這是我已經試過:

[email protected]:~$ mkdir mainrepo 
[email protected]:~$ cd mainrepo/ 
[email protected]:~/mainrepo$ echo "inital entry" > text.txt 
[email protected]:~/mainrepo$ git init 
Initialized empty Git repository in /home/abalter/mainrepo/.git/ 
[email protected]:~/mainrepo$ git add text.txt 
[email protected]:~/mainrepo$ git commit -m "initial commit" 
[master (root-commit) fb829d6] initial commit 
1 file changed, 1 insertion(+) 
create mode 100644 text.txt 
[email protected]:~/mainrepo$ cd .. 
[email protected]:~$ git clone mainrepo/ userrepo 
Cloning into 'userrepo'... 
done. 
[email protected]:~$ cd userrepo/ 
[email protected]:~/userrepo$ echo "users addition" >> text.txt 
[email protected]:~/userrepo$ git commit -am "users addition" 
[master 3d4ea94] users addition 
1 file changed, 1 insertion(+) 
[email protected]:~/userrepo$ git add remote origin ../mainrepo/ 
fatal: ../mainrepo/: '../mainrepo/' is outside repository 
[email protected]:~/userrepo$ git push origin master 
Counting objects: 5, done. 
Writing objects: 100% (3/3), 267 bytes | 0 bytes/s, done. 
Total 3 (delta 0), reused 0 (delta 0) 
remote: error: refusing to update checked out branch: refs/heads/master 
remote: error: By default, updating the current branch in a non-bare repository 
remote: error: is denied, because it will make the index and work tree inconsistent 
remote: error: with what you pushed, and will require 'git reset --hard' to match 
remote: error: the work tree to HEAD. 
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to 
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into 
remote: error: its current branch; however, this is not recommended unless you 
remote: error: arranged to update its work tree to match what you pushed in some 
remote: error: other way. 
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set 
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'. 
To /home/abalter/mainrepo/ 
! [remote rejected] master -> master (branch is currently checked out) 
error: failed to push some refs to '/home/abalter/mainrepo/' 
[email protected]:~/userrepo$ 

顯然我錯過了一步,但我不知道它是什麼。

編輯 我意識到它應該是git remote add origin ../mainrepo。但是

[email protected]:~/userrepo$ git remote add origin ../mainrepo 
fatal: remote origin already exists. 
[email protected]:~/userrepo$ 

如果它已經存在,爲什麼我不能推它呢?

回答

2

這裏的答案是否定的比實際的遠程計算機的設置不同:一個版本庫,你推到應該是--bare克隆。例如,把事情整個事情了通過使用戶系統信息庫(這樣你可以做一些初步提交)開始:

$ mkdir set-it-up && cd set-it-up 
$ git init 
[create and add files as usual and then] 
$ git commit 

然後移動到你想要的裸主回購的地方,如:

$ mkdir master-repo.git; cd master-repo.git; git init --bare 

(您可能要添加--shared--shared=all等;見the git init documentation)。現在你擁有一個沒有工作目錄的倉庫,適用於克隆和推送。

填寫了,你現在可以獲取(手動)一切從你的「設置它了」回購:

git fetch /path/to/set-it-up 'refs/*:refs/*' 

在這一點上它是安全的刪除您的「設置它了」複製:克隆master-repo.git目錄將爲您提供一個新的克隆,其中克隆的origin設置爲master-repo.git目錄的路徑。

或者,在你的設置它了回購,你現在可以添加主repo.git目錄爲「原產地」的網址,然後按。


由於git的2.5,它可能使明智的事情(爲明智一些值)與非共享資源庫發生,但是這是一個有點複雜來形容。

您可以做出初步的裸庫完全是空的,如果你喜歡。克隆一個空的倉庫時,git抱怨一下,但是你可以在克隆中工作並推回來填充裸倉庫。

+0

謝謝!我不知道如何創建'--bare'庫。 – abalter

0

我發現this,其中說:「共享庫應該是裸存儲庫。」所以我以錯誤的方式去解決這個問題。這工作:

[email protected]:~$ git init --bare baremain.git 
Initialized empty Git repository in /home/abalter/baremain.git/ 
[email protected]:~$ git clone /home/abalter/baremain.git/ alicecopy 
Cloning into 'alicecopy'... 
warning: You appear to have cloned an empty repository. 
done. 
[email protected]:~$ cd alicecopy/ 
[email protected]:~/alicecopy$ echo "alice created this" > alice.txt 
[email protected]:~/alicecopy$ git add alice.txt 
[email protected]:~/alicecopy$ git commit -m "alice created file" 
[master (root-commit) 20b15da] alice created file 
1 file changed, 1 insertion(+) 
create mode 100644 alice.txt 
[email protected]:~/alicecopy$ git push origin master 
Counting objects: 3, done. 
Writing objects: 100% (3/3), 229 bytes | 0 bytes/s, done. 
Total 3 (delta 0), reused 0 (delta 0) 
To /home/abalter/baremain.git/ 
* [new branch]  master -> master 
[email protected]:~/alicecopy$ cd .. 
[email protected]:~$ git clone /home/abalter/baremain.git/ bobcopy 
Cloning into 'bobcopy'... 
done. 
[email protected]:~$ cd bobcopy/ 
[email protected]:~/bobcopy$ echo "bob added this" >> alice.txt 
[email protected]:~/bobcopy$ git add alice.txt 
[email protected]:~/bobcopy$ git commit -m "bob added stuff" 
[master b7b2e2a] bob added stuff 
1 file changed, 1 insertion(+) 
[email protected]:~/bobcopy$ git push origin master 
Counting objects: 5, done. 
Writing objects: 100% (3/3), 266 bytes | 0 bytes/s, done. 
Total 3 (delta 0), reused 0 (delta 0) 
To /home/abalter/baremain.git/ 
    20b15da..b7b2e2a master -> master 
[email protected]:~/bobcopy$ cd ../alicecopy/ 
[email protected]:~/alicecopy$ git pull 
remote: Counting objects: 5, done. 
remote: Total 3 (delta 0), reused 0 (delta 0) 
Unpacking objects: 100% (3/3), done. 
From /home/abalter/baremain 
    20b15da..b7b2e2a master  -> origin/master 
Updating 20b15da..b7b2e2a 
Fast-forward 
alice.txt | 1 + 
1 file changed, 1 insertion(+) 
[email protected]:~/alicecopy$ cat alice.txt 
alice created this 
bob added this 
[email protected]:~/alicecopy$ 

編輯:因爲如果你已經有一個目錄滿你想用的東西方法2。

[email protected]:~$ mkdir bobsstuff 
[email protected]:~$ cd bobsstuff/ 
[email protected]:~/bobsstuff$ echo "this is bob's existing file" > bob.txt 
[email protected]:~/bobsstuff$ cd .. 
[email protected]:~$ git init --bare mainrepo.git 
Initialized empty Git repository in /home/abalter/mainrepo.git/ 
[email protected]:~$ cd bobsstuff/ 
[email protected]:~/bobsstuff$ git remote add origin../mainrepo.git/ 
fatal: Not a git repository (or any of the parent directories): .git 
[email protected]:~/bobsstuff$ echo "oops--not yet a repo" 
oops--not yet a repo 
[email protected]:~/bobsstuff$ git init 
Initialized empty Git repository in /home/abalter/bobsstuff/.git/ 
[email protected]:~/bobsstuff$ git remote add origin ../mainrepo.git/ 
[email protected]:~/bobsstuff$ git add bob.txt 
[email protected]:~/bobsstuff$ git add . 
[email protected]:~/bobsstuff$ git commit -m "initial commit of all my files" 
[master (root-commit) 02a3d2e] initial commit of all my files 
1 file changed, 1 insertion(+) 
create mode 100644 bob.txt 
[email protected]:~/bobsstuff$ git push origin master 
Counting objects: 3, done. 
Writing objects: 100% (3/3), 244 bytes | 0 bytes/s, done. 
Total 3 (delta 0), reused 0 (delta 0) 
To ../mainrepo.git/ 
* [new branch]  master -> master 
[email protected]:~/bobsstuff$ cd .. 
[email protected]:~$ git clone mainrepo.git/ alicesstuff 
Cloning into 'alicesstuff'... 
done. 
[email protected]:~$ cd alicesstuff/ 
[email protected]:~/alicesstuff$ cat bob.txt 
this is bob's existing file 
[email protected]:~/alicesstuff$ echo "alice added this" >> bob.txt 
[email protected]:~/alicesstuff$ cat bob.txt 
this is bob's existing file 
alice added this 
[email protected]:~/alicesstuff$ echo "alice created this" > alice.txt 
[email protected]:~/alicesstuff$ git add . 
[email protected]:~/alicesstuff$ git status 
On branch master 
Your branch is up-to-date with 'origin/master'. 

Changes to be committed: 
    (use "git reset HEAD <file>..." to unstage) 

    new file: alice.txt 
    modified: bob.txt 

[email protected]:~/alicesstuff$ git commit -m "alice's initial commit" 
[master 2b9d332] alice's initial commit 
2 files changed, 2 insertions(+) 
create mode 100644 alice.txt 
[email protected]:~/alicesstuff$ git push origin master 
Counting objects: 6, done. 
Compressing objects: 100% (2/2), done. 
Writing objects: 100% (4/4), 341 bytes | 0 bytes/s, done. 
Total 4 (delta 0), reused 0 (delta 0) 
To /home/abalter/mainrepo.git/ 
    02a3d2e..2b9d332 master -> master 
[email protected]:~/alicesstuff$ cd ../bobsstuff/ 
[email protected]:~/bobsstuff$ git pull origin master 
remote: Counting objects: 6, done. 
remote: Compressing objects: 100% (2/2), done. 
remote: Total 4 (delta 0), reused 0 (delta 0) 
Unpacking objects: 100% (4/4), done. 
From ../mainrepo 
* branch   master  -> FETCH_HEAD 
    02a3d2e..2b9d332 master  -> origin/master 
Updating 02a3d2e..2b9d332 
Fast-forward 
alice.txt | 1 + 
bob.txt | 1 + 
2 files changed, 2 insertions(+) 
create mode 100644 alice.txt 
[email protected]:~/bobsstuff$ cat bob.txt 
this is bob's existing file 
alice added this 
[email protected]:~/bobsstuff$ cat alice.txt 
alice created this 
相關問題