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

主要的区别似乎是:

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


当前回答

我使用下面的脚本读取一个文本文件,其中包含我所有的SVN repo,并将它们转换为GIT,然后使用GIT clone——bare转换为一个裸露的GIT repo

#!/bin/bash
file="list.txt"
while IFS= read -r repo_name
do
 printf '%s\n' "$repo_name"
 sudo git svn clone --shared --preserve-empty-dirs --authors-file=users.txt file:///programs/svn/$repo_name 
 sudo git clone --bare /programs/git/$repo_name $repo_name.git
 sudo chown -R www-data:www-data $repo_name.git
 sudo rm -rf $repo_name
done <"$file"

List.txt有这样的格式

repo1_name
repo2_name

users.txt有这个格式

(无作者)=罗杰斯王子<prince.rogers.nelson@payesley.park.org>

www-data是Apache web服务器用户,需要权限才能通过HTTP推送更改

其他回答

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

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

$ mv repo/.git repo.git
$ git --git-dir=repo.git config core.bare true
$ rm -rf 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克隆——裸到一个新的位置(见下文)。

下面是一个小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消息,并且不会发生文件系统更改。

我使用下面的脚本读取一个文本文件,其中包含我所有的SVN repo,并将它们转换为GIT,然后使用GIT clone——bare转换为一个裸露的GIT repo

#!/bin/bash
file="list.txt"
while IFS= read -r repo_name
do
 printf '%s\n' "$repo_name"
 sudo git svn clone --shared --preserve-empty-dirs --authors-file=users.txt file:///programs/svn/$repo_name 
 sudo git clone --bare /programs/git/$repo_name $repo_name.git
 sudo chown -R www-data:www-data $repo_name.git
 sudo rm -rf $repo_name
done <"$file"

List.txt有这样的格式

repo1_name
repo2_name

users.txt有这个格式

(无作者)=罗杰斯王子<prince.rogers.nelson@payesley.park.org>

www-data是Apache web服务器用户,需要权限才能通过HTTP推送更改

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

Git clone——bare existing_repo_path bare_repo_path