git仓库类型介绍
Posted 乌龟漫步
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了git仓库类型介绍相关的知识,希望对你有一定的参考价值。
git仓库类型
git仓库有以下两种方式:
- 工作目录下的
.git
目录 - 目录名为
<project>.git
的裸仓库,通常用于通过push和fetch来与其他人交换历史。- 一个人通过push将修改提交到裸仓库
- 另一个人通过fetch将裸仓库的修改同步到本地工作目录
裸仓库实践
✔ /tmp/test.git $ git init --bare . # 在当前目录初始化git仓库并指定为裸仓库
Initialized empty Git repository in /private/tmp/test.git/
✔ /tmp/test.git $ ls
branches config description HEAD hooks info objects refs
✔ /tmp/test.git $
程序员1将test.git克隆到/tmp/test_clone并创建了文件test.txt,然后将更改推送到了裸仓库test.git。
✔ /tmp $ git clone file:///tmp/test.git test_clone
Cloning into \'test_clone\'...
warning: You appear to have cloned an empty repository.
✔ /tmp $ cd test_clone/
✔ /tmp/test_clone [master L|✔] $ ls
✔ /tmp/test_clone [master L|✔] $ echo test > test.txt
✘-1 /tmp/test_clone [master L|…1] $ git add .
✔ /tmp/test_clone [master L|●1] $ git commit -am \'first commit\'
[master (root-commit) 59d3a83] first commit
1 file changed, 1 insertion(+)
create mode 100644 test.txt
✔ /tmp/test_clone [master|✔] $
✔ /tmp/test_clone [master|✔] $ git remote -v
origin file:///tmp/test.git (fetch)
origin file:///tmp/test.git (push)
✔ /tmp/test_clone [master|✔] $ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 212 bytes | 212.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To file:///tmp/test.git
* [new branch] master -> master
✔ /tmp/test_clone [master|✔] $
程序员2将test.git克隆到/tmp/test_clone2并创建了文件test2.txt,此时可以看到程序员1的提交。程序员2将更改推送到裸仓库。
✔ /tmp $ git clone file:///tmp/test.git test_clone2
Cloning into \'test_clone2\'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
✔ /tmp $ cd test_clone2/
✔ /tmp/test_clone2 [master|✔] $ git log
commit 59d3a83b18c7fd01d766b7437f993911318a5f74 (HEAD -> master, origin/master, origin/HEAD)
Author: p1 <xxx@qq.com>
Date: Thu Oct 14 18:01:14 2021 +0800
first commit
✔ /tmp/test_clone2 [master|✔] $ echo \'test2\' > test2.txt
✔ /tmp/test_clone2 [master|…1] $ git add test2.txt
✔ /tmp/test_clone2 [master|●1] $ git commit -am \'Add file test2.txt\'
[master 4873552] Add file test2.txt
1 file changed, 1 insertion(+)
create mode 100644 test2.txt
✔ /tmp/test_clone2 [master ↑·1|✔] $ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 277 bytes | 277.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To file:///tmp/test.git
59d3a83..4873552 master -> master
✔ /tmp/test_clone2 [master|✔] $
此时,程序员1可以将程序员2的提交同步到本地,由此完成了一次协作。
✔ /tmp/test_clone [master ↓·1|✔] $ git pull
Updating 59d3a83..4873552
Fast-forward
test2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 test2.txt
✔ /tmp/test_clone [master|✔] $ git log
commit 48735524a8c488ef922a3bb106274e5782ac8372 (HEAD -> master, origin/master)
Author: p1 <yyy@qq.com>
Date: Thu Oct 14 18:05:55 2021 +0800
Add file test2.txt
commit 59d3a83b18c7fd01d766b7437f993911318a5f74
Author: p1 <xxx@qq.com>
Date: Thu Oct 14 18:01:14 2021 +0800
first commit
✔ /tmp/test_clone [master|✔] $
以上是关于git仓库类型介绍的主要内容,如果未能解决你的问题,请参考以下文章