git init与git init --bare

来源:简书 分类: 文章浏览史 发布时间:2023-03-24 11:55:00 最后更新:2023-03-24 浏览:167
转载声明:
本文为摘录自“简书”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2023-03-24 11:55:00

今天在创建git仓库时,想到了加不加--bare参数的问题,之前印象中知道是有区别的,具体区别在哪一时还真说不清了,这篇文章就总结一下。

差分对比

首先,这两个命令都是初始化一个空的git仓库,分别用这两个命令创建两个示例分析一下:

$ git init repo
$ tree -a repo
repo
└── .git
    ├── branches
    ├── config
    ├── description
    ├── HEAD
    ├── hooks
    │   ├── applypatch-msg.sample
    │   ├── commit-msg.sample
    │   ├── fsmonitor-watchman.sample
    │   ├── post-update.sample
    │   ├── pre-applypatch.sample
    │   ├── pre-commit.sample
    │   ├── prepare-commit-msg.sample
    │   ├── pre-push.sample
    │   ├── pre-rebase.sample
    │   ├── pre-receive.sample
    │   └── update.sample
    ├── info
    │   └── exclude
    ├── objects
    │   ├── info
    │   └── pack
    └── refs
        ├── heads
        └── tags

10 directories, 15 files

$ git init --bare repo.git
$ tree -a repo.git 
repo.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
├── readme
└── refs
    ├── heads
    └── tags

9 directories, 16 files

可以看到,初始化后的目录结构是不一样的,加了bare参数后不会在根目录创建.git文件夹,而把.git下面的文件直接拿到根目录里来了。再来看一下配置上有什么不同:

$ cd repo; git config --list | grep core
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true

$ cd repo.git; git config --list | grep core
core.repositoryformatversion=0
core.filemode=true
core.bare=true

可以看到有两项配置不一样,从字面意思也基本能猜到代表什么意思。

功能差异

加bare参数创建的仓库(repo.git)一般叫“裸仓库”,里面没有工作区的概念,即你不能直接在这样的仓库里进行正常的git命令操作;反之,不加bare参数创建的仓库(repo)就可以直接在上面进行git操作。如果是这样的话,那“裸仓库”还有什么用呢?我们先来看两个示例,分别clone上面创建的两个仓库,然后修改、提交并push到远程仓库:

$ git clone repo c_1

$ cd c_1; touch test.txt; git add -A; g commit -m "test commit"
$ git push origin master
对象计数中: 3, 完成.
写入对象中: 100% (3/3), 205 bytes | 205.00 KiB/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: 默认禁止更新非纯仓库的当前分支,因为您推送的内容将导致索引和工作区
remote: 不一致,并且将需要执行 'git reset --hard' 将工作区匹配到 HEAD。
remote: 
remote: 您可以在远程仓库中设置 'receive.denyCurrentBranch' 配置变量为
remote: 'ignore' 或 'warn' 以允许推送到当前分支。然而不推荐这么做,除非您
remote: 用某种方式将其工作区更新至您推送的状态。
remote: 
remote: 若要屏蔽此信息且保持默认行为,设置 'receive.denyCurrentBranch'
remote: 配置变量为 'refuse'。
To /home/evan/ARM/repo
 ! [remote rejected] master -> master (branch is currently checked out)
error: 无法推送一些引用到 '/home/evan/ARM/repo'

可以发现,并不能推送修改到“非裸仓库”,提示里也给出了说明。再来试试裸仓库的情况:

$ git clone repo.git c_2
$ cd c_2; touch test.txt; git add -A; g commit -m "test commit"
$ git push origin master
对象计数中: 3, 完成.
写入对象中: 100% (3/3), 204 bytes | 204.00 KiB/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
To /home/evan/ARM/repo.git
 * [new branch]      master -> master

推送修改到“裸仓库”就一切正常,这也正是裸仓库存在的意义, 裸仓库一般情况下仅作为远程仓库使用。个人理解,这样主要是为了维护远程仓库的“干净”,考虑这个场景,如果有人在远程仓库做了git操作,而你往上提交推送的时候必然会产生很多不必要的冲突。而将远程仓库赋予bare属性,大家沿着同一个准线往上提交推送,管理起来就方便多了。所以,我们在GitHub等这样的网站上看到的项目都是bare仓库。

总结

使用git init --bare “repo”可以创建一个裸仓库,并且这个仓库是可以被正常clone和push的,裸仓库不包含工作区,所以不能在裸仓库上直接提交变更。

php技术微信