自己搭建git 代码服务器

Posted app

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自己搭建git 代码服务器相关的知识,希望对你有一定的参考价值。

使用git服务器的工程师都需要生成一个ssh的公钥

~/.ssh$ ssh-keygen
Generating public/private rsa key pair.
……………………
……………………
然后一律按回车,完成后会在home目录下的.ssh目录中生成两个文件id_rsa(私有密钥)和id_rsa.pub(公钥)。
将生成的公钥id_rsa.pub发给git仓库的管理者。
 
然后在服务器端:
sudo adduser git
su git
cd ~
mkdir .ssh
在git用户新建一个.ssh的目录,将开发者的SSH公钥添加到git这个用户的authorized_keys这个文件当中。如有zhangsan,lisi两个开发者。
cat /tmp/id_rsa_zhangsan.pub >> ~/.ssh/authorized_keys
cat /tmp/id_rsa_lisi.pub >> ~/.ssh/authorized_keys
 
建立一个空的仓库
cd work
mkdir project.git
cd project.git
git --bare init
使用--bare会初始化一个不带任何工作目录的仓库,不会看到工程源码。
 
开发者端:
若没有仓库的情况下,需要首先新建一个仓库。在需要建立仓库的,目录下执行以下的命令。
cd my_project
git init
git add . (将所有代码加入工程管理)
git commit -m "xxx project initial commit"
然后执行:git push origin master
如果出现以下错误:

The authenticity of host ‘localhost (::1)‘ can‘t be established.
RSA key fingerprint is eb:e3:e2:36:84:64:23:f2:b8:7b:a0:4c:ef:d4:9e:7d.
Are you sure you want to continue connecting (yes/no)?
Host key verification failed.

执行一下:$ssh localhost -o StrictHostKeyChecking=no

(用ssh登录一下就好使,就不会弹出The authenticity of host ‘localhost (::1)‘ can‘t be established.的错误了。)

然后再退出ssh登录。

然后:

[email protected]:~/linux-2.6$ git push origin master
Counting objects: 56913, done.
Compressing objects: 100% (56081/56081), done.
Writing objects: 100% (56913/56913), 150.96 MiB | 4.08 MiB/s, done.
Total 56913 (delta 4433), reused 0 (delta 0)
To [email protected]:/home/git/work/project.git
* [new branch] master -> master
[email protected]:~/linux-2.6$ ls

就好了。

 

小组内开发者b下载代码时:

[email protected]:~$ git clone [email protected]:/home/git/work/project.git
Initialized empty Git repository in /home/b/project/.git/
The authenticity of host ‘localhost (::1)‘ can‘t be established.
RSA key fingerprint is eb:e3:e2:36:84:64:23:f2:b8:7b:a0:4c:ef:d4:9e:7d.
Are you sure you want to continue connecting (yes/no)?
Host key verification failed.
fatal: The remote end hung up unexpectedly
[email protected]:~$

同样执行一下:
[email protected]:~$ ssh localhost -o StrictHostKeyChecking=no

然后:

[email protected]:~$ git clone [email protected]:/home/git/work/project.git
Initialized empty Git repository in /home/b/project/.git/
[email protected]‘s password:
remote: Counting objects: 56913, done.
remote: Compressing objects: 100% (51648/51648), done.
remote: Total 56913 (delta 4433), reused 56913 (delta 4433)
Receiving objects: 100% (56913/56913), 150.96 MiB | 13.48 MiB/s, done.
Resolving deltas: 100% (4433/4433), done.
Checking out files: 100% (53646/53646), done.
[email protected]:~$ ls
examples.desktop project
[email protected]:~$ ls
examples.desktop project
[email protected]:~$
[email protected]:~$
[email protected]:~$ cd project/
[email protected]:~/project$ ls
arch crypto include kernel net security
block Documentation init lib README sound
certs drivers ipc MAINTAINERS REPORTING-BUGS tools
COPYING firmware Kbuild Makefile samples usr
CREDITS fs Kconfig mm scripts virt

分析 ssh localhost -o StrictHostKeyChecking=no可能不会检查公钥的正确性就让登录了。

 

以上是关于自己搭建git 代码服务器的主要内容,如果未能解决你的问题,请参考以下文章

搭建本地私有的git服务器

在Linux下搭建Git服务器

git服务器搭建post-receive 钩子部署服务端代码

git分布式的理解----简单服务端搭建

Git连接GitLab远程仓库

自己搭建git 代码服务器