在单个服务器上使用 git,即没有真正的远程来源
Using git on a single server, i.e. no truly remote origin
这看起来很愚蠢,但我想不通。那里有大量关于克隆或添加远程服务器,或推送到克隆存储库的说明。但我正在尝试弄清楚如何在服务器上的共享目录中设置 git 存储库,并让其他人从他们个人目录中的克隆进行协作。
这是我试过的:
abalter@U1:~$ mkdir mainrepo
abalter@U1:~$ cd mainrepo/
abalter@U1:~/mainrepo$ echo "inital entry" > text.txt
abalter@U1:~/mainrepo$ git init
Initialized empty Git repository in /home/abalter/mainrepo/.git/
abalter@U1:~/mainrepo$ git add text.txt
abalter@U1:~/mainrepo$ git commit -m "initial commit"
[master (root-commit) fb829d6] initial commit
1 file changed, 1 insertion(+)
create mode 100644 text.txt
abalter@U1:~/mainrepo$ cd ..
abalter@U1:~$ git clone mainrepo/ userrepo
Cloning into 'userrepo'...
done.
abalter@U1:~$ cd userrepo/
abalter@U1:~/userrepo$ echo "users addition" >> text.txt
abalter@U1:~/userrepo$ git commit -am "users addition"
[master 3d4ea94] users addition
1 file changed, 1 insertion(+)
abalter@U1:~/userrepo$ git add remote origin ../mainrepo/
fatal: ../mainrepo/: '../mainrepo/' is outside repository
abalter@U1:~/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/'
abalter@U1:~/userrepo$
我显然漏掉了一步,但我不知道那是什么。
编辑
我意识到它应该是 git remote add origin ../mainrepo
。但是
abalter@U1:~/userrepo$ git remote add origin ../mainrepo
fatal: remote origin already exists.
abalter@U1:~/userrepo$
如果它已经存在,为什么我不能推送它?
我找到了 this,上面写着 "Shared Repositories Should Be Bare Repositories." 所以我做错了。这有效:
abalter@U1:~$ git init --bare baremain.git
Initialized empty Git repository in /home/abalter/baremain.git/
abalter@U1:~$ git clone /home/abalter/baremain.git/ alicecopy
Cloning into 'alicecopy'...
warning: You appear to have cloned an empty repository.
done.
abalter@U1:~$ cd alicecopy/
abalter@U1:~/alicecopy$ echo "alice created this" > alice.txt
abalter@U1:~/alicecopy$ git add alice.txt
abalter@U1:~/alicecopy$ git commit -m "alice created file"
[master (root-commit) 20b15da] alice created file
1 file changed, 1 insertion(+)
create mode 100644 alice.txt
abalter@U1:~/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
abalter@U1:~/alicecopy$ cd ..
abalter@U1:~$ git clone /home/abalter/baremain.git/ bobcopy
Cloning into 'bobcopy'...
done.
abalter@U1:~$ cd bobcopy/
abalter@U1:~/bobcopy$ echo "bob added this" >> alice.txt
abalter@U1:~/bobcopy$ git add alice.txt
abalter@U1:~/bobcopy$ git commit -m "bob added stuff"
[master b7b2e2a] bob added stuff
1 file changed, 1 insertion(+)
abalter@U1:~/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
abalter@U1:~/bobcopy$ cd ../alicecopy/
abalter@U1:~/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(+)
abalter@U1:~/alicecopy$ cat alice.txt
alice created this
bob added this
abalter@U1:~/alicecopy$
编辑: 方法 2,如果你已经有了一个装满你想要使用的东西的目录。
abalter@U1:~$ mkdir bobsstuff
abalter@U1:~$ cd bobsstuff/
abalter@U1:~/bobsstuff$ echo "this is bob's existing file" > bob.txt
abalter@U1:~/bobsstuff$ cd ..
abalter@U1:~$ git init --bare mainrepo.git
Initialized empty Git repository in /home/abalter/mainrepo.git/
abalter@U1:~$ cd bobsstuff/
abalter@U1:~/bobsstuff$ git remote add origin../mainrepo.git/
fatal: Not a git repository (or any of the parent directories): .git
abalter@U1:~/bobsstuff$ echo "oops--not yet a repo"
oops--not yet a repo
abalter@U1:~/bobsstuff$ git init
Initialized empty Git repository in /home/abalter/bobsstuff/.git/
abalter@U1:~/bobsstuff$ git remote add origin ../mainrepo.git/
abalter@U1:~/bobsstuff$ git add bob.txt
abalter@U1:~/bobsstuff$ git add .
abalter@U1:~/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
abalter@U1:~/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
abalter@U1:~/bobsstuff$ cd ..
abalter@U1:~$ git clone mainrepo.git/ alicesstuff
Cloning into 'alicesstuff'...
done.
abalter@U1:~$ cd alicesstuff/
abalter@U1:~/alicesstuff$ cat bob.txt
this is bob's existing file
abalter@U1:~/alicesstuff$ echo "alice added this" >> bob.txt
abalter@U1:~/alicesstuff$ cat bob.txt
this is bob's existing file
alice added this
abalter@U1:~/alicesstuff$ echo "alice created this" > alice.txt
abalter@U1:~/alicesstuff$ git add .
abalter@U1:~/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
abalter@U1:~/alicesstuff$ git commit -m "alice's initial commit"
[master 2b9d332] alice's initial commit
2 files changed, 2 insertions(+)
create mode 100644 alice.txt
abalter@U1:~/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
abalter@U1:~/alicesstuff$ cd ../bobsstuff/
abalter@U1:~/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
abalter@U1:~/bobsstuff$ cat bob.txt
this is bob's existing file
alice added this
abalter@U1:~/bobsstuff$ cat alice.txt
alice created this
这里的答案与实际远程机器上的设置没有什么不同:您推送到的存储库应该1 是一个 --bare
克隆。例如,要设置所有内容,您首先要创建一个用户存储库(以便您可以进行一些初始提交):2
$ 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)。现在你有一个空的——没有工作目录——存储库,适用于 cloning-from 和 pushing-to。
要填写它,您现在可以(手动)从 "set-it-up" 存储库中获取所有内容:
git fetch /path/to/set-it-up 'refs/*:refs/*'
此时删除您的 "set-it-up" 副本是安全的:克隆 master-repo.git 目录将为您提供一个可以在其中工作的新克隆,克隆的 origin
设置为master-repo.git目录的路径。
或者,在您的 set-it-up 存储库中,您现在可以将 master-repo.git 目录添加为 "origin" URL,然后推送。
1从 git 2.5 开始,可以通过 non-shared 存储库实现合理的事情(对于某些合理的价值),但这是描述起来有点复杂。
2如果愿意,您可以将初始裸存储库完全清空。克隆一个空的存储库时,git 有点抱怨,但是您可以在克隆中工作并返回以填充裸存储库。
这看起来很愚蠢,但我想不通。那里有大量关于克隆或添加远程服务器,或推送到克隆存储库的说明。但我正在尝试弄清楚如何在服务器上的共享目录中设置 git 存储库,并让其他人从他们个人目录中的克隆进行协作。
这是我试过的:
abalter@U1:~$ mkdir mainrepo
abalter@U1:~$ cd mainrepo/
abalter@U1:~/mainrepo$ echo "inital entry" > text.txt
abalter@U1:~/mainrepo$ git init
Initialized empty Git repository in /home/abalter/mainrepo/.git/
abalter@U1:~/mainrepo$ git add text.txt
abalter@U1:~/mainrepo$ git commit -m "initial commit"
[master (root-commit) fb829d6] initial commit
1 file changed, 1 insertion(+)
create mode 100644 text.txt
abalter@U1:~/mainrepo$ cd ..
abalter@U1:~$ git clone mainrepo/ userrepo
Cloning into 'userrepo'...
done.
abalter@U1:~$ cd userrepo/
abalter@U1:~/userrepo$ echo "users addition" >> text.txt
abalter@U1:~/userrepo$ git commit -am "users addition"
[master 3d4ea94] users addition
1 file changed, 1 insertion(+)
abalter@U1:~/userrepo$ git add remote origin ../mainrepo/
fatal: ../mainrepo/: '../mainrepo/' is outside repository
abalter@U1:~/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/'
abalter@U1:~/userrepo$
我显然漏掉了一步,但我不知道那是什么。
编辑
我意识到它应该是 git remote add origin ../mainrepo
。但是
abalter@U1:~/userrepo$ git remote add origin ../mainrepo
fatal: remote origin already exists.
abalter@U1:~/userrepo$
如果它已经存在,为什么我不能推送它?
我找到了 this,上面写着 "Shared Repositories Should Be Bare Repositories." 所以我做错了。这有效:
abalter@U1:~$ git init --bare baremain.git
Initialized empty Git repository in /home/abalter/baremain.git/
abalter@U1:~$ git clone /home/abalter/baremain.git/ alicecopy
Cloning into 'alicecopy'...
warning: You appear to have cloned an empty repository.
done.
abalter@U1:~$ cd alicecopy/
abalter@U1:~/alicecopy$ echo "alice created this" > alice.txt
abalter@U1:~/alicecopy$ git add alice.txt
abalter@U1:~/alicecopy$ git commit -m "alice created file"
[master (root-commit) 20b15da] alice created file
1 file changed, 1 insertion(+)
create mode 100644 alice.txt
abalter@U1:~/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
abalter@U1:~/alicecopy$ cd ..
abalter@U1:~$ git clone /home/abalter/baremain.git/ bobcopy
Cloning into 'bobcopy'...
done.
abalter@U1:~$ cd bobcopy/
abalter@U1:~/bobcopy$ echo "bob added this" >> alice.txt
abalter@U1:~/bobcopy$ git add alice.txt
abalter@U1:~/bobcopy$ git commit -m "bob added stuff"
[master b7b2e2a] bob added stuff
1 file changed, 1 insertion(+)
abalter@U1:~/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
abalter@U1:~/bobcopy$ cd ../alicecopy/
abalter@U1:~/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(+)
abalter@U1:~/alicecopy$ cat alice.txt
alice created this
bob added this
abalter@U1:~/alicecopy$
编辑: 方法 2,如果你已经有了一个装满你想要使用的东西的目录。
abalter@U1:~$ mkdir bobsstuff
abalter@U1:~$ cd bobsstuff/
abalter@U1:~/bobsstuff$ echo "this is bob's existing file" > bob.txt
abalter@U1:~/bobsstuff$ cd ..
abalter@U1:~$ git init --bare mainrepo.git
Initialized empty Git repository in /home/abalter/mainrepo.git/
abalter@U1:~$ cd bobsstuff/
abalter@U1:~/bobsstuff$ git remote add origin../mainrepo.git/
fatal: Not a git repository (or any of the parent directories): .git
abalter@U1:~/bobsstuff$ echo "oops--not yet a repo"
oops--not yet a repo
abalter@U1:~/bobsstuff$ git init
Initialized empty Git repository in /home/abalter/bobsstuff/.git/
abalter@U1:~/bobsstuff$ git remote add origin ../mainrepo.git/
abalter@U1:~/bobsstuff$ git add bob.txt
abalter@U1:~/bobsstuff$ git add .
abalter@U1:~/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
abalter@U1:~/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
abalter@U1:~/bobsstuff$ cd ..
abalter@U1:~$ git clone mainrepo.git/ alicesstuff
Cloning into 'alicesstuff'...
done.
abalter@U1:~$ cd alicesstuff/
abalter@U1:~/alicesstuff$ cat bob.txt
this is bob's existing file
abalter@U1:~/alicesstuff$ echo "alice added this" >> bob.txt
abalter@U1:~/alicesstuff$ cat bob.txt
this is bob's existing file
alice added this
abalter@U1:~/alicesstuff$ echo "alice created this" > alice.txt
abalter@U1:~/alicesstuff$ git add .
abalter@U1:~/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
abalter@U1:~/alicesstuff$ git commit -m "alice's initial commit"
[master 2b9d332] alice's initial commit
2 files changed, 2 insertions(+)
create mode 100644 alice.txt
abalter@U1:~/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
abalter@U1:~/alicesstuff$ cd ../bobsstuff/
abalter@U1:~/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
abalter@U1:~/bobsstuff$ cat bob.txt
this is bob's existing file
alice added this
abalter@U1:~/bobsstuff$ cat alice.txt
alice created this
这里的答案与实际远程机器上的设置没有什么不同:您推送到的存储库应该1 是一个 --bare
克隆。例如,要设置所有内容,您首先要创建一个用户存储库(以便您可以进行一些初始提交):2
$ 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)。现在你有一个空的——没有工作目录——存储库,适用于 cloning-from 和 pushing-to。
要填写它,您现在可以(手动)从 "set-it-up" 存储库中获取所有内容:
git fetch /path/to/set-it-up 'refs/*:refs/*'
此时删除您的 "set-it-up" 副本是安全的:克隆 master-repo.git 目录将为您提供一个可以在其中工作的新克隆,克隆的 origin
设置为master-repo.git目录的路径。
或者,在您的 set-it-up 存储库中,您现在可以将 master-repo.git 目录添加为 "origin" URL,然后推送。
1从 git 2.5 开始,可以通过 non-shared 存储库实现合理的事情(对于某些合理的价值),但这是描述起来有点复杂。
2如果愿意,您可以将初始裸存储库完全清空。克隆一个空的存储库时,git 有点抱怨,但是您可以在克隆中工作并返回以填充裸存储库。