现在的位置: 首页 > 综合 > 正文

Git–用git建立code库

2013年09月22日 ⁄ 综合 ⁄ 共 2384字 ⁄ 字号 评论关闭

利用点时间,把自己这段时间使用git的工具的内容,使用过程中遇到的问题都梳理下。首先我们建立一个文件库(基于Ubuntu系统):

1.必须要安装:

[html] view
plain
copy

  1. sudo apt-get install git-core  

2.建立code库:

[html] view
plain
copy

  1. ~$ mkdir gitTest  
  2.   
  3. ~$ cd gitTest/  
  4.   
  5. ~/gitTest$ mkdir test.git  
  6.   
  7. ~/gitTest$ cd test.git/  
  8.   
  9. ~/gitTest/test.git$ git --bare init  

这样一个空的库就建好了,看下空的库中都有什么东西:

[html] view
plain
copy

  1. ~/gitTest/test.git$ ll .git/  
  2. branches/    config    description  HEAD    hooks/    info/   objects/  refs/  

关注以下三项内容:

HEAD 的文件:

[html] view
plain
copy

  1. ~/gitTest/test.git/.git$ cat HEAD   
  2. ref: refs/heads/master  

HEAD 文件中的内容其实只是包含了一个索引信息,并且,这个索引将总是指向你的项目中的当前开发分支。master 是默认的分支,这也是为什么 .git/HEAD 创建的时候就指向 master 的原因,尽管目前它其实并不存在。 git 将假设你会在 master 上开始并展开你以后的工作,除非你自己创建你自己的分支。

实际上你可以将你的工作分支叫任何名字,而不必在版本库中一定要有一个叫 master 的分支.

objects子目录:

它包含了你的项目中的所有对象,我们不必直接地了解到这些对象内容,我们应该关心是存放在这些对象中的项目的数据。目前它只包含info,pack两个空文件夹。

refs子目录:

它用来保存指向对象的索引。具体地说,子目录 refs 包含着两个子目录叫 heads 和 tags,就像他们的名字所表达的意味一样:他们存放了不同的开发分支的头的索引, 或者是你用来标定版本的标签的索引。

以上,一个空的文件库已经建好了(作为服务器端),然后使用ssh localhost 命令测试ssh 是否联通?

[html] view
plain
copy

  1. ssh: connect to host localhost port 22: Connection refused  

出现这句话说明ssh 没有联通。连接不通的原因可能是ssh没有安装,或者存在防火墙,不过一般个人使用的ubuntu很少有装防火墙。

我的是由于没有安装ssh 客户端和服务器端:

[html] view
plain
copy

  1. sudo apt-get install openssh-client  
  2.   
  3. sudo apt-get install openssh-server  

如果用sudo apt-get install 不行的话,参考在ubuntu安装ssh 这篇文章。

安装之后在测试下,发现可以联通了,出现如下信息:

[html] view
plain
copy

  1. ssh localhost  
  2. The authenticity of host 'localhost (127.0.0.1)' can't be established.  
  3. ECDSA key fingerprint is d8:a4:3a:91:06:ab:6b:57:a2:6d:e4:a7:04:8f:a3:3f.  
  4. Are you sure you want to continue connecting (yes/no)?  

3.在本地的git仓库"添加一个远程仓库"

[html] view
plain
copy

  1. git remote add origin ssh://172.17.22.11:/home/carson/gitTest/test.git/  

添加远程仓库前后:cat test.git/config,信息对比会多出如下信息:

[remote "origin"]
url = ssh://172.17.22.11:/home/carson/gitTest/test.git/
fetch = +refs/heads/*:refs/remotes/origin/*

4.远程Clone code:

[html] view
plain
copy

  1. git clone carson@172.17.22.11:/home/carson/gitTest/test.git/  

5.远程添加Code文件:    

[html] view
plain
copy

  1. git add firstCode.java  
  2.   
  3. git commit -m "add new file" firstCode.java  
  4.   
  5. git push  

git push 的时候会出现如下错误:

No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'carson@172.17.22.11:/home/carson/gitTest/test.git/'

解决方法:在git Server 端:

vim .git/config
添加:
          [receive]
           denyCurrentBranch = ignore

功能:允许客户端push(上传代码到.git仓库)代码。

注意:第一次git push的时候,要加上 git push origin master

抱歉!评论已关闭.