我如何将一个“正常”的Git存储库转换为一个裸库?

主要的区别似乎是:

在普通的Git存储库中,您在存储库中有一个.git文件夹,其中包含所有相关数据和构成工作副本的所有其他文件 在裸Git存储库中,没有工作副本,文件夹(让我们称之为repo.git)包含实际的存储库数据


当前回答

以下是我认为最安全、最简单的方法。这里没有没有上面说过的。我只是想看到一个答案,显示一个安全的一步一步的程序。您从想要使其为空的存储库(repo)中启动一个文件夹。我采用了上面暗示的约定,即裸存储库文件夹具有.git扩展名。

(1) Backup, just in case.
    (a) > mkdir backup
    (b) > cd backup
    (c) > git clone ../repo
(2) Make it bare, then move it
    (a) > cd ../repo
    (b) > git config --bool core.bare true
    (c) > mv .git ../repo.git
(3) Confirm the bare repository works (optional, since we have a backup)
    (a) > cd ..
    (b) > mkdir test
    (c) > cd test
    (d) > git clone ../repo.git
(4) Clean up
    (a) > rm -Rf repo
    (b) (optional) > rm -Rf backup/repo
    (c) (optional) > rm -Rf test/repo

其他回答

哇,居然有这么多人插话,真是令人惊讶,尤其是考虑到似乎没有一个人停下来问这个人为什么要做他正在做的事情。

裸git和非裸git仓库之间的唯一区别是,非裸版本有一个工作副本。你需要裸回购的主要原因是,如果你想让第三方可以使用它,你实际上不能直接使用它,所以在某种程度上,你将不得不克隆它,此时你就会回到常规的工作副本版本。

也就是说,要转换为裸回购,你所要做的就是确保你没有提交挂起,然后:

rm -R * && mv .git/* . && rm -R .git

这就对了,裸回购。

我认为下面的链接会有帮助

GitFaq:如何使现有的非裸库变得裸?

$ mv repo/.git repo.git
$ git --git-dir=repo.git config core.bare true
$ rm -rf repo

补充2: 写完答案后,我意识到接受的答案可能会在我的PC上导致相同的结果,如果后面跟着git add *。

我的文件从我的工作文件夹中消失了(只剩下。git了),它又好又紧凑:

git switch --orphan some_new_branch_name 

然后转换为裸,如果你想:

git config --bool core.bare true

这样,包括远程链接在内的配置将被保存:

$ git config --list
core.repositoryformatversion=0
core.filemode=true
core.bare=true
remote.origin.url=https://github.com/vmatare/thinkfan.git
remote.origin.fetch=+refs/*:refs/*
remote.origin.mirror=true

补充道: 在评论中提到,它不会删除“任何被git忽略的文件”,这种情况下,它们需要额外手动删除(或存储库本身,即.git子文件夹被移动到其他地方)。

注: 在核心。毫无疑问,一些行为会导致错误:

$ git fetch --all
Fetching origin
fatal: Refusing to fetch into current branch refs/heads/devel of non-bare repository
error: Could not fetch origin

Some_new_branch_name在git分支的输出中没有被列出。为了进一步测试,我做了git checkout master,我得到了文件,在git分支的输出中再次没有some_new_branch_name,所以我认为新的孤儿分支不会被添加到存储库中,除非那里完成了一些工作(和/或执行了提交)。

用于执行上述所有操作的onlineer:

for i in `ls -A .`; do if [ $i != ".git" ]; then rm -rf $i; fi; done; mv .git/* .; rm -rf .git; git config --bool core.bare true

(如果事情搞砸了,你又没有备份,可别怪我:P)

也请大家考虑使用

git clone --mirror path_to_source_repository path_to_bare_repository

从文档中可以看到:

设置源存储库的镜像。这意味着——裸露。与——bare相比,——mirror不仅将源的本地分支映射到目标的本地分支,它还映射所有的引用(包括远程跟踪分支,notes等),并设置一个refspec配置,以便所有这些引用都被目标库中的git远程更新覆盖。