我如何将一个“正常”的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

其他回答

下面是一个小BASH函数,您可以将它添加到基于UNIX的系统上的.bashrc或.profile中。一旦添加,shell要么重新启动,要么通过调用source ~/重新加载文件。配置文件或源代码~/.bashrc。

function gitToBare() {
  if [ -d ".git" ]; then
    DIR="`pwd`"
    mv .git ..
    rm -fr *
    mv ../.git .
    mv .git/* .
    rmdir .git

    git config --bool core.bare true
    cd ..
    mv "${DIR}" "${DIR}.git"

    printf "[\x1b[32mSUCCESS\x1b[0m] Git repository converted to "
    printf "bare and renamed to\n  ${DIR}.git\n"
    cd "${DIR}.git"
  else
    printf "[\x1b[31mFAILURE\x1b[0m] Cannot find a .git directory\n"
  fi
}

一旦在包含.git目录的目录中调用,它将进行适当的更改以转换存储库。如果调用时不存在.git目录,则会出现FAILURE消息,并且不会发生文件系统更改。

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

git clone --bare /path/to/repo

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

简而言之:用repo/的内容替换repo的内容。Git,然后告诉存储库它现在是一个裸存储库。

为此,执行以下命令:

cd repo
mv .git ../repo.git # renaming just for clarity
cd ..
rm -fr repo
cd repo.git
git config --bool core.bare true

注意,这不同于做一个git克隆——裸到一个新的位置(见下文)。

我已经看了答案,我是这么做的:

cd repos
mv .git repos.git
cd repos.git
git config --bool core.bare true # from another answer
cd ../
mv repos.git ../
cd ../
rm -rf repos/ # or delete using a file manager if you like

这将留下repos/。Git作为裸rest . Git

简单的读

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号试图指出的答案。当然,人们可以重新发明轮子;-)