在虚拟机上搭建自己的 git 服务器并创建 git 仓库

参考自: https://blog.csdn.net/TomorrowAndTuture/article/details/108611882

  • git daemon 好似没有用

新建,初始化空目录仓库

[root@ /home/PythonProject]$ mkdir git
[root@ /home/PythonProject]$ ls
CRMProject  git  home  odoo10
[root@ /home/PythonProject]$ cd git 
[root@ /home/PythonProject/git]$ mkdir -p test-repo.git  && cd $_   # 新建并进入该目录

[root@ /home/PythonProject/git/test-repo.git]$ git --bare init        # 初始化空目录仓库
初始化空的 Git 版本库于 /home/PythonProject/git/test-repo.git/

[root@ /home/PythonProject/git/test-repo.git]$ ll -a
总用量 12
drwxrwsr-x 7 root root 119 3月  11 22:12 .
drwxr-xr-x 3 root root  27 3月  11 22:12 ..
drwxrwsr-x 2 root root   6 3月  11 22:12 branches
-rwxrwsr-x 1 root root  66 3月  11 22:12 config
-rwxrwsr-x 1 root root  73 3月  11 22:12 description
-rwxrwsr-x 1 root root  23 3月  11 22:12 HEAD
drwxrwsr-x 2 root root 242 3月  11 22:12 hooks
drwxrwsr-x 2 root root  21 3月  11 22:12 info
drwxrwsr-x 4 root root  30 3月  11 22:12 objects
drwxrwsr-x 4 root root  31 3月  11 22:12 refs

修改仓库的mod权限

[root@master git]# chmod 775 test-repo.git/ -R

设置默认新建的文件和文件夹同属于其父目录的用户组

chmod g+s test-repo.git -R
set -m g:root:rwx test-repo.git

开启git daemon 服务

git daemon --verbose --export-all --base-path=/root/git/test-repo.git/
Ready to rumble

本地机客户端 创建目录并初始化成仓库

Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ git remote rm  origin       # 删除远程的origin

Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true

Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ git remote add origin ssh://192.168.80.133/home/PythonProject/git/test-repo.git #  重新添加远程

Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ vim .git/config      # 编辑配置文件,把远程添加上用户名root@,上传是输入的密码是centos里该root账户的密码

Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ git remote -v    # 添加了root
origin  ssh://root@192.168.80.133/home/PythonProject/git/test-repo.git (fetch)
origin  ssh://root@192.168.80.133/home/PythonProject/git/test-repo.git (push)

Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ git push -u  origin master # git push 带 -u时,下次就不必带origin master了
ssh: connect to host 192.168.80.133 port 22: Connection timed out  #  确认ip是否正确,和端口是否打开或者防火墙是否关闭
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

ip错了, 关闭防火墙,重新git push

Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ git push -u  origin master
The authenticity of host '192.168.80.139 (192.168.80.139)' can't be established.
ECDSA key fingerprint is SHA256:8y0sVVOgFFaDT+RcL+CFVSTXbg7Ku6jTe6Ig7jEb48U.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.80.139' (ECDSA) to the list of known hosts.
root@192.168.80.139's password:            # 此处要输入该虚拟机root账户的密码
Counting objects: 137, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (132/132), done.
Writing objects: 100% (137/137), 1.46 MiB | 0 bytes/s, done.
Total 137 (delta 24), reused 0 (delta 0)
To ssh://root@192.168.80.139/home/PythonProject/git/test-repo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

上传之后,在该仓库下服务器上找不到该luffy_permission

[root@ /home/PythonProject/git/test-repo.git]$ find / -name luffy_permission
/home/ramelon/luffy_permission
/home/ramelon/luffy_permission/luffy_permission
/home/PythonProject/CRMProject/luffy_permission

在本地重新上传a2021311.txt文件

Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ vim a2021311.txt

Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ git add a2021311.txt
warning: LF will be replaced by CRLF in a2021311.txt.
The file will have its original line endings in your working directory.

Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   a2021311.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   db.sqlite3
        modified:   luffy_permission/settings.py
        modified:   rbac/admin.py
        modified:   rbac/models.py


Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ git commit -m '添加a2021311.txt'
[master 97a8c40] 添加a2021311.txt
warning: LF will be replaced by CRLF in a2021311.txt.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
 create mode 100644 a2021311.txt

Administrator@cpo-it001 MINGW64 /y/RBAC_LuffyPermission项目/RBAC组件/true/luffy_permission (master)
$ git push
root:ty@192.168.80.139's password:
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 292 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To ssh://root:ty@192.168.80.139/home/PythonProject/git/test-repo.git
   8240d2b..97a8c40  master -> master

在服务器上搜索还是没找到

[root@ /home/PythonProject/git/test-repo.git]$ find / -name a2021311.txt
[root@ /home/PythonProject/git/test-repo.git]$ find / -name a2021311.txt
[root@ /home/PythonProject/git/test-repo.git]$ find / -name a2021311.txt

但是重新在本地换一个目录下git clone 就可以了

Administrator@cpo-it001 MINGW64 /e/win10
$ git clone ssh://root:ty@192.168.80.139/home/PythonProject/git/test-repo.git
Cloning into 'test-repo'...
root:ty@192.168.80.139's password:
remote: Counting objects: 140, done.
remote: Compressing objects: 100% (110/110), done.
remote: Total 140 (delta 25), reused 136 (delta 24)
Receiving objects: 100% (140/140), 1.46 MiB | 0 bytes/s, done.
Resolving deltas: 100% (25/25), done.
Checking connectivity... done.

Administrator@cpo-it001 MINGW64 /e/win10
$ ll
total 4
drwxr-xr-x 1 Administrator 197121 0 三月 12 09:12 test-repo/

