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

主要的区别似乎是:

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


当前回答

简而言之:用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克隆——裸到一个新的位置(见下文)。

其他回答

我只是想推到网络路径上的存储库,但git不会让我这样做,除非该存储库被标记为裸。 我只需要改变它的配置:

git config --bool core.bare true

除非你想保持文件整洁,否则没必要乱动。

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

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

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

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

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

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

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