繁体   English   中英

如何使用 git 拉取特定目录

[英]How to pull specific directory with git

我有一个带有 git 的项目,我只想克隆或拉取特定目录,例如 myproject/javascript,就像 subversion 一样。
进行一些更改,提交并再次推回。
这是可能的?

  1. cd 到你的 repo 副本的顶部
  2. git fetch
  3. git checkout HEAD path/to/your/dir/or/file

    • (3)中的“ path/... ”从包含“ .../file ”的存储库根目录正下方的目录开始

    • 请注意,可以使用特定提交的哈希码代替“HEAD”,然后您将获得特定于该提交的修订(文件)或修订(目录)。

在空目录中:

git init
git remote add [REMOTE_NAME] [GIT_URL]
git fetch REMOTE_NAME
git checkout REMOTE_NAME/BRANCH -- path/to/directory

经过多次寻找答案,没有找到,放弃,再试等等,我终于在另一个SO线程中找到了解决方案:

如何 git-pull 除了一个文件夹之外的所有文件夹

要复制粘贴那里的内容:

git init
git remote add -f origin <url>
git config core.sparsecheckout true
echo <dir1>/ >> .git/info/sparse-checkout
echo <dir2>/ >> .git/info/sparse-checkout
echo <dir3>/ >> .git/info/sparse-checkout
git pull origin master

要执行 OP 想要的操作(仅处理一个目录),只需在执行上述步骤时将该目录添加到.git/info/sparse-checkout

非常感谢@cforbish!

如果您想在不输入目录的情况下获取目录中的最新更改,您可以执行以下操作:

$ git -C <Path to directory> pull

也许这个命令会有所帮助:

git archive --remote=MyRemoteGitRepo --format=tar BranchName_or_commit  path/to/your/dir/or/file > files.tar

“等等”

这是不可能的。 您需要拉所有存储库或什么都不拉。

来自 git 2.19 的git clone --filter现在可以在 GitHub 上运行(测试 2020-09-18,git 2.25.1)

我不确定拉/取,但至少对于初始克隆,此选项与远程协议的更新一起添加,它确实阻止了从服务器下载对象。

例如,仅克隆此存储库的d1所需的对象: https : //github.com/cirosantilli/test-git-partial-clone我可以这样做:

git clone \
  --depth 1 \
  --filter=blob:none \
  --no-checkout \
  https://github.com/cirosantilli/test-git-partial-clone \
;
cd test-git-partial-clone
git checkout master -- d1

我在以下位置更详细地介绍了这一点: Git:如何仅克隆 Git 存储库的子目录?

很可能在那个领域为git clone实现的任何东西也有git pull类似物,但我还不能很容易地找到它。

尝试并测试了这个作品!

mkdir <directory name> ;  //Same directory name as the one you want to pull
cd <directory name>;
git remote add origin <GIT_URL>;
git checkout -b '<branch name>';
git config core.sparsecheckout true;
echo <directory name>/ >> .git/info/sparse-checkout;
git pull origin <pull branch name>

希望这是有帮助的!

有时,您只想查看以前的文件副本,而无需经历差异的繁琐。

在这种情况下,克隆存储库并签出您感兴趣的特定提交并查看该克隆存储库中的子目录同样容易。 因为一切都是本地的,所以你可以在完成后删除这个克隆。

对于像我一样在理论文件路径和示例方面的所有挣扎,这里有一个真实的例子:微软在 git hub 上提供了他们的文档和示例,不幸的是,他们确实在这个存储库中收集了大量主题的所有示例文件:

https://github.com/microsoftarchive/msdn-code-gallery-community-s-z

我只对路径中的 Microsoft Dynamics js 文件感兴趣

msdn-code-gallery-community-s-z/Sdk.Soap.js/

所以我做了以下

创建一个

msdn-code-gallery-community-s-zSdkSoapjs\.git\info\sparse-checkout

磁盘上我的存储库文件夹中的文件

git sparse-checkout init

在 Windows 上使用 cmd 在该目录中

文件内容为

msdn-code-gallery-community-s-zSdkSoapjs\.git\info\sparse-checkout

Sdk.Soap.js/*

最后做一个

git pull origin master

暂无
暂无

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

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