Administrator@cpo-it001 MINGW64 /e/win10
$ ll -a
total 12
drwxr-xr-x 1 Administrator 197121 0 三月 12 09:12 ./
drwxr-xr-x 1 Administrator 197121 0 三月 11 16:44 ../
drwxr-xr-x 1 Administrator 197121 0 三月 12 09:12 test-repo/

Administrator@cpo-it001 MINGW64 /e/win10
$ vim a.txt

Administrator@cpo-it001 MINGW64 /e/win10
$ git status
fatal: Not a git repository (or any of the parent directories): .git

Administrator@cpo-it001 MINGW64 /e/win10
$ cat a.txt
你好啊

Administrator@cpo-it001 MINGW64 /e/win10
$ git add .
fatal: Not a git repository (or any of the parent directories): .git

Administrator@cpo-it001 MINGW64 /e/win10
$ cd

Administrator@cpo-it001 MINGW64 ~
$ cd -
/e/win10

Administrator@cpo-it001 MINGW64 /e/win10
$ cd test-repo/

Administrator@cpo-it001 MINGW64 /e/win10/test-repo (master)
$ ll
total 80
-rw-r--r-- 1 Administrator 197121    14 三月 12 09:12 a2021311.txt
-rw-r--r-- 1 Administrator 197121 62464 三月 12 09:12 db.sqlite3
drwxr-xr-x 1 Administrator 197121     0 三月 12 09:12 luffy_permission/
-rwxr-xr-x 1 Administrator 197121   836 三月 12 09:12 manage.py*
drwxr-xr-x 1 Administrator 197121     0 三月 12 09:12 rbac/
-rw-r--r-- 1 Administrator 197121   147 三月 12 09:12 readme.txt
-rw-r--r-- 1 Administrator 197121    27 三月 12 09:12 requirements.txt
drwxr-xr-x 1 Administrator 197121     0 三月 12 09:12 web/
-rwxr-xr-x 1 Administrator 197121   117 三月 12 09:12 正则匹配.py*

细心的你可能会发现,服务端目录结构一直是这样的:

test-repo:[root@ /home/PythonProject/git/test-repo.git]$ 
[root@ /home/PythonProject/git/test-repo.git]$ ll
总用量 12
drwxrwsr-x  2 root root   6 3月  11 22:12 branches
-rwxrwsr-x  1 root root  66 3月  11 22:12 config
-rwxrwsr-x  1 root root  73 3月  11 22:12 description
-rwxrwsr-x  1 root root  23 3月  11 22:12 HEAD
drwxrwsr-x  2 root root 242 3月  11 22:12 hooks
drwxrwsr-x  2 root root  21 3月  11 22:12 info
drwxrwsr-x 10 root root  90 3月  12 10:17 objects
drwxrwsr-x  4 root root  31 3月  11 22:12 refs
[root@ /home/PythonProject/git/test-repo.git]$ cd ..
[root@ /home/PythonProject/git]$ ll 
总用量 0
drwxrwsr-x 7 root root 119 3月  11 22:12 test-repo.git
[root@ /home/PythonProject/git]$ 

但是竟然没有找到你 push 上去的 text.txt 文件,这是因为我们初始化仓库的时候用的命令 ”git init --bare” 初始化一个裸仓库,至于裸仓库和一般仓库的区别,请参考 https://blog.csdn.net/qq_42694242/article/details/82695162。

git 本地库和裸库的区别: https://blog.csdn.net/qq_42694242/article/details/82695162
在使用Git初始化版本库的时候,使用”git init”命令和使用”git init --bare”命令有什么区别呢?
用”git init”初始化的版本库(暂且称之为working repository)将会生成2类文件:“.git“版本库目录(记录版本历史)和实际项目文件的拷贝。你可以把这类版本库叫做“工作目录”。工作目录是一个包含有版本历史目录“.git”和源文件的目录。你可以在工作目录修改你的源文件并使用”git add”和”git commit”命令进行版本管理。
用“git init –bare”初始化的版本库(暂且称之为bare repository)仅包含”.git”目录(记录版本历史),不含项目源文件拷贝。如果你进入版本目录,你会发现仅有”.git”目录,没有其他文件。版本库仅包含记录着版本历史的文件。

什么情况下使用“git init”和”git init --bare”呢?
working repository适合于实际编辑生产过程中,在工作目录下,你将会进行实际的编码、文件管理操作和保存项目在本地工作。如果你开始创建一个项目将包含有源代码和和版本跟踪记录的时候你可以使用”git init”.或者,如果你克隆”git clone”一个已经存在的版本库的时候,你也可以得到一个working repository,它也将包含”.git”目录和源文件的拷贝。
bare repository主要是用作分享版本库。开发者使用bare repository可以向其他人分享存储在本地的版本库,以便于实时分享代码更新和团队协作 。通过使用”git push”命令,你可以将你的本地更新提交至“中心版本库”(其他开发者可访问的中心库)。其他开发者可以使用“git pull”命令者接受你提交的版本更新。如果你正在一个多人协作的项目团队或者同一个项目需要在不同电脑上面完成的时候,bare repository可以满足你的分布式开发需求。
总结:“工作目录”是通过使用“git init“或“git clone”创建的本地项目拷贝。我们可以在工作目录下面修改和测试代码。通过测试后我们可以使用“git add“和”git commit“命令本地提交修改,然后使用“git push”命令向远程 bare repository库提交更新,通常bare repository指定其他服务器,其他开发者将可以及时看到你的更新。当我们想去更新本地工作目录的时候,我们可以使用“git pull”命令去接受其他开发者提交的更新。

posted @ 2021-03-12 10:25  ty1539  阅读(429)  评论(0编辑  收藏  举报