在Git中共享存储库时使用“init --bare”的必要性

时间:2016-08-05 03:55:06

标签: git github version-control

为了共享存储库并与不同的成员开发相同的项目, 我尝试过两种方式分享:

(方法1)在Github中创建一个存储库,然后要求成员在自己的本地计算机中克隆该特定存储库。

(方法2)使用“--bare”进行Git初始化,然后将远程路径设置为与创建的Github存储库路径相同。            然后要求成员克隆Github存储库。

使用这两种方法,可以成功执行pull / add / commit / push命令。 也就是说,存储库是与成员共享的。

通过这样做,我对在Git中共享存储库时使用“init --bare”的必要性感到困惑, 因为我似乎仍然可以在不使用“init --bare”命令的情况下创建共享存储库。

由于我收集的大量信息都说“init --bare”用于共享存储库, 我的问题是“如果不使用裸命令,仍然可以创建共享存储库,那么我们为什么要使用init --bare”?

如果我误解了任何概念,或者有更好的建议,请通知我。

非常感谢。

2016/08/09已添加:

我进行了两次实验:案例一[init]和案例二[init --bare]。

虽然我尝试以类似的方式创建共享存储库, Case One [init]似乎没有成功。

----------------Case One [init]----------------
(create shared_repo)
[/home/huang]$ mkdir git_repo_shared
[/home/huang/git_repo_shared]$ cd git_repo_shared
[/home/huang/git_repo_shared]$ git init
Initialized empty Git repository in /home/huang/git_repo_shared/.git/

(User 1)
[/home/huang/git_repo_shared]$ cd ..
[/home/huang]$ mkdir User1
[/home/huang]$ cd User1
[/home/huang/User1]$ git clone /home/huang/git_repo_shared
Initialized empty Git repository in /home/huang/User1/git_repo_shared/.git/
warning: You appear to have cloned an empty repository.

(User 2)
[/home/huang/User1]$ cd ..
[/home/huang]$ mkdir User2
[/home/huang]$ cd User2
[/home/huang/User2]$ git clone /home/huang/git_repo_shared
Initialized empty Git repository in /home/huang/User2/git_repo_shared/.git/
warning: You appear to have cloned an empty repository.

(The revision made by User1 can syncronize to User2's folder)
[/home/huang/User2]$ cd ~/User1/git_repo_shared
[/home/huang/User1/git_repo_shared]$ echo test_user1 > test_user1.txt
[/home/huang/User1/git_repo_shared]$ git add test_user1.txt


[/home/huang/User1/git_repo_shared]$ git commit -m "test_user1.txt"
[master (root-commit) ae09328] test_user1.txt
 Committer: huang <huang@test-git-a.c.exture-analysis.internal>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

If the identity used for this commit is wrong, you can fix it with:

    git commit --amend --author='Your Name <you@example.com>'

 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 test_user1.txt


[/home/huang/User1/git_repo_shared]$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 244 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
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/huang/git_repo_shared
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/home/huang/git_repo_shared'

$ cd ~/User2/git_repo_shared/
$ git pull origin master
$ ls


----------------Case Two [init --bare]----------------
(create shared_repo)
[/home/huang]$ mkdir git_repo_shared_bare
[/home/huang]$ cd git_repo_shared_bare
[/home/huang/git_repo_shared_bare]$ git init --bare
Initialized empty Git repository in /home/huang/git_repo_shared_bare/

(User 3)
[/home/huang/git_repo_shared_bare]$ cd ..
[/home/huang]$ mkdir User3
[/home/huang]$ cd User3
[/home/huang/User3]$ git clone /home/huang/git_repo_shared_bare
Initialized empty Git repository in /home/huang/User3/git_repo_shared_bare/.git/
warning: You appear to have cloned an empty repository.

(User 4)
[/home/huang/User3]$ cd ..
[/home/huang]$ mkdir User4
[/home/huang]$ cd User4
[/home/huang/User4]$ git clone /home/huang/git_repo_shared_bare
Initialized empty Git repository in /home/huang/User4/git_repo_shared_bare/.git/
warning: You appear to have cloned an empty repository.

(The revision made by User3 can syncronize to User4's folder)
[/home/huang/User4]$ cd ~/User3/git_repo_shared_bare
[/home/huang/User3/git_repo_shared_bare]$ echo test_user3 > test_user3.txt
[/home/huang/User3/git_repo_shared_bare]$ git add test_user3.txt


[/home/huang/User3/git_repo_shared_bare]$ git commit -m "test_user3.txt"
[master (root-commit) 889a5c3] test_user3.txt
 Committer: huang <huang@test-git-a.c.exture-analysis.internal>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

If the identity used for this commit is wrong, you can fix it with:

    git commit --amend --author='Your Name <you@example.com>'

 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 test_user3.txt


[/home/huang/User3/git_repo_shared_bare]$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 242 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/huang/git_repo_shared_bare
 * [new branch]      master -> master

[/home/huang/User3/git_repo_shared_bare]$ cd ~/User4/git_repo_shared_bare
[/home/huang/User4/git_repo_shared_bare]$ git pull origin master
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/huang/git_repo_shared_bare
 * branch            master     -> FETCH_HEAD
[/home/huang/User4/git_repo_shared_bare]$ ls
test_user3.txt

2 个答案:

答案 0 :(得分:1)

它不是机会的必需品。我们知道,我们不会需要已检出的工作目录,因为存储库只是用作共享媒体(如普通团队&#34;推/拉&#34;存储库,或者Github托管了存储库),所以我们跳过它。

现在可能没什么区别,但是如果你在某些共享主机上有很多存储库,那么它可能会堆积起来。而且它不仅仅是已保存的硬盘空间,而且由于工作目录绝对不会被使用/查看,所以它完全可以避免。

所以,简而言之,你过度思考它。 :)

答案 1 :(得分:0)

  1. 裸仓库没有工作树,这意味着我们无法在裸仓库中运行git checkout。因此可以节省大量磁盘空间。

  2. 默认情况下,我们无法推送更新已检出的远程参考,因为它不安全。