git仓库服务搭建

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

posted @ 2020-12-08 16:26  朝朝哥  阅读(173)  评论(0编辑  收藏  举报