如何将普通的 Git 存储库转换为裸存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2199897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a normal Git repository to a bare one?
提问by Boldewyn
How can I convert a 'normal' Git repository to a bare one?
如何将“普通”Git 存储库转换为裸存储库?
The main difference seems to be:
主要区别似乎是:
in the normal Git repository, you have a
.git
folder inside the repository containing all relevant data and all other files making up your working copyin a bare Git repository, there is no working copy and the folder (let's call it
repo.git
) contains the actual repository data
在普通的 Git 存储库中,存储库中有一个
.git
文件夹,其中包含所有相关数据和构成工作副本的所有其他文件在裸 Git 存储库中,没有工作副本,文件夹(我们称之为
repo.git
)包含实际的存储库数据
回答by J?rg W Mittag
In short: replace the contents of repo
with the contents of repo/.git
, then tell the repository that it is now a bare repository.
简而言之:将 的内容替换为repo
的内容repo/.git
,然后告诉存储库它现在是一个裸存储库。
To do this, execute the following commands:
为此,请执行以下命令:
cd repo
mv .git ../repo.git # renaming just for clarity
cd ..
rm -fr repo
cd repo.git
git config --bool core.bare true
Note that this is different from doing a git clone --bare
to a new location (see below).
请注意,这git clone --bare
与对新位置执行 a 不同(见下文)。
回答by jonescb
Your method looks like it would work; the file structure of a bare repository is just what is inside the .git directory. But I don't know if any of the files are actually changed, so if that fails, you can just do
你的方法看起来可行;裸仓库的文件结构就是 .git 目录中的内容。但我不知道是否有任何文件被实际更改,所以如果失败,你可以这样做
git clone --bare /path/to/repo
You'll probably need to do it in a different directory to avoid a name conflict, and then you can just move it back to where you want. And you may need to change the config file to point to wherever your origin repo is.
您可能需要在不同的目录中执行此操作以避免名称冲突,然后您可以将其移回您想要的位置。并且您可能需要更改配置文件以指向您的原始存储库所在的位置。
回答by xhh
I think the following link would be helpful
我认为以下链接会有所帮助
GitFaq: How do I make existing non-bare repository bare?
$ mv repo/.git repo.git
$ git --git-dir=repo.git config core.bare true
$ rm -rf repo
回答by Chip Kaye
Unless you specifically want or need to twiddle bits on the filesystem, it really is dead simple to create a bare version of a non-bare repository (mentioned in several other posts here). It's part of git's core functionality:
除非您特别想要或需要在文件系统上修改位,否则创建非裸存储库的裸版本确实非常简单(在此处的其他几篇文章中提到)。它是 git 核心功能的一部分:
git clone --bare existing_repo_path bare_repo_path
git clone --bare existing_repo_path bare_repo_path
回答by Jacek Krawczyk
Please also consider to use
也请考虑使用
git clone --mirror path_to_source_repository
From the documentation:
从文档:
Set up a mirror of the source repository. This implies --bare. Compared to --bare, --mirror not only maps local branches of the source to local branches of the target, it maps all refs (including remote-tracking branches, notes etc.) and sets up a refspec configuration such that all these refs are overwritten by a git remote update in the target repository.
设置源存储库的镜像。这意味着--bare。与--bare 相比,--mirror 不仅将源的本地分支映射到目标的本地分支,它还映射所有引用(包括远程跟踪分支、注释等)并设置引用规范配置,以便所有这些引用被目标存储库中的 git 远程更新覆盖。
回答by Slion
I just wanted to push to a repository on a network path but git would not let me do that unless that repository was marked as bare. All I needed was to change its config:
我只是想推送到网络路径上的存储库,但除非该存储库被标记为空,否则 git 不会让我这样做。我所需要的只是改变它的配置:
git config --bool core.bare true
No need to fiddle with the files unless you want to keep it clean.
除非您想保持干净,否则无需摆弄文件。
回答by Dan D.
i've read the answers and i have done this:
我已经阅读了答案,我已经这样做了:
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
this will leave the contents of repos/.git
as the bare repos.git
这将使内容保持repos/.git
为裸露repos.git
回答by sdesciencelover
Here's what I think is safest and simplest. There is nothing here not stated above. I just want to see an answer that shows a safe step-by-step procedure. You start one folder up from the repository (repo) you want to make bare. I've adopted the convention implied above that bare repository folders have a .git extension.
这是我认为最安全和最简单的方法。这里没有没有上面没有说明的。我只想看到一个显示安全分步程序的答案。您从要创建的存储库 (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
回答by apos
Simply read
简单阅读
Pro Git Book: 4.2 Git on the Server - Getting Git on a Server
专业 Git 手册:4.2 服务器上的 Git - 在服务器上获取 Git
which boild down to
归结为
$ git clone --bare my_project my_project.git
Cloning into bare repository 'my_project.git'...
done.
Then put my_project.gitto the server
然后把my_project.git放到服务器上
Which mainly is, what answer #42tried to point out. Shurely one could reinvent the wheel ;-)
这主要是,#42试图指出的答案是什么。当然可以重新发明轮子;-)
回答by nyteshade
Here is a little BASH function you can add to your .bashrc or .profile on a UNIX based system. Once added and the shell is either restarted or the file is reloaded via a call to source ~/.profile
or source ~/.bashrc
.
这是一个小 BASH 函数,您可以将其添加到基于 UNIX 的系统上的 .bashrc 或 .profile 中。添加后,shell 要么重新启动,要么通过调用source ~/.profile
或重新加载文件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
}
Once called within a directory containing a .git directory, it will make the appropriate changes to convert the repository. If there is no .git directory present when called, a FAILURE message will appear and no file system changes will happen.
一旦在包含 .git 目录的目录中调用,它将进行适当的更改以转换存储库。如果调用时不存在 .git 目录,则会出现 FAILURE 消息并且不会发生文件系统更改。