1)服务端
安装Git
[root@localhost ~]# apt-get install -y git
创建一个git用户,用来运行git服务
[root@localhost ~]# adduser git // 创建 git 用户
[root@localhost ~]# cd ~git // 进入git用户的home目录
[root@localhost ~]# mkdir .ssh
[root@localhost ~]# chmod 700 .ssh
[root@localhost ~]# touch .ssh/authorized_keys
[root@localhost ~]# chmod 600 .ssh/authorized_keys
禁用shell登录
注,出于安全考虑,创建的git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。找到类似下面的一行,改为:
[root@git ~]# gedit /etc/passwd
git:x:1000:1000::/home/git:/usr/bin/git-shell
这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。
初始化Git仓库
先选定一个目录作为Git仓库,这里是/data/git/project.git。
[root@git ~]# cd /data/git/
[root@git git]# git init --bare project.git
执行以上命令 Git命令,会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾。然后,把owner改为git:
[root@git git]# chown -R git.git project.git
2)客服端
使用 SSH 公钥进行认证
为了向 Git 服务器提供 SSH 公钥,如果某系统用户尚未拥有密钥,必须事先为其生成一份。 这个过程在所有操作系统上都是相似的。 首先,你需要确认自己是否已经拥有密钥。 默认情况下,用户的 SSH 密钥存储在其 ~/.ssh
目录下。 进入该目录并列出其中内容,你便可以快速确认自己是否已拥有密钥:
[root@localhost ~]# ls ~/.ssh
authorized_keys2 id_dsa known_hosts
config id_dsa.pub
如果没有id_rsa.pub文件,则客服端生成SSH公钥
[root@localhost ~]# ssh-keygen
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:
79:87:ad:40:ef:ac:4f:ea:a0:47:f4:45:c3:22:31:66 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| E. . |
| o... + |
| ..o . |
| .. o.o |
| . .S.+ o |
| . .= o |
| .. = |
| ... + |
| .. .+.. |
+-----------------+
[root@localhost ~]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDmUiESftpB4oyLwzqMculvSL9M9yIiraaRinSXLkjNZFBIX9zIxKpCkU7SBKXOfFrQAa0dy+iS8CvAuGmKTQZx27pYXIS/fYpPS5BF/iZP7uQmJaICNuX+Eyog+KceRFyr8TIxsHsMlN74h9t4FusWn7GT7ulLmKc8n1H9t8QPCwT018z9swBzq1+QHJ6nMYGDU2NPai9dvaW3ik69OrxOtAlmHcLPPBkafgdMiL9AkIOGKJzJ73/UcrNUWqFGiwho9pKzWLaoupVYa7t/I+mWXwt/H9tr22qTxiDJZhTVKNPLdeVTUSpj8ZTe9/AAxo4PvNt+jZ5k24jfNEPkmpCl TOYOTA@DESKTOP-O7FUOAH
注,一路回车即可,将生成的id_rsa.pub内容追加到服务器上的/home/git/.ssh/authorized_keys
后面,下次你用git时就不需要输入用户名和密码了。(默认输入密码pull,请忽略此方法)
上传id_rsa.pub到/home/git/.ssh/,重命名为id_rsa.kwin.pub,追加到authorized_keys 文件的末尾
[root@localhost ~]# cd ~git/.ssh
[root@localhost .ssh]# cat id_rsa.kwin.pub >> authorized_keys
[root@localhost ~]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyQ6mcBiyiYiScdh9hBua8cXoOx59VVudyAkA+m+Gc+hUF09oKAyf5LlI1RJkbobX90L3afUexqnHT+hka1oaX4Gu7tfHYu7nJyGVPcteebJ14wNec750kUH0sS+f87U+Sb37Ynmh/FCCTUU+m/goimH5oe/gH8uSh3mFBlA+NKcBPRWCx7W44L5MK4YqcbddmjXsp+JAO6tHaYBn3GnLB3UzLbQHX222AGO6nByHNBmRHMXePaIzH76zWiy/OjiciJzRon/riftO+O+qOA9/+ZoB0KzycA0MeEOwqx5iWwRHzx8WrYufC9PZdvlKe/a4KxSG1XA15y69y0dFfl0CL root@localhost.localdomain
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDmUiESftpB4oyLwzqMculvSL9M9yIiraaRinSXLkjNZFBIX9zIxKpCkU7SBKXOfFrQAa0dy+iS8CvAuGmKTQZx27pYXIS/fYpPS5BF/iZP7uQmJaICNuX+Eyog+KceRFyr8TIxsHsMlN74h9t4FusWn7GT7ulLmKc8n1H9t8QPCwT018z9swBzq1+QHJ6nMYGDU2NPai9dvaW3ik69OrxOtAlmHcLPPBkafgdMiL9AkIOGKJzJ73/UcrNUWqFGiwho9pKzWLaoupVYa7t/I+mWXwt/H9tr22qTxiDJZhTVKNPLdeVTUSpj8ZTe9/AAxo4PvNt+jZ5k24jfNEPkmpCl TOYOTA@DESKTOP-O7FUOAH
说明:我这里有两个用户登录服务器,所以我这里就有两个ssh-rsa
克隆远程仓库
注,现在可以通过git clone命令克隆远程仓库了,在各自的电脑上运行:
root@asus-All-Series:~/git# git clone git@192.168.0.97:/home/git/repositories/test2.git
Cloning into 'test2'...
git@192.168.0.97's password:
warning: You appear to have cloned an empty repository.
本地push到上面的远程库
$ git push origin master
如若出现下列报错
root@asus-All-Series:~/git/test2# git push origin master
git@192.168.0.97's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
error: insufficient permission for adding an object to repository database ./objects
fatal: failed to write object
error: unpack failed: unpack-objects abnormal exit
To git@192.168.0.97:/home/git/repositories/test2.git
! [remote rejected] master -> master (n/a (unpacker error))
error: failed to push some refs to 'git@192.168.0.97:/home/git/repositories/test2.git'
把git用户组下的 git远程库中的code目录重新赋权chmod -R 777 *(对所有文件都赋予最高权限)
root@asus-All-Series:/home/git/repositories# ll test2.git/
总用量 40
drwxrwxrwx 7 asus git 4096 7月 30 12:24 ./
drwxrwxrwx 6 1001 git 4096 7月 30 12:24 ../
drwxr-xr-x 2 root root 4096 7月 30 12:24 branches/
-rw-r--r-- 1 root root 66 7月 30 12:24 config
-rw-r--r-- 1 root root 73 7月 30 12:24 description
-rw-r--r-- 1 root root 23 7月 30 12:24 HEAD
drwxr-xr-x 2 root root 4096 7月 30 12:24 hooks/
drwxr-xr-x 2 root root 4096 7月 30 12:24 info/
drwxr-xr-x 4 root root 4096 7月 30 12:24 objects/
drwxr-xr-x 4 root root 4096 7月 30 12:24 refs/
root@asus-All-Series:/home/git/repositories# chmod 777 test2.git/ -R
显示push成功了
root@asus-All-Series:~/git/test2# git push origin master
git@192.168.0.97's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 215 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.0.97:/home/git/repositories/test2.git
* [new branch] master -> master
本地创建新分支(非master分支)
git branch branchName
root@asus-All-Series:~/git/test2# git branch my-branch
root@asus-All-Series:~/git/test2# git branch -a
*master
my-branch
remotes/origin/master
本地切换到新分支
git checkout branchName
root@asus-All-Series:~/git/test2# git checkout my-branch
Switched to branch 'my-branch'
root@asus-All-Series:~/git/test2# git branch -a
master
* my-branch
remotes/origin/master
以上两个步骤可以合为一个步骤 git branch -b branchName
创建远程新分支(这一步骤在建立本地分支后必须做)
$ git push origin branchName:branchName
本地新分支的提交commit到远端新分支
root@asus-All-Series:~/git/test2# echo "hello git" > hello1.c // 本地新分支创建 hello1.c
root@asus-All-Series:~/git/test2# git add hello1.c // 本地新分支上 add 新文件
root@asus-All-Series:~/git/test2# git commit -m "test1" // 本地新分支上 commit 新文件
[my-branch cfed675] test1
1 file changed, 1 insertion(+)
create mode 100644 hello1.c
root@asus-All-Series:~/git/test2# git push origin my-branch // 本地新分支上 push 新文件到远程
git@192.168.0.97's password:
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 233 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
To git@192.168.0.97:/home/git/repositories/test2.git
* [new branch] my-branch -> my-branch
root@asus-All-Series:~/git/test2# git branch -a // 可以看到远程 remotes/origin/my-branch
master
* my-branch
remotes/origin/master
remotes/origin/my-branch