Chinaunix首页 | 论坛 | 博客
  • 博客访问: 31039980
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: 嵌入式

2018-05-04 11:53:34

建立一个 Git 代码共享仓库服务器。

1. 服务器

安装ssh
sudo apt-get install openssh-server
ps -e|grep ssh


通常用 SSH 协议即可,我们应该为 Git 创建一个专用账号。

$ sudo useradd git

$ sudo passwd git
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully


创建一个用来保存代码仓库的目录,注意赋予 git 账号读写权限。

$ sudo mkdir -p /var/git.server/project1
$ cd /var/git.server

$ sudo chown git project1
$ sudo chgrp git project1

$ ls -l
total 4
drwxr-xr-x 2 git git 4096 2010-05-17 00:55 project1


初始化 project1,注意在服务器上我们无需保留工作目录,因此创建一个纯粹(bare)的代码仓库。

$ cd project1/

$ sudo su git

$ pwd
/var/git.server/project1

$ git --bare init
Initialized empty Git repository in /var/git.server/project1/

$ ls -l
total 32
drwxr-xr-x 2 git git 4096 2010-05-17 00:59 branches
-rw-r--r-- 1 git git   66 2010-05-17 00:59 config
-rw-r--r-- 1 git git   73 2010-05-17 00:59 description
-rw-r--r-- 1 git git   23 2010-05-17 00:59 HEAD
drwxr-xr-x 2 git git 4096 2010-05-17 00:59 hooks
drwxr-xr-x 2 git git 4096 2010-05-17 00:59 info
drwxr-xr-x 4 git git 4096 2010-05-17 00:59 objects
drwxr-xr-x 4 git git 4096 2010-05-17 00:59 refs

$ exit


我们在服务器上克隆一份用于管理和测试(应该禁止直接操作服务器仓库目录)。

$ git clone /var/git.server/project1/
Initialized empty Git repository in /home/yuhen/project1/.git/
warning: You appear to have cloned an empty repository.

$ ls -al project1
total 12
drwxr-xr-x  3 yuhen yuhen 4096 2010-05-17 01:02 .
drwxr-xr-x 10 yuhen yuhen 4096 2010-05-17 01:02 ..
drwxr-xr-x  7 yuhen yuhen 4096 2010-05-17 01:02 .git


我们添加点项目初始化文件。

$ cd project1

$ cat > .gitingore << end
> *~
> *.swp
> end

$ touch README

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#       .gitingore
#       README
nothing added to commit but untracked files present (use "git add" to track)

$ git add .
$ git commit -am "Start"
[master (root-commit) 723471e] Start
 1 files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 .gitingore
 create mode 100644 README


我们向服务器提交第一个版本。

$ git push git@localhost:/var/git.server/project1/ master

Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 258 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@localhost:/var/git.server/project1/
 * [new branch]      master -> master


通常情况下,我们可以用 origin 来代替服务器地址,不过当前测试账号没有写 git.server/project1 的权限,因此用 ssh 路径。同时需要指定 branch。

2. 客户端

好了,现在作为一个普通程序员,我们开始为 project1 项目工作。

$ git clone git@192.168.1.202:/var/git.server/project1
Initialized empty Git repository in /home/yuhen/project1/.git/
git@192.168.1.202's password: 
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (4/4), done.

$ ls -al project1
total 16
drwxr-xr-x  3 yuhen yuhen 4096 2010-05-17 01:11 .
drwxr-xr-x 27 yuhen yuhen 4096 2010-05-17 01:10 ..
drwxr-xr-x  8 yuhen yuhen 4096 2010-05-17 01:11 .git
-rw-r--r--  1 yuhen yuhen    9 2010-05-17 01:11 .gitingore
-rw-r--r--  1 yuhen yuhen    0 2010-05-17 01:11 README


代码已经克隆回来了,我们添加或修改一些文件。

$ touch INSTALL

$ git status
# On branch master
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#    INSTALL
nothing added to commit but untracked files present (use "git add" to track)

$ git add .

$ git commit -am "INSTALL"
[master b85e275] INSTALL
 Committer: yuhen 
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 INSTALL


在将代码提交(push)到服务器之前,首先要确认相关更新已经合并(merge)到 master 了,还应该先从服务器刷新(pull)最新代码,以确保自己的提交不会和别人最新提交的代码冲突。

$ git pull origin master
git@192.168.1.202's password: 
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From 192.168.1.202:/var/git.server/project1
 * branch            master     -> FETCH_HEAD
Merge made by recursive.
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 HISTORY

$ git push origin master
git@192.168.1.202's password: 
Could not chdir to home directory /home/git: No such file or directory
Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 474 bytes, done.
Total 4 (delta 2), reused 0 (delta 0)
To git@192.168.1.202:/var/git.server/project1
   c4d7b1e..ee8cfb3  master -> master

$ git log
commit ee8cfb3d14eed091e6f96d60af68ec07c05ab09d
Merge: b85e275 c4d7b1e
Author: yuhen 
Date:   Mon May 17 01:17:49 2010 +0800

    Merge branch 'master' of 192.168.1.202:/var/git.server/project1

commit c4d7b1e796cf52e0b600f2c7e992f304052fa8c1
Author: Q.yuhen 
Date:   Mon May 17 01:17:26 2010 +0800

    HISTORY

commit b85e275b52812e3d9ac36da78fb8cc924380a58c
Author: yuhen 
Date:   Mon May 17 01:13:35 2010 +0800

    INSTALL

commit 723471e3421d7fdfa80bf31e5c27f5a174e95afd
Author: Q.yuhen 
Date:   Mon May 17 01:05:53 2010 +0800

    Start


我们应该避免频繁向服务器提交代码,而是在一个相对稳定的版本测试通过后再进行。
基本操作就是这些了,当然我们还可以提供只读账号或者 HTTP 访问协议,相关内容可参考《Pro Git》

阅读(1257) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~