“与”的区别是什么;git init“;及;git init--bare";?

“与”的区别是什么;git init“;及;git init--bare";?,git,version-control,Git,Version Control,gitinit和gitinit--bare之间有什么不同?我发现很多博客文章都要求Git服务器使用--bare 从一开始,它说: 创建一个裸存储库。如果未设置GIT_DIR环境,则将其设置为当前工作目录 但这究竟意味着什么呢?Git服务器设置是否需要--bare?默认Git存储库假定您将使用它作为工作目录。通常,当您在服务器上时,不需要有工作目录。只有存储库。在这种情况下,您应该使用--bare选项。简短回答 裸存储库是没有工作副本的git存储库,因此.git的内容是该目录的顶级内容 使用非裸存

gitinit
gitinit--bare
之间有什么不同?我发现很多博客文章都要求Git服务器使用
--bare

从一开始,它说:

创建一个裸存储库。如果未设置GIT_DIR环境,则将其设置为当前工作目录


但这究竟意味着什么呢?Git服务器设置是否需要
--bare

默认Git存储库假定您将使用它作为工作目录。通常,当您在服务器上时,不需要有工作目录。只有存储库。在这种情况下,您应该使用
--bare
选项。

简短回答 裸存储库是没有工作副本的git存储库,因此.git的内容是该目录的顶级内容

使用非裸存储库在本地工作,使用裸存储库作为中央服务器/中心与其他人共享您的更改。例如,当您在github.com上创建存储库时,它将被创建为裸存储库

因此,在您的计算机中:

git init
touch README
git add README
git commit -m "initial commit"
在服务器上:

cd /srv/git/project
git init --bare
然后在客户端上,推送:

git push username@server:/srv/git/project master
然后,您可以通过将键入内容添加为远程文件来保存键入内容

服务器端的存储库将通过pull和push获得提交,而不是通过编辑文件然后在服务器机器中提交它们,因此它是一个裸存储库

细节 您可以推送到一个不是裸存储库的存储库,git会发现那里有一个.git存储库,但由于大多数“集线器”存储库不需要工作副本,因此使用裸存储库是正常的,建议使用裸存储库,因为在这种存储库中使用工作副本没有任何意义

但是,如果推送到非裸存储库,则会使工作副本不一致,git会警告您:

remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
您可以跳过此警告。但建议的设置是:使用非裸存储库在本地工作,使用裸存储库作为中心或中央服务器进行推拉

如果您想直接与其他开发人员的工作副本共享工作,您可以从其他存储库中提取,而不是推送。

Non-Bare Git Repo 此变体创建了一个具有工作目录的存储库,因此您可以实际工作(
git clone
)。创建之后,您将看到该目录包含一个.git文件夹,其中包含历史记录和所有git管道。您在.git文件夹所在的级别工作

裸吉特回购 另一个变体创建一个没有工作目录的存储库(
git clone--bare
)。你没有一个可以工作的目录。目录中的所有内容现在都包含在上述情况下的.git文件夹中

为什么你会使用一个与另一个 不需要工作目录的git repos的需要是,您可以将分支推送到它,而它无法管理某人正在处理的内容。您仍然可以推送到一个非空的存储库,但您将被拒绝,因为您可能会在该工作目录中移动某人正在处理的分支

因此,在没有工作文件夹的项目中,您只能在git存储对象时看到它们。它们被压缩、序列化并存储在其内容的SHA1(散列)下。为了在裸存储库中获取对象,需要
git show
,然后指定要查看的对象的sha1。你不会看到像你的项目那样的结构

裸存储库通常是中心存储库,每个人都将其工作转移到中心存储库。没有必要操纵实际工作。这是一种在多人之间同步工作的方法。您将无法直接查看项目文件

如果您是唯一一个在项目中工作的人,或者您不想/不需要“逻辑中心”存储库,那么您可能不需要任何裸存储库。在这种情况下,人们更愿意从其他存储库中
git-pull
。这避免了git在推送到非裸存储库时遇到的异议


希望这有帮助

默认为非裸存储库。它是在运行
git init
时创建的,或者从服务器克隆(不带
bare
选项)时得到的

使用这样的存储库时,可以查看和编辑存储库中的所有文件。当您与存储库交互时(例如通过提交更改),Git将您的更改存储在名为
.Git
的隐藏目录中

当您有一个git服务器时,不需要有文件的工作副本。您所需要的只是存储在
.Git
中的Git数据。裸存储库就是
.git
目录,没有用于修改和提交文件的工作区


当您从服务器克隆时,Git在
.Git
目录中拥有创建工作副本所需的所有信息。

当我不久前读到这个问题时,一切都让我感到困惑。我刚开始使用git,有这些工作副本(在当时没有任何意义)。我将试着从这个家伙的角度来解释这一点,他刚开始使用git,对术语一无所知

一个很好的差异示例可以用以下方式描述:

--bare
只给了你一个存储空间(你不能在那里开发)。如果不使用
——裸
,它将使您能够在那里进行开发(并有一个存储位置)

git init
从当前目录创建git存储库。它在其中添加了.git文件夹,使您可以启动修订历史记录

git init--bare
也会创建一个存储库,但它没有工作目录。这意味着您无法在该存储库中编辑文件、提交更改和添加新文件

当<代码>--裸露时
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.