英文:
How to get the contents of a folder in a repository to the root of a local folder?
问题
这是一个本地文件夹 /var/www/test/
还有一个Git存储库,其中有一个 build/static
文件夹。我需要获取此文件夹中的文件和文件夹 /var/www/test/
但结果是,在 /var/www/test/
中创建了 build
文件夹,里面有一个 static
文件夹。我需要 static
在 /var/www/test/
的根目录下(即 /var/www/test/static
)
如何正确操作?提前感谢。
尝试像这样操作:
cd /var/www/test/
git init
git remote add -f origin <repo_address>
git config core.sparseCheckout true
echo 'build/static' >> .git/info/sparse-checkout
git pull origin master
英文:
There is a local folder /var/www/test/
There is also a Git repository that has a build/static
folder. I need to get files and folders from this folder in /var/www/test/
But as a result, I got that in /var/www/test/
, build
was created with a static
folder inside. And I need static
to be at the root of /var/www/test/
(i.e. /var/www/test/static
)
How to do it right? Thanks in advance.
Tried like this:
cd /var/www/test/
git init
git remote add -f origin <repo_address>
git config core.sparseCheckout true
echo 'build/static' >> .git/info/sparse-checkout
git pull origin master
答案1
得分: 1
你可以在远程主机的/var/www
目录之外创建一个Git裸仓库。
在它上面添加一个pre-receive
钩子,将你的仓库子目录的内容部署到/var/www/test/static
。
这个示例假设/var/www/test/static
已经存在且为空。
另外,假定build
目录位于你仓库的顶层。
你可以尝试其他目录结构,但这对我来说有效。
当你推送到远程裸仓库时,pre-receive
钩子应该部署文件。
注意:
- 如果在
/var/www/test/static
中手动更新文件,Git将拒绝覆盖它们。 - 不需要第三方Git托管服务。
#!/bin/sh
# custom read/write-tree deploy的pre-receive脚本
while read old new ref; do [[ $ref = refs/heads/master ]] && {
export GIT_INDEX_FILE="$GIT_DIR/deploy_index"
export GIT_WORK_TREE="/var/www/test/static"
git read-tree -um $(git write-tree) $new:build/static || exit 1
}; done
英文:
You could create a git bare repo on your remote host outside of /var/www
.
Add a pre-receive
hook to it that deploys the contents of a sub folder of your repo into
/var/www/test/static
. This example will expect /var/www/test/static
to exist and be empty.
Also build
is assumed to be at the top level of your repository.
You could experiment with other directory structures, but this works for me.
When you push to the remote bare repo, the pre-receive hook should deploy the files.
Notes:
- If files are manually updated in
/var/www/test/static
, git will refuse to overwrite them. - A third party git hosting service is not required.
#!/bin/sh
# pre-receive script for custom read/write-tree deploy
while read old new ref; do [[ $ref = refs/heads/master ]] && {
export GIT_INDEX_FILE="$GIT_DIR/deploy_index"
export GIT_WORK_TREE="/var/www/test/static"
git read-tree -um $(git write-tree) $new:build/static || exit 1
}; done
答案2
得分: 0
我提出一种方法,可以避免在意图提供服务的文件夹中出现.git
目录(即Git本地仓库元数据),因为您可能不想冒将项目历史公开的风险。
这也将更容易在未来进行操作。
- 确保您的
/var/www/test/
目录中没有static
文件夹(如果存在,删除它)。 - 在其他地方克隆您的仓库(是否稀疏无关紧要)(不要在
/var/www/test/
中克隆)。 - 将您检出的
static
文件夹链接到/var/www/test/
:ln -s /path-to-your-cloned/build/static /var/www/test/static
(注意:
-s
选项用于创建符号链接,如果出于某种原因需要硬链接,可以删除此选项)
完成!每次您拉取时,新内容将自动在/var/www/test/
中提供。
英文:
I propose an approach that will avoid having .git
directory (i.e. Git local repository metadata) in a folder that is meant to be served, because you probably don’t want to risk making your project history public.
It also will be easier to work with in the future.
- Make sure you don’t have a
static
folder in/var/www/test/
(remove it if existing) - Clone your repository (sparsely or not, does not impact the outcome) somewhere else (not in
/var/www/test/
) - Link your checked out
static
folder into/var/www/test/
:ln -s /path-to-your-cloned/build/static /var/www/test/static
(note: the
-s
option is for creating a symbolic link, you can instead make a hardlink if you need it for some reason by removing this option)
Done! Any time you pull, the new content will automatically be made available in /var/www/test/
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论