Ubuntu使用git来管理代码

首先安装:
sudo apt-get install git-core
sudo apt-get install openssh-server

sudo apt-get install gitosis
更改python默认版本
sudo rm /usr/bin/python

sudo ln -s python2.6 /usr/bin/python
然后:
增加名为Git的用户,命令行禁用密码
sudo
adduser --system --shell /bin/sh  --gecos 'git version control' --group
--disabled-password --home /home/git git
因为git需要ssh验证 ,所以在本地机器上生成ssh key

默认会在 ~/.ssh 下生成两个文件
私钥(本地保存) id_rsa
公钥(上传服务器) id_rsa.pub


ssh-keygen -t rsa

ls ~/.ssh/
id_rsa  id_rsa.pub  known_hosts

上传公钥到USER用户的根目录下
scp .ssh/id_rsa.pub USER@YOUR_SERVER:
sudo -H -u
git gitosis-init < /home/USER/id_rsa.pub
sudo chmod 755
/home/git/repositories/gitosis-admin.git/hooks/post-update

如果有密钥,或迁移密钥到本地,会需要下面的步骤,( 默认可忽略此步骤 )
#启动ssh-agent,
ssh-agent

#添加id_rsa到ssh-agent
ssh-add id_rsa
到这一步,基本上都安装好了,可以直接到本地进行测试
git
clone git@YOUR_SERVER:gitosis-admin.git

--------------------------


首先我们在服务器上初始化一个git 项目:

[root@li96-10 repository]# mkdir blog.git


[root@li96-10 repository]# cd blog.git/

[root@li96-10 blog.git]#
git init --bare #一定要加上--bare,且该目录不能有任何文件和目录,否则报错:fatal: Out of memory? mmap
failed: No such device

Initialized empty Git repository in
/usr/local/system/repository/blog.git/

接下来我们在创建一个git 项目:


[root@li96-10 blog.git]# cd ..

[root@li96-10 repository]# mkdir
test.git

[root@li96-10 repository]# cd test.git/

[root@li96-10
test.git]# echo "some content" >> file

[root@li96-10 test.git]#
git add file

fatal: Not a git repository (or any of the parent
directories): .git

[root@li96-10 test.git]# git init

Initialized
empty Git repository in /usr/local/system/repository/test.git/.git/


[root@li96-10 test.git]# git add file

[root@li96-10 test.git]#
git commit -am "first commit"

[master (root-commit) 3ea3176] first
commit

1 files changed, 1 insertions(+), 0 deletions(-)

create
mode 100644 file

将test.git push到blog.git项目上

[root@li96-10
test.git]# git remote add origin ../blog.git

[root@li96-10 test.git]#
git push origin master

Counting objects: 3, done.

Writing
objects: 100% (3/3), 213 bytes, done.

Total 3 (delta 0), reused 0 (delta
0)

Unpacking objects: 100% (3/3), done.

To ../blog.git

*
[new branch]      master -> master

现在我们从blog.git中clone出来:


[root@li96-10 blog.git]# cd ..

[root@li96-10 repository]# git
clone blog.git #因为我们的reposi是blog.git,clone下来后就会是blog了

Initialized empty
Git repository in /usr/local/system/repository/blog/.git/

[root@li96-10
repository]# cd blog

[root@li96-10 blog]# ll

total 4


-rw-r--r-- 1 root root 13 Jan  4 22:09 file


可以看到项目中果然有个file文件,是刚才我们push进去的
那么下面我们在新clone的branch blog里面增加一个文件看看,


[root@li96-10 blog]# echo 'Second file contents' > file2


[root@li96-10 blog]# git add file2

[root@li96-10 blog]# git
commit -am'added second file'

[master 63be855] added second file


1 files changed, 1 insertions(+), 0 deletions(-)

create mode
100644 file2

[root@li96-10 blog]# git status

# On branch master


# Your branch is ahead of 'origin/master' by 1 commit.

#


nothing to commit (working directory clean)

然后我们push到blog.git


[root@li96-10 blog]# git push

Counting objects: 4, done.


Delta compression using up to 4 threads.

Compressing objects:
100% (2/2), done.

Writing objects: 100% (3/3), 280 bytes, done.


Total 3 (delta 0), reused 0 (delta 0)

Unpacking objects: 100%
(3/3), done.

To /usr/local/system/repository/blog.git

  
3ea3176..63be855  master -> master

这样的话,我们的blog.git已经有两个文件了file,file2

下面我们进到test.git里面,并且将刚才在first的修改合并过来:

[root@li96-10 blog]# cd
../test.git/

[root@li96-10 test.git]# git fetch origin

remote:
Counting objects: 4, done.

remote: Compressing objects: 100% (2/2),
done.

remote: Total 3 (delta 0), reused 0 (delta 0)

Unpacking
objects: 100% (3/3), done.

From ../blog

   3ea3176..63be855 
master     -> origin/master

[root@li96-10 test.git]# git merge
origin/master

Updating 3ea3176..63be855

Fast forward


file2 |    1 +

1 files changed, 1 insertions(+), 0 deletions(-)


create mode 100644 file2


我们可以看到,已经将在first的修改合并过来了,在上面的操作中我们用了fetch和merge,其实我们可以将两个命令简化为一个pull命令:


[root@li96-10 test.git]# git pull origin master

From ../blog


* branch            master     -> FETCH_HEAD

Already
up-to-date.

总结一下,将版本库拿下来的命令是pull,而将本地修改与版本库合并的命令是push.并且push之前先要pull哦!

当然了,这些操作都是在一台机器上操作的,下面演示一下使用ssh协议连线到真正的remote 机器:
从remote上clone下来:


[root@master repository]# git clone
ssh://gituser@xxx.xxx.xxx.xxx/path/to/repository/blog.git

Initialized
empty Git repository in /root/repository/blog/.git/

remote: Counting
objects: 1055, done.

remote: Compressing objects: 100% (822/822), done.


remote: Total 1055 (delta 115), reused 1055 (delta 115)


Receiving objects: 100% (1055/1055), 1.29 MiB | 19 KiB/s, done.


Resolving deltas: 100% (115/115), done.

[root@master
repository]# ll

总计 4

drwxr-xr-x 17 root root 4096 01-05 20:57
blog

然后,当我们在本地做了修改,想要提交到remote的时候,我们就可以直接push了:

[root@master
repository]# cd blog/

[root@master blog]# git push

Everything
up-to-date

这样就基本OK了!
提交之后,在另外一个客户端通过git pull 就可以更新到代码了。


附上参考资料:
http://www.stylejar.com/archives/ubuntu_install_git_server.html

http://xf986321.iteye.com/blog/702974
http://www.linuxsir.org/bbs/thread281294.html
git原理http://www.iteye.com/topic/732199

posted @ 2012-07-05 14:36  坏混混  阅读(656)  评论(0编辑  收藏  举报