我有一个裸的 git 仓库,但需要通过 ssh 访问和浏览其内容(以类似文件管理器的用户体验方式)。
我认为我可以克隆它:
git clone -l <path_to_bare_repo> <new_normal_repo>
不过,我的代码库大小约为20GB,我没有足够的空间来复制它。有办法能够将裸代码库就地转换为带有工作副本的状态吗?
注意:我在一个非常简单的1-commit仓库上进行了测试。请再次核对,阅读man页面,并且在遵循您在StackOverflow上找到的建议之前一定要备份。 (您有备份,对吗?)
将一个--bare
仓库转换为非裸仓库的方法:
.git
文件夹。HEAD branches config description hooks info objects refs
等)移动到您刚刚创建的.git
中。git config --local --bool core.bare false
来将本地git仓库转换为非裸仓库。master
(或任何您的主分支)上,并且所有文件都已被删除并且删除已被暂存。这是正常的。只需手动检出master
或执行git reset --hard
,然后就完成了。.git/config
文件,在[remote“origin”]
部分的url=<...>
后添加一行fetch=+refs/heads/*:refs/remotes/origin/*
。否则,git fetch
将看不到origin/master
和其他原点的分支。这些步骤与将普通仓库转换为裸仓库的问题相反 - 特别注意此答案,它指出上述步骤(我推测是任意一种方向)与执行git-clone
有所不同。不确定这是否对你有关联,但你在问题中提到了git clone
。
我有一个稍微不同的情况:
解决方案:
.git
目录下:git clone --bare https://github.com/user/project .git
git config --local --bool core.bare false
.git
裸仓库不包括文件 'index
'。)git reset HEAD -- .
.git/index
了。cd /path/to/current/worktree
# That creates a .git directly at the right place
git clone --bare /url/of/repo .git
# restore the link between the local repo and its upstream remote repo
git config --local --bool core.bare false
git config --local remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git fetch origin
git branch -u origin/master master
# reset the index (not the working tree)
git reset HEAD -- .
git reset
step)更简单易懂。.git
子文件夹,最初由归档(非git)制成。我无法检出非裸体存储库,因为我进行检出的文件夹不为空。在子文件夹中执行非裸体git clone --no-checkout
将强制我将.git
上移一级。执行裸克隆允许我直接创建所需的.git
子文件夹。您可以在此处查看脚本:https://github.com/VonC/compileEverything/blob/3fb31b840b6d03e28125ffccb69122c07cd63837/git/update-repo-git.sh#L3-L21 - VonC原帖作者的问题是没有足够的空间以简单的方式完成某些事情。对于那些有足够空间的人,答案则要简单得多:
git clone foo.git foo
mkdir clone
mv bare.git clone/.git
更改 core.bare:
cd clone
git config --local --bool core.bare false
添加一个默认的来源refspec,使得git fetch
和git push
选择与通常相同的默认值:
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
生成索引文件和工作树:
git checkout master
我建议使用git checkout
而不是git reset
来生成文件,以防将其意外地输入到错误的位置。
cd
进入裸库,然后执行以下操作:
git config core.bare false
git reset --hard
git clone X.git X
git cat-file -p <commit-sha>
命令查看任何提交所引用的树。使用git cat-file -p <blob-sha>
命令查看由blob引用的文件的内容。使用git show <sha>:path
命令,其中sha是提交或树,以查看路径下blob的内容。git worktree add ../repo2
cd ..
git status # now works fine
推送即部署
与其将裸远程转换为标准存储库,不如使用钩子目录中的post-receive脚本将存储库扩展到部署目录。
为了方便参考,这是来自上面链接的脚本内容示例。它将仅将“master”分支的推送部署到与存储库父目录同级的名为“deploy”的目录中:
#!/usr/bin/env ruby
# post-receive
# 1. Read STDIN (Format: "from_commit to_commit branch_name")
from, to, branch = ARGF.read.split " "
# 2. Only deploy if master branch was pushed
if (branch =~ /master$/) == nil
puts "Received branch #{branch}, not deploying."
exit
end
# 3. Copy files to deploy directory
deploy_to_dir = File.expand_path('../deploy')
`GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f master`
puts "DEPLOY: master(#{to}) copied to '#{deploy_to_dir}'"
.git
的目录中,并将配置文件中的bare
参数设置为false,则它应该表现得像一个普通的代码仓库,您可以通过git checkout
命令获取文件。 - Noufal Ibrahimgit show
和git cat-file
在裸仓库中完成你想要的一切。 - William Pursell