git clone,忽略目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14326365/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:15:58  来源:igfitidea点击:

git clone, ignoring a directory

git

提问by hookedonwinter

I'm trying to test something on a wordpress install. In doing so, I'd like to quickly replicate the repo. However, the upload directory (wp-content/uploads) is massive, so I'd like to ignore it.

我正在尝试在 wordpress 安装上测试一些东西。这样做时,我想快速复制回购。但是,上传目录(wp-content/uploads)很大,所以我想忽略它。

Note: I don't want to .gitignore this directory all the time, just for this scenario.

注意:我不想一直 .gitignore 这个目录,只是为了这个场景。

Basically, I'd like a command like this pseudo code: git clone --ignore wp-content/uploads.

基本上,我想这样的伪代码的命令:git clone --ignore wp-content/uploads

Is the best way to add that directory to .gitignore, clone, and then revert .gitignore? Or is there a better method?

将该目录添加到 .gitignore、克隆然后还原 .gitignore 的最佳方法是什么?或者有更好的方法吗?

回答by Rogier van het Schip

A bit late to the party, but: Don't you want a sparse checkout?

聚会有点晚了,但是:您不想结帐吗?

mkdir <repo> && cd <repo>
git init
git remote add –f <name> <url>

Enable sparse-checkout:

启用稀疏结帐:

git config core.sparsecheckout true

Configure sparse-checkout by listing your desired sub-trees in .git/info/sparse-checkout:

通过在 .git/info/sparse-checkout 中列出你想要的子树来配置 sparse-checkout:

echo some/dir/ >> .git/info/sparse-checkout
echo another/sub/tree >> .git/info/sparse-checkout

Checkout from the remote:

从远程结帐:

git pull <remote> <branch>

See http://jasonkarns.com/blog/subdirectory-checkouts-with-git-sparse-checkout/for more info.

有关更多信息,请参阅http://jasonkarns.com/blog/subdirectory-checkouts-with-git-sparse-checkout/

回答by poke

git clonewill always clone the completerepository*, including all previous commits ever added to the repository. So even if you remove the files temporarily, and clone it then, you will still receive the older versions which do contain those files.

git clone将始终克隆完整的存储库*,包括以前添加到存储库的所有提交。因此,即使您暂时删除文件,然后克隆它,您仍然会收到包含这些文件的旧版本。

Also, just editing the .gitignorewill not remove tracked files from the repository even if they would normally be ignored.

此外,仅编辑.gitignore不会从存储库中删除跟踪的文件,即使它们通常会被忽略。

So no, it is not really possible to skip a certain folder during cloning.

所以不,在克隆过程中真的不可能跳过某个文件夹。

*It is possible to limit the amount of commits retrieved during a clone, but this will not make the repository very usable.

*可以限制克隆期间检索的提交数量,但这不会使存储库非常有用。

回答by Adam Dymitruk

you can specify the depth to clone (--depth=1to get only 1 commit). You may want to set up a branch with this directory missing and then clone with depth of one only. Since git is snapshot based, it's not easy to exclude a part of a commit when cloning. This is the closest to what you want.

您可以指定要克隆的深度(--depth=1仅获得 1 次提交)。您可能希望设置一个缺少此目录的分支,然后仅以 1 的深度进行克隆。由于 git 是基于快照的,因此在克隆时排除一部分提交并不容易。这是最接近你想要的。

If you have full control of this repo, you may want to make a submodule of this directory and only do a submodule update when you want to admin that part.

如果你完全控制了这个 repo,你可能想要创建这个目录的子模块,并且只在你想要管理那个部分时才做一个子模块更新。

回答by jthill

On the server:

在服务器上:

 git checkout master^0    # the ^0 checks out the commit itself, not the branch
 git filter-branch --tree-filter 'git rm -r wp-content/uploads' HEAD
 git checkout -b filtered

(filter-branch on a big project here generates new history at about 2-3 commits per second)

(这里一个大项目的过滤器分支以每秒大约 2-3 次提交的速度生成新的历史记录)

Then, anywhere you like,

然后,在你喜欢的任何地方,

 git init
 git remote add gimme your://repo/path
 git fetch gimme filtered

edit: fixed syntax errors according to http://git-scm.com/docs/git-filter-branch

编辑:根据http://git-scm.com/docs/git-filter-branch修复语法错误