git|git init与git init --bare的区别,另附创建git仓库步骤

在本文中我将:

  1. 使用“普通库”代指用"git init"命令创建的GIT库;
  2. 使用“裸库”代指用"git init --bare"命令创建的GIT库;
当你创建一个普通库时,在工作目录下,除了.git目录之外,你还可以看到库中所包含的所有源文件。你拥有了一个可以进行浏览和修改(add, commit, delete等)的本地库。
当你创建一个裸库时,在工作目录下,只有一个.git目录,而没有类似于本地库那样的文件结构可供你直接进行浏览和修改。但是你仍旧可以用git show命令来进行浏览,举个例子(参数为某个commit的SHA1值):
# git show 921dc435a3acd46e48e3d1e54880da62dac18fe0

一般来说,一个裸库往往被创建用于作为大家一起工作的共享库,每一个人都可以往里面push自己的本地修改。一个惯用的命名方式是在库名后加上.git,举个例子:
//建议在创建example.git时候,应该切换至git用户 [root@host-192-0-11-183 home]# su git bash-4.2$ pwd /home bash-4.2$ mkdir testGitrepo mkdir: cannot create directory ‘testGitrepo’: Permission denied bash-4.2$ mkdir testGitrepo mkdir: cannot create directory ‘testGitrepo’: Permission denied /*上述显示,无法在home目录下,使用git用户创建testGitrepo目录,那么这是为啥呢?因为home目录的没有git用户的操作权限。所以 step1:先使用root用户创建testGitrepo目录 */ [root@host-192-0-11-183 home]# mkdir testGitrepo [root@host-192-0-11-183 home]# ll drwxr-xr-x2 root root6 Mar5 01:53 testGitrepo /*step2:将testGitrepo目录chown git*/ [root@host-192-0-11-183 home]# chown git testGitrepo/ [root@host-192-0-11-183 home]# ll total 106508 drwxr-xr-x2 gitroot6 Mar5 01:53 testGitrepo /*step3:进入testGitrepo,su git 然后mkdir project.git*/ [root@host-192-0-11-183 home]# cd testGitrepo/ [root@host-192-0-11-183 testGitrepo]# ll total 0 [root@host-192-0-11-183 testGitrepo]# su git bash-4.2$ mkdir project.git /*step4:cd project.git使用git init --bare初始化git仓库*/ bash-4.2$ cd project.git/ bash-4.2$ ls bash-4.2$ git init --bare Initialized empty Git repository in /home/testGitrepo/project.git/ bash-4.2$ pwd /home/testGitrepo/project.git //

这样你便拥有了一个叫做example的共享库。在你自己的本地机器上,你可以用git remote add命令做初始化check-in:
// assume there're some initial files you want to push to the bare repo you just created, // which are placed under example directory # cd example # git init # git add * # git commit -m "My initial commit message" /* git@意思相当于使用git用户进行ssh登陆 与上面git server端使用su git切换用户,之后再创建project.git相呼应 猜想git remote add origin root@example.com:example.git 是不是就可以在git server端不使用su git切换用户,再创建project.git了呢? 待验证 */ # git remote add origin git@example.com:example.git # git push -u origin master

项目团队里面的每个人都可以clone这个库,然后完成本地修改之后,往这个库中push自己的代码。
# git clone git@example.com:example.git # cd example

猜想git remote add origin root@example.com:example.git
是不是就可以在git server端不使用su git切换用户,再创建project.git了呢,直接使用root创建project.git?
待验证
本文绝对非原创仅记录用
参考:
  1. 普通库与裸库的区别:http://stackoverflow.com/questions/78611...
  2. 该如何使用一个裸库:http://stackoverflow.com/questions/76324...
  3. 什么是GIT裸库:http://www.saintsjd.com/2011/01/what-is-...
  4. 如何设置一个远程共享库并进行团队协作:http://thelucid.com/2008/12/02/git-setti...
  5. git remote add与git clone的区别:http://stackoverflow.com/questions/48555...
【git|git init与git init --bare的区别,另附创建git仓库步骤】http://www.voidcn.com/article/p-rulqnwwt-bsk.html

    推荐阅读