简体   繁体   English

从 git 中提取特定文件或特定文件夹

[英]Pull a particular file or a particular folder from git

I want to know that is there any way in the git by which we can pull a particular folder or a particular file from the git.我想知道在 git 中有什么方法可以从 git 中提取特定文件夹或特定文件。

Thanks in advance.提前致谢。

For now, you have to fetch everything, and:现在,您必须获取所有内容,并且:

  • either do a shallow clone要么做一个 浅克隆
  • or copy what you need from the cloned repo in another working folder.或者从另一个工作文件夹中的克隆存储库中复制您需要的内容。

But that won't always be the case.但情况并非总是如此。
A future 2.16.x/2.17 Q2 2018 Git will introduce what is called a partial clone (or narrow clone)未来的 2.16.x/2.17 Q2 2018 Git 将引入所谓的部分克隆(或窄克隆)

See See commit 3aa6694 , commit aa57b87 , commit 35a7ae9 , commit 1e1e39b , commit acb0c57 , commit bc2d0c3 , commit 640d8b7 , commit 10ac85c (08 Dec 2017) by Jeff Hostetler ( jeffhostetler ) .请参阅提交 3aa6694提交 aa57b87提交 35a7ae9提交 1e1e39b提交 acb0c57提交 bc2d0c3提交 640d8b7提交 10ac85c (2017 年 12 月 8 日)(杰夫· jeffhostetler )( jeffhostetler
See commit a1c6d7c , commit c0c578b , commit 548719f , commit a174334 , commit 0b6069f (08 Dec 2017) by Jonathan Tan ( jhowtan ) .请参阅Jonathan Tan ( jhowtan ) 的commit a1c6d7ccommit c0c578bcommit 548719fcommit a174334commit 0b6069f (2017 年 12 月 8 日
(Merged by Junio C Hamano -- gitster -- in commit 6bed209 , 13 Feb 2018) (由Junio C gitster合并-- gitster -- in commit 6bed209 ,2018 年 2 月 13 日)

See the tests for a partial clone here :此处查看部分克隆测试

git clone --no-checkout --filter=blob:none "file://$(pwd)/srv.bare" pc1 

Combine that with the Git 2.25 (Q1 2020) git sparse-checkout command .将其与Git 2.25(2020 年第一季度) git sparse-checkout命令结合使用

See more with " Bring your monorepo down to size with sparse-checkout " from Derrick Stolee查看更多来自Derrick Stolee 的使用稀疏结帐缩小您的 monorepo 的大小

Pairing sparse-checkout with the partial clone feature accelerates these workflows even more.将稀疏结账与部分克隆功能配对可以进一步加速这些工作流程。
This combination speeds up the data transfer process since you don't need every reachable Git object, and instead, can download only those you need to populate your cone of the working directory这种组合加快了数据传输过程,因为您不需要每个可访问的 Git 对象,而是可以仅下载填充工作目录锥所需的那些对象

$ git clone --filter=blob:none --no-checkout https://github.com/derrickstolee/sparse-checkout-example
Cloning into 'sparse-checkout-example'...
Receiving objects: 100% (373/373), 75.98 KiB | 2.71 MiB/s, done.
Resolving deltas: 100% (23/23), done.
 
$ cd sparse-checkout-example/
 
$ git sparse-checkout init --cone
Receiving objects: 100% (3/3), 1.41 KiB | 1.41 MiB/s, done.
 
$ git sparse-checkout set client/android
Receiving objects: 100% (26/26), 985.91 KiB | 5.76 MiB/s, done.

With Git 2.31 (Q1 2021), fix in passing custom args from " git clone " ( man ) to upload-pack on the other side.在 Git 2.31(2021 年第一季度)中,修复了将自定义参数从“ git cloneman传递到另一端的upload-pack问题。

See commit ad6b5fe (02 Feb 2021), and commit ad5df6b (28 Jan 2021) by Jacob Vosmaer ( jacobvosmaer ) .提交ad6b5fe (2021年2月2日),并提交ad5df6b (2021年1月28日),由雅各布·沃斯莫尔( jacobvosmaer
(Merged by Junio C Hamano -- gitster -- in commit 60f8121 , 12 Feb 2021) (由Junio C gitster合并-- gitster -- in commit 60f8121 ,2021 年 2 月 12 日)

upload-pack.c : fix filter spec quoting bug upload-pack.c :修复过滤器规范引用错误

Signed-off-by: Jacob Vosmaer签字人:Jacob Vosmaer

Fix a bug in upload-pack.c that occurs when you combine partial clone and uploadpack.packObjectsHook.修复了在将部分克隆和 uploadpack.packObjectsHook 组合时发生的upload-pack.c中的错误。
You can reproduce it as follows:您可以按如下方式重现它:

 git clone -u 'git -c uploadpack.allowfilter '\\ uploadpack.packobjectshook=env '\\ load-pack' --filter=blob:none --no-local \\ .git dst.git

Be careful with the line endings because this has a long quoted string as the -u argument.小心行尾,因为它有一个长引号字符串作为 -u 参数。

The error I get when I run this is:我运行时得到的错误是:

 Cloning into '/tmp/broken'... remote: fatal: invalid filter-spec ''blob:none'' error: git upload-pack: git-pack-objects died with error. fatal: git upload-pack: aborting due to possible repository corruption on the remote side. remote: aborting due to possible repository corruption on the remote side. fatal: early EOF fatal: index-pack failed

The problem is caused by unneeded quoting.问题是由不必要的引用引起的。

This bug was already present in 10ac85c (" upload-pack : add object filtering for partial clone", 2017-12-08, Git v2.17.0-rc0 -- merge listed in batch #2 ) when the server side filter support was introduced.当引入服务器端过滤器支持时,此错误已存在于10ac85c (“ upload-pack :为部分克隆添加对象过滤”,2017 年 12 月 8 日,Git v2.17.0-rc0 -- 合并第 2 批中列出) .
In fact, in 10ac85c this was broken regardless of uploadpack.packObjectsHook.事实上,在10ac85c 中,无论uploadpack.packObjectsHook 如何,这都被破坏了。
Then in 0b6069f (" fetch-pack : test support excluding large blobs", 2017-12-08, Git v2.17.0-rc0 -- merge listed in batch #2 ) the quoting was removed but only behind a conditional that depends on whether uploadpack.packObjectsHook is set.然后在0b6069f (" fetch-pack : test support without large blob", 2017-12-08, Git v2.17.0-rc0 -- 合并批处理#2中列出) 引用被删除,但仅在一个条件后面,取决于是否设置了uploadpack.packObjectsHook

Because uploadpack.packObjectsHook is apparently rarely used, nobody noticed the problematic quoting could still happen.因为uploadpack.packObjectsHook显然很少使用,没有人注意到有问题的引用仍然可能发生。

Remove the conditional quoting.删除条件引用。

If you want to update particular file to origin state do:如果要将特定文件更新为origin状态,请执行以下操作:

git fetch origin
git checkout  origin/[yourbranch] -- [path_to_your_files]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM