1.服務端
1、創建目錄存放代碼庫
[root@localhost home]# mkdir git_ck
2、初始化代碼庫testgit.git
[root@localhost git_ck]# git init --bare testgit.git
初始化空的 Git 版本庫于 /home/git_ck/testgit.git/
[root@localhost git_ck]# ls -a
. .. testgit.git
3.配置連接的客戶端的公鑰
cat 客戶端公鑰.txt >> /root/.ssh/authorized_keys
4.重啟sshd 服務
systemctl restart sshd
2. 客戶端
2.1 拉取遠程倉庫
1、初始化文件
admin@admin-PC MINGW64 /e/news/git_ck
$ git init
Initialized empty Git repository in E:/news/git_ck/.git/
2.克隆服務器倉庫
admin@admin-PC MINGW64 /e/news/git_ck (master)
$ git clone root@172.30.4.137:/home/git_ck/testgit.git
Cloning into 'testgit'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (5/5), done.
admin@admin-PC MINGW64 /e/news/git_ck (master)
$ ls -a
./ ../ .git/ testgit/
3.進入項目目錄
admin@admin-PC MINGW64 /e/news/git_ck (master)
$ cd testgit/
admin@admin-PC MINGW64 /e/news/git_ck/testgit (master)
$ ls -a
./ ../ .git/ README.nd
2.2 本地工作區上傳至服務器
1、win客戶端新增文件-提交至master分支
admin@admin-PC MINGW64 /e/news/git_ck/testgit (master)
$ vim test.txt
admin@admin-PC MINGW64 /e/news/git_ck/testgit (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
admin@admin-PC MINGW64 /e/news/git_ck/testgit (master)
$ git add .
admin@admin-PC MINGW64 /e/news/git_ck/testgit (master)
$ git commit .
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory.
[master f296ae0] "oneone"
1 file changed, 1 insertion(+)
2、添加遠程地址并推送
admin@admin-PC MINGW64 /e/news/git_ck/testgit (master)
$ git remote add origin root@172.30.4.137:/home/git_ck/testgit.git
admin@admin-PC MINGW64 /e/news/git_ck/testgit (master)
$ git push origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 272 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To 172.30.4.137:/home/git_ck/testgit.git
34aa5ab..f296ae0 master -> master
3. 測試
注:
服務器倉庫沒有工作區無法查看客戶的提交,故在起一個客戶端查看共享效果
1、將當前服務器作為一個客戶端,克隆testgit.git 倉庫
[root@localhost te_git]# git init
初始化空的 Git 版本庫于 /tmp/te_git/.git/
[root@localhost te_git]# ls -a
. .. .git
[root@localhost te_git]# git clone root@127.0.0.1:/home/git_ck/testgit.git
正克隆到 'testgit'...
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:kb0bK+OA+vfY9owyYaXDIO9oomh4MEK6Bug5RMQtBmg.
ECDSA key fingerprint is MD5:b8:a1:4a:56:de:96:2d:19:ab:ff:e3:89:a9:34:e9:1f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
root@127.0.0.1's password:
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 11 (delta 0), reused 0 (delta 0)
接收對象中: 100% (11/11), done.
[root@localhost te_git]# ls -a
. .. .git testgit
[root@localhost te_git]# cd testgit/
[root@localhost testgit]# ls -a
. .. .git README.nd test.txt
# 注:已發現win客戶端加的test.txt 文件
2、linux客戶端創建requests.py文件并提交
[root@localhost testgit]# touch requests.py
[root@localhost testgit]# git add .
[root@localhost testgit]# git commit .
[master 4befbeb] "requests"
Committer: root <root@localhost.localdomain>
您的姓名和郵件地址基于登錄名和主機名進行了自動設置。請檢查它們正確
與否。您可以通過下面的命令對其進行明確地設置以免再出現本提示信息:
git config --global user.name "Your Name"
git config --global user.email you@example.com
設置完畢后,您可以用下面的命令來修正本次提交所使用的用戶身份:
git commit --amend --reset-author
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 requests.py
[root@localhost testgit]# git push origin master
root@127.0.0.1's password:
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 278 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To root@127.0.0.1:/home/git_ck/testgit.git
7055fb7..4befbeb master -> master
3. 到win客戶端拉取最新提交
admin@admin-PC MINGW64 /e/news/git_ck/testgit (master)
$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From 172.30.4.137:/home/git_ck/testgit
7055fb7..4befbeb master -> origin/master
Updating 7055fb7..4befbeb
Fast-forward
requests.py | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 requests.py
admin@admin-PC MINGW64 /e/news/git_ck/testgit (master)
$ ls -a
./ ../ .git/ README.nd requests.py test.txt