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

主要的区别似乎是:

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


当前回答

首先,备份你现有的回购:

(a)  mkdir backup

(b)  cd backup

(c)  git clone non_bare_repo

其次,运行以下命令:

git clone --bare -l non_bare_repo new_bare_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,所以我认为新的孤儿分支不会被添加到存储库中,除非那里完成了一些工作(和/或执行了提交)。

你的方法看起来可行;裸存储库的文件结构就是.git目录中的内容。但我不知道是否有文件被修改过,如果失败了,你可以这样做

git clone --bare /path/to/repo

您可能需要在不同的目录中执行此操作以避免名称冲突,然后您可以将其移回您想要的位置。您可能需要更改配置文件,以指向您的原始回购所在的位置。

除非您特别想要或需要在文件系统上摆弄比特,否则创建一个非裸库的裸版本真的非常简单(在这里的其他几篇文章中提到过)。它是git核心功能的一部分:

Git clone——bare existing_repo_path bare_repo_path

简单的读

Pro Git Book: 4.2 Git on the Server - get Git on a Server

归结起来是什么

$ git clone --bare my_project my_project.git
Cloning into bare repository 'my_project.git'...
done.

然后输入my_project。Git到服务器

主要是42号试图指出的答案。当然,人们可以重新发明轮子;-)

首先,备份你现有的回购:

(a)  mkdir backup

(b)  cd backup

(c)  git clone non_bare_repo

其次,运行以下命令:

git clone --bare -l non_bare_repo new_bare_repo