准备工作:

1、一台虚拟linux环境和window

开始工作

       1、安装git(略)

  2、创建git用户和创建test.git裸仓库

  1. [root@localhost ~]# useradd -m git #创建用户,shell是bash
  2. [root@localhost ~]# su git
  3. [git@localhost root]$ cd ~
  4. [git@localhost ~]$ mkdir repository && cd repository
  5. [git@localhost repository]$ git init --bare test.git

  此时在repository目录生成test.git,作为远程仓库

  3、生成公私钥(多种工具生成),此处使用ssh-keygen,大概情形如下

  1. [git@localhost .ssh] cd /home/git/.ssh #没有.ssh目录,自行创建
  2. [git@localhost .ssh]$ ssh-keygen -t rsa -C 'git@master.com'
  3. Generating public/private rsa key pair.
  4. Enter file in which to save the key (/home/git/.ssh/id_rsa):
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in /home/git/.ssh/id_rsa.
  8. Your public key has been saved in /home/git/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. SHA256:J06qIJSTy2n0kSfmAwavvxrv2fwK1pd2UwifUg6axl4 git@master.com
  11. The key's randomart image is:
  12. +---[RSA 2048]----+
  13. | |
  14. | |
  15. |. o . |
  16. |..o..o * o |
  17. | O.==.E S o |
  18. |=.O++. * + |
  19. |+=++o = + |
  20. |.* *.+ . . |
  21. |.o*.=o. |
  22. +----[SHA256]-----+

  会生成两个文件id_rsa和id_rsa.pub,以.pub结尾的是公钥,将公钥放到authorized_keys文件中,实现免密登录

  1. [git@localhost .ssh]$ cat id_rsa.pub >> ./authorized_keys

  4、window客户端

  1. $ git clone git@ip:/home/git/repository/test.git

  5、Linux客户端

  1. [git@localhost ~]$ mkdir test && cd test
  2. [git@localhost test]$ git init #初始化仓库
  3. [git@localhost test]$ git remote add origin git@ip:/home/git/repostroy/test.git
  4. [git@localhost test]$ git pull origin master
  5. The authenticity of host 'ip (ip)' can't be established.
  6. ECDSA key fingerprint is SHA256:uSKcAOh8Q8Lca9vlPQhjVGm8lPtEpc09Ze4GTW70lFs.
  7. ECDSA key fingerprint is MD5:66:f8:99:85:bc:e2:20:51:f0:4e:59:88:30:2e:5a:2d.
  8. Are you sure you want to continue connecting (yes/no)? yes
  9. Warning: Permanently added 'ip' (ECDSA) to the list of known hosts.
  10. remote: Counting objects: 100, done.
  11. remote: Compressing objects: 100% (73/73), done.
  12. remote: Total 100 (delta 5), reused 0 (delta 0)
  13. Receiving objects: 100% (100/100), 7.68 KiB | 0 bytes/s, done.
  14. Resolving deltas: 100% (5/5), done.
  15. From 192.168.0.159:/home/git/repostroy/test
  16. * branch master -> FETCH_HEAD

  6、使用post-update实现自动化 

  1. [git@localhost ~]$ /home/git/repostroy/test.git/hooks/post-update
  2. # 添加如下内容
  3. unset GIT_DIR
  4. cd /home/git/fail
  5. git pull origin master

  7、测试(window客户端)

 

   成功!!!!

碰到的几个问题

1、post-receive和post-update的区别、

2、远程无法执行

 答、需要给post-receive或者post-update执行权限

3、如图

  

 答:要在git用户生成公私钥,因为git远程库是用git用户做pull操作的

4、cannot open .git/FETCH_HEAD: Permission denied

 答:查看FETCH_HEAD的所属主、所属组是不是git,不是用户chown命令修改

5、暂时没有

 

 

  

  

版权声明:本文为xuqiuye原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xuqiuye/p/12201065.html