centos下 Git 上传下载文件

将git设置为默认路径,不然后面克隆时会报错

[root@localhost code]$ ln -s /usr/local/git/bin/git-upload-pack /usr/bin/git-upload-pack 

[root@localhost code]$ ln -s /usr/local/git/bin/git-receive-pack /usr/bin/git-receive-pack 

至此,CentOS 就安装上了最新版本的 Git。

第二步,创建一个git用户组和用户,用来运行git服务:

$ groupadd git
$ useradd git -g git
$ passwd git  #参数是用户名
  • 1
  • 2
  • 3

最好切换到git用户 不然后面新建的git仓库都要改权限 烦烦烦!!

$ su - git 
  • 1
 
  • 1

第三步,创建证书登录:

如何生成密钥:http://blog.csdn.net/permanent_2008/article/details/73839384

备注:下边虚线内容为多余内容,只是留着存档而已。于本教程没有关系

添加证书之前,还要做这么一步:

Git服务器打开RSA认证 。在Git服务器上首先需要将/etc/ssh/sshd_config中将RSA认证打开,

即:

1.RSAAuthentication yes

2.PubkeyAuthentication yes

3.AuthorizedKeysFile .ssh/authorized_keys

这里我们可以看到公钥存放在.ssh/authorized_keys文件中。

所以我们在/home/git下创建.ssh目录,然后创建authorized_keys文件,并将刚生成的公钥导入进去。

然后再次clone的时候,或者是之后push的时候,就不需要再输入密码了:

Zhu@XXX/E/testgit/8.34 $ git clone [email protected]:/data/git/learngit.git Cloning into ‘learngit’… warning: You appear to have cloned an empty repository. Checking connectivity… done.

===============================

收集所有需要登录的用户的公钥,就是他们自己的id_rsa.pub文件,把所有公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。

$ cd /home/git/ $ mkdir .ssh #新建文件夹 $ chmod 700 .ssh $ touch .ssh/authorized_keys #新建文件 $ chmod 600 .ssh/authorized_keys

第四步,初始化Git仓库

$ cd /home/git
$ git init --bare test.git
Initialized empty Git repository in /home/git/test.git/
  • 1
  • 2
  • 3

以上命令会创建一个空仓库,服务器上的Git仓库通常都以.git结尾。



第五步、本地克隆仓库

$ git clone git@your-ip:test.git
Cloning into 'test'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
  • 1
  • 2
  • 3
  • 4

your-ip 为您 Git 所在服务器 ip 

 

用git clone 获取服务器上的代码

[root@localhost code]$ git clone [email protected]:/root/code.git 

报错如下:

bash: git-upload-pack: command not found
fatal: The remote end hung up unexpectedly

什么原因呢?原来代码服务器【192.168.57.61】上的git安装路径是/usr/local/git,不是默认路径,根据提示,在git服务器192.168.57.61上, 建立链接文件:

[root@localhost code]# ln -s /usr/local/git/bin/git-upload-pack /usr/bin/git-upload-pack 

再次,执行git clone ,果真可以了。

当然,如果无法修改git代码服务器上配置,可以在clone时,添加–upload-pack选项来指定git服务器上的git-upload-pack 路径,达到上面相同的目的,如下所示:

[root@localhost code]$ git clone –upload-pack “/usr/local/git/bin/git-upload-pack” [email protected]:/root/code.git 

当然,也许你会遇到git-receive-pack 之类的错误,很有可能和这个原理是一样的,请采用类似的操作即可

5.禁止Shell登录

出于安全考虑,git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。 
找到类似下面的一行:

git:x:502:502::/home/git:/bin/bash
  • 1

改为

git:x:502:502::/home/git:/usr/local/git/bin/git-shell
  • 1

这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出



参考文档:
git安装部署  http://www.cnblogs.com/imayanlong/p/5617626.html 
git/ssh捋不清的几个问题  http://www.cnblogs.com/hustskyking/p/problems-in-git-when-ssh.html 
centos7下git服务器端搭建和验证  https://my.oschina.net/u/2343829/blog/644663  
Git 服务器搭建  http://www.runoob.com/git/git-server.html   
Windows下SSH连接git服务器  https://www.zybuluo.com/zhutoulwz/note/30495   
 
 

Git 提交的时候,git push 报错error: remote unpack failed: unpack-objects abnormal exit


远程仓库权限问题,进入到仓库目录控制台输入


chown -R git:git somthing.git

猜你喜欢

转载自blog.csdn.net/jsboy123/article/details/80624222