[root@localhost ~]# useradd git
[root@localhost ~]# passwd git
[root@localhost ~]# mkdir /git-root/
[root@localhost ~]# cd /git-root/
[root@localhost git-root]# git init --bare shell.git
Initialized empty Git repository in /git-root/shell.git/
注意:
git init 和 git init –bare 的区别:
使用--bare选项时,不再生成.git目录,而是只生成.git目录下面的版本历史记录文件,这些版本历史记录文件也不再存放在.git目录下面,而是直接存放在版本库的根目录下面.
用"git init"初始化的版本库用户也可以在该目录下执行所有git方面的操作。但别的用户在将更新push上来的时候容易出现冲突。
使用”git init –bare”方法创建一个所谓的裸仓库,之所以叫裸仓库是因为这个仓库只保存git历史提交的版本信息,而不允许用户在上面进行各种git操作,如果你硬要操作的话,只会得到下面的错误(”This operation must be run in a work tree”)这个就是最好把远端仓库初始化成bare仓库的原因
[root@localhost git-root]# chown -R git:git shell.git
[root@localhost git-root]# su - git
[git@localhost ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/git/.ssh/id_rsa): Created directory '/home/git/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/git/.ssh/id_rsa.
Your public key has been saved in /home/git/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:fWnqJTR7uMvajcOELlrcz/cGxZHtLZbPfo7ROT3in5Q git@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| o |
| o .|
| . +.|
| . .* o|
| S.+ +o + |
| . ....B. .*|
| o..o= oo.Eo|
| .. .*oBo +o*|
| .. ...X+.+++o|
+----[SHA256]-----+
[git@localhost ~]$
[git@localhost ~]$ cd .ssh/
[git@localhost .ssh]$ cp id_rsa.pub authorized_keys
[git@localhost .ssh]$ vim authorized_key
[git@localhost .ssh]$ logout
[root@localhost git-root]# usermod -s /usr/bin/git-shell git
[root@localhost git-root]# cd
[root@localhost ~]# cd /opt/
[root@localhost opt]# git clone git@192.168.1.102:/git-root/shell.git
Cloning into 'shell'...
The authenticity of host '192.168.1.102 (192.168.1.102)' can't be established.
ECDSA key fingerprint is SHA256:mytNPhHxff0nDGl3LGorCnwAscYkBONVssV44ntQFjw.
ECDSA key fingerprint is MD5:a4:30:b9:1c:35:4a:3b:9c:e5:3d:24:7c:62:26:c7:35.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.102' (ECDSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.
[root@localhost opt]# ls
rh shell
[root@localhost opt]# cd shell/
[root@localhost shell]# vim test1.sh
[root@localhost shell]# git add test1.sh
[root@localhost shell]# git commit -m 'first commit'
[master (root-commit) 33c5fbf] first commit
1 file changed, 2 insertions(+)
create mode 100644 test1.sh
[root@localhost shell]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 230 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.1.102:/git-root/shell.git
* [new branch] master -> master
[root@localhost shell]# 1、http://github.com 注册账户
2、在github上创建仓库
3、生成本地ssh key
[root@localhost ~]# ssh-keygen -t rsa -C 'meteor@163.com' # 邮箱要与github上注册的相同
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:RiE6UR1BtzV5avyE2uz6TNPsVHa2D2eHprghrJEkd/g meteor@163.com
The key's randomart image is:
+---[RSA 2048]----+
| ..oo=o. o. |
| o ..o o... |
| o . .. + |
| . o = . |
| . + S = o =|
| + * . oo.=o|
| o E ..o B.+|
| o . =.* +o|
| . +++ . .|
+----[SHA256]-----+
[root@localhost ~]#
[root@localhost ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVThfq4brrlsPGtAknVB0TLPx+7Dd3qlxTbSIrUOsGC5Y8JuNqVTlIntZB4oNj8cSQrWvec9CKm0a8o7WwaJIiqpxurz+YpQHP2KbapftKIxsX4hPf/z+p0El1U6arQa35/xmNsq+cJLH/bDdRG+EMDhuCBmjVZOlLj/hEdeIT6s56AnnCkaWoF+sq58KCF7Tk54jRbs/YiyE4SN7FuA70r+07sA/uj0+lmuk4E190KtQUELhjX/E9stivlqiRhxnKvVUqXDywsjfM8Rtvbi4Fg9R8Wt9fpd4QwnWksYUoR5qZJFYXO4hSZrUnSMruPK14xXjDJcFDcP2eHIzKgLD1 meteor@163.com4、复制以上的公钥,在github 中添加ssh key
5、测试
[root@localhost ~]# yum install git
........
[root@localhost ~]# ssh -T git@github.com
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Hi meteor! You've successfully authenticated, but GitHub does not provide shell access.
[root@localhost ~]#原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。