如何将普通的 Git 存储库转换为裸存储库?
如何将“普通”Git 存储库转换为裸存储库?
主要区别似乎是:
- 包含所有相关数据和构成工作副本的所有其他文件
在裸 Git 存储库中,没有工作副本,并且文件夹(我们称之为
repo.git
)包含
How can I convert a 'normal' Git repository to a bare one?
The main difference seems to be:
in the normal Git repository, you have a
.git
folder inside the repository containing all relevant data and all other files making up your working copyin a bare Git repository, there is no working copy and the folder (let's call it
repo.git
) contains the actual repository data
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
简而言之:将
repo
的内容替换为repo/.git
的内容,然后告诉存储库它现在是一个裸存储库。为此,请执行以下命令:
请注意,这与将
git clone --bare /path/to/repo
执行到新位置(如此处所述)。考虑这种情况
如果这是您的意图,那很好。如果您需要远程/源的镜像,这不是方法。
In short: replace the contents of
repo
with the contents ofrepo/.git
, then tell the repository that it is now a bare repository.To do this, execute the following commands:
Note that this is different from doing a
git clone --bare /path/to/repo
to a new location (as described here).Consider this scenario
If that's your intention, that's fine. If you needed a mirror of the remote/origin, this is not the way.
你的方法看起来可行;裸存储库的文件结构就是 .git 目录中的内容。但我不知道是否有任何文件实际上被更改,所以如果失败,您可以这样做
您可能需要在不同的目录中执行它以避免名称冲突,然后您可以将其移回到你想要的地方。您可能需要更改配置文件以指向您的原始存储库所在的位置。
Your method looks like it would work; the file structure of a bare repository is just what is inside the .git directory. But I don't know if any of the files are actually changed, so if that fails, you can just do
You'll probably need to do it in a different directory to avoid a name conflict, and then you can just move it back to where you want. And you may need to change the config file to point to wherever your origin repo is.
我认为以下链接会有所帮助
GitFaq:如何使现有非裸存储库裸?
I think the following link would be helpful
GitFaq: How do I make existing non-bare repository bare?
除非您特别想要或需要在文件系统上进行调整,否则创建非裸存储库的裸版本确实非常简单(在其他几篇文章中提到过)。它是 git 核心功能的一部分:
git clone --bare existing_repo_path bare_repo_path
Unless you specifically want or need to twiddle bits on the filesystem, it really is dead simple to create a bare version of a non-bare repository (mentioned in several other posts here). It’s part of git’s core functionality:
git clone --bare existing_repo_path bare_repo_path
另请考虑使用
文档:
Please also consider to use
From the documentation:
我只是想推送到网络路径上的存储库,但 git 不会让我这样做,除非该存储库被标记为裸。
我所需要的只是改变它的配置:
不需要摆弄文件,除非你想保持它干净。
I just wanted to push to a repository on a network path but git would not let me do that unless that repository was marked as bare.
All I needed was to change its config:
No need to fiddle with the files unless you want to keep it clean.
我已经阅读了答案,并且已经这样做了:
这会将
repos/.git
的内容保留为裸露的repos.git
i've read the answers and i have done this:
this will leave the contents of
repos/.git
as the barerepos.git
这是我认为最安全和最简单的。这里没有什么是上面没有提到的。我只是想看到一个显示安全分步过程的答案。您可以从要裸露的存储库 (repo) 开始一个文件夹。我采用了上面暗示的约定,即裸存储库文件夹具有 .git 扩展名。
Here's what I think is safest and simplest. There is nothing here not stated above. I just want to see an answer that shows a safe step-by-step procedure. You start one folder up from the repository (repo) you want to make bare. I've adopted the convention implied above that bare repository folders have a .git extension.
只需阅读
Pro Git Book: 4.2 Git on the Server - Getting Git on a Server
归结为 然后
将 my_project.git 到服务器
主要是,答案是什么 #42试图指出。确实,人们可以重新发明轮子;-)
Simply read
Pro Git Book: 4.2 Git on the Server - Getting Git on a Server
which boild down to
Then put my_project.git to the server
Which mainly is, what answer #42 tried to point out. Shurely one could reinvent the wheel ;-)
这是一个小 BASH 函数,您可以将其添加到基于 UNIX 的系统上的 .bashrc 或 .profile 中。添加后,shell 将重新启动,或者通过调用
source ~/.profile
或source ~/.bashrc
重新加载文件。一旦在包含 .git 目录的目录中调用,它将进行适当的更改以转换存储库。如果调用时不存在 .git 目录,则会出现 FAILURE 消息,并且不会发生文件系统更改。
Here is a little BASH function you can add to your .bashrc or .profile on a UNIX based system. Once added and the shell is either restarted or the file is reloaded via a call to
source ~/.profile
orsource ~/.bashrc
.Once called within a directory containing a .git directory, it will make the appropriate changes to convert the repository. If there is no .git directory present when called, a FAILURE message will appear and no file system changes will happen.
那些说删除文件并移动 .git 目录的方法并不干净,并且没有使用“git”方法来做一些应该很简单的事情。这是我发现的将普通存储库转换为裸存储库的最干净的方法。
首先将 /path/to/normal/repo 克隆到名为 repo.git 的裸存储库中,
然后删除指向 /path/to/normal/repo 的源,
最后您可以删除原始存储库。此时您可以将 repo.git 重命名为 repo,但表示 git 存储库的标准约定是 some.git,所以我个人会保留这种方式。
完成所有这些后,您可以克隆新的裸存储库(这实际上创建了一个普通存储库,也是您将其从裸存储转换为普通存储库的方式)
当然,如果您有其他上游,您会想要记下它们,并更新您的裸存储库以包含它。但同样,这一切都可以通过 git 命令完成。请记住,手册页是您的朋友。
The methods that say to remove files and muck about with moving the .git directory are not clean and not using the "git" method of doing something that's should be simple. This is the cleanest method I have found to convert a normal repo into a bare repo.
First clone /path/to/normal/repo into a bare repo called repo.git
Next remove the origin that points to /path/to/normal/repo
Finally you can remove your original repo. You could rename repo.git to repo at that point, but the standard convention to signify a git repository is something.git, so I'd personally leave it that way.
Once you've done all that, you can clone your new bare repo (which in effect creates a normal repo, and is also how you would convert it from bare to normal)
Of course if you have other upstreams, you'll want to make a note of them, and update your bare repo to include it. But again, it can all be done with the git command. Remember the man pages are your friend.
如果您的存储库只有很少的本地签出分支 /refs/heads/* 和很少的远程分支分支 remotes/origin/* 并且如果您想将其转换为 BARE 存储库,所有分支都在 /refs/heads/* 中,
您可以执行以下操作来保存历史记录。
In case you have a repository with few local checkedout branches /refs/heads/* and few remote branch branches remotes/origin/* AND if you want to convert this into a BARE repository with all branches in /refs/heads/*
you can do the following to save the history.
以下是来自 gitglossary裸存储库的定义>:
我来到这里是因为我正在使用“本地存储库”,并且希望能够做任何我想做的事情,就好像它是远程存储库一样。我只是玩玩,试图了解 git。我假设这就是任何想阅读此答案的人的情况。
我很想得到专家的意见或一些具体的反例,但是似乎(在翻阅我找到的一些 git 源代码之后)只需转到文件
.git/config
并设置 <将strong>core属性bare设置为true,git将允许你远程对存储库做任何你想做的事情。即以下几行应该存在于.git/config
中:(这大致是命令
git config --bool core.bare true
的作用,这可能是推荐的处理更复杂的情况)我对这种说法的理由是,在 git 源代码中,似乎有两种不同的方法来测试存储库是否是裸露的。一种是检查全局变量 is_bare_repository_cfg。这是在执行的某些设置阶段设置的,并反映在 .git/config 文件中找到的值。另一个是函数
is_bare_repository()
。以下是该函数的定义:我没有时间也没有专业知识来绝对自信地说出这一点,但据我所知,您是否将
bare
属性设置为true< /code> 在
.git/config
中,这应该始终返回1
。函数的其余部分可能是针对以下情况:稍后我会尝试一下,但这似乎表明设置 core.bare = true 相当于从配置文件中删除 core.bare 并正确设置目录。
无论如何,设置 core.bare = true 肯定会让你推动它,但我不确定项目文件的存在是否会导致其他一些操作出错。这很有趣,我认为推送到存储库并查看本地发生的情况(即运行 git status 并理解结果)很有启发性。
Here is the definition of a bare repository from gitglossary:
I arrived here because I was playing around with a "local repository" and wanted to be able to do whatever I wanted as if it were a remote repository. I was just playing around, trying to learn about git. I'll assume that this is the situation for whoever wants to read this answer.
I would love for an expert opinion or some specific counter-examples, however it seems that (after rummaging through some git source code that I found) simply going to the file
.git/config
and setting the core attribute bare to true, git will let you do whatever you want to do to the repository remotely. I.e. the following lines should exist in.git/config
:(This is roughly what the command
git config --bool core.bare true
will do, which is probably recommended to deal with more complicated situations)My justification for this claim is that, in the git source code, there seems to be two different ways of testing if a repo is bare or not. One is by checking a global variable
is_bare_repository_cfg
. This is set during some setup phase of execution, and reflects the value found in the.git/config
file. The other is a functionis_bare_repository()
. Here is the definition of this function:I've not the time nor expertise to say this with absolute confidence, but as far as I could tell if you have the
bare
attribute set totrue
in.git/config
, this should always return1
. The rest of the function probably is for the following situation:I'll experiment with it when I can later, but this would seem to indicate that setting core.bare = true is equivalent to removeing core.bare from the config file and setting up the directories properly.
At any rate, setting core.bare = true certainly will let you push to it, but I'm not sure if the presence of project files will cause some other operations to go awry. It's interesting and I suppose instructive to push to the repository and see what happened locally (i.e. run
git status
and make sense of the results).我使用以下脚本读取包含所有 SVN 存储库列表的文本文件并将其转换为 GIT,然后使用 git clone --bare 转换为裸 git 存储库
list.txt 具有格式
和 users.txt有格式
www-data 是 Apache Web 服务器用户,需要权限才能通过 HTTP 推送更改
I used the following script to read a text file that has a list of all my SVN repos and convert them to GIT, and later use git clone --bare to convert to a bare git repo
list.txt has the format
and users.txt has the format
www-data is the Apache web server user, permission is needed to push changes over HTTP
大多数解决方案只是删除主工作树
但这也会删除
此处未跟踪的文件,我们将未跟踪的文件移动到单独的目录中,
最复杂的问题来自于
rsync 在同一文件系统上移动文件
most solutions simply delete the main worktree
but that also deletes untracked files
here, we move untracked files to a separate directory
most complexity comes from the workaround for
rsync move files on the same filesystem
首先,
备份
您现有的存储库:其次,运行以下命令:
First,
backup
your existing repo:Second, run the following:
添加了 2 个:
写完答案后意识到,如果后面跟着
git add *
,接受的答案可能会在我的电脑上产生相同的结果。文件从我的工作文件夹中消失(只有
. git
左),它又漂亮又紧凑:然后如果愿意的话转换为裸:
这样保留包括远程链接的配置:
添加:
在评论中提到它不会删除“任何 git-ignored 文件”,在这种情况下,需要手动另外删除它们(或者将存储库本身,即
.git
子文件夹移动到其他地方)。备注:
在
core.bare true
之前,某些操作会导致错误:此后
some_new_branch_name
尚未在gitbranch
的输出中列出。为了进一步测试,我做了git checkout master
,我得到了文件,并且git分支
的输出中再次没有some_new_branch_name
,所以我认为新的除非在那里完成了一些工作(和/或执行了提交),否则孤儿分支不会被添加到存储库中。
Added 2:
After writing the answer realized that accepted answer would likely result in the same result on my PC if followed by
git add *
.I got files disappear from my working folder (only
.git
left), it is again nice and compact by:Then convert to bare if one wants to:
That way config including remote link is kept:
Added:
In the comments it is mentioned that it would not remove "any git-ignored files", it such case they need to be additionally deleted manually (or repository itself that is
.git
subfolder be moved elsewhere).Notes:
Before
core.bare true
some actions resulted in errors:some_new_branch_name
have not been listed in output ofgit branch
after that. To test further I didgit checkout master
I got files back and again nosome_new_branch_name
in output ofgit branch
, so I think neworphan
branch would not be added to repository unless some work is done there (and / or commits performed).Oneliner 用于执行上述所有操作:(
如果出现问题并且您没有备份,请不要怪我 :P)
Oneliner for doing all of the above operations:
(don't blame me if something blows up and you didn't have backups :P)
哇,令人惊讶的是有这么多人参与其中,尤其是考虑到似乎没有人停下来问为什么这个人正在做他正在做的事情。
裸露和非裸露 git 存储库之间的唯一区别是非裸露版本有一个工作副本。您需要一个裸存储库的主要原因是,如果您想将其提供给第三方,您实际上无法直接对其进行操作,因此在某些时候您将不得不克隆它,此时您将需要克隆它。回到常规工作副本版本。
话虽如此,要转换为裸存储库,您所要做的就是确保没有待处理的提交,然后:
好了,裸存储库。
Wow, it's simply amazing how many people chimed in on this, especially considering it doesn't seem that not a single on stopped to ask why this person is doing what he's doing.
The ONLY difference between a bare and non-bare git repository is that the non-bare version has a working copy. The main reason you would need a bare repo is if you wanted to make it available to a third party, you can't actually work on it directly so at some point you're going to have to clone it at which point you're right back to a regular working copy version.
That being said, to convert to a bare repo all you have to do is make sure you have no commits pending and then just :
There ya go, bare repo.