如何使用 git 拉取特定目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2425059/
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
How to pull specific directory with git
提问by bluefoot
I have a project with git, and I just want to clone or pull a specific directory, like myproject/javascript just like subversion does.
make some changes, commit and push back again.
It's possible?
我有一个带有 git 的项目,我只想克隆或拉取特定目录,例如 myproject/javascript,就像 subversion 一样。
进行一些更改,提交并再次推回。
这是可能的?
回答by vergueishon
- cd into the top of your repo copy
git fetch
git checkout HEAD path/to/your/dir/or/file
Where "
path/...
" in (3) starts at the directory just below the repo root containing your ".../file
"NOTE that instead of "HEAD", the hash code of a specific commit may be used, and then you will get the revision (file) or revisions (dir) specific to that commit.
- cd 到你的 repo 副本的顶部
git fetch
git checkout HEAD path/to/your/dir/or/file
其中,“
path/...
”(3)开始在目录略低于含回购根你的“.../file
”请注意,可以使用特定提交的哈希码代替“HEAD”,然后您将获得特定于该提交的修订(文件)或修订(目录)。
回答by scottalan
In an empty directory:
在空目录中:
git init
git remote add [REMOTE_NAME] [GIT_URL]
git fetch REMOTE_NAME
git checkout REMOTE_NAME/BRANCH -- path/to/directory
回答by msb
After much looking for an answer, not finding, giving up, trying again and so on, I finally found a solution to this in another SO thread:
经过多次寻找答案,没有找到,放弃,再试等等,我终于在另一个SO线程中找到了解决方案:
How to git-pull all but one folder
To copy-paste what's there:
要复制粘贴那里的内容:
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
To do what OP wants (work on only one dir), just add that one dir to .git/info/sparse-checkout
, when doing the steps above.
要执行 OP 想要的操作(仅处理一个目录),只需.git/info/sparse-checkout
在执行上述步骤时将该目录添加到。
Many many thanks to @cforbish !
非常感谢@cforbish!
回答by shingara
It's not possible. You need pull all repository or nothing.
这是不可能的。您需要拉所有存储库或什么都不拉。
回答by fabrice belle
Maybe this command can be helpful :
也许这个命令会有所帮助:
git archive --remote=MyRemoteGitRepo --format=tar BranchName_or_commit path/to/your/dir/or/file > files.tar
"Et voilà"
“等等”
回答by feralamillo
If you want to get the latest changes in a directory without entering it, you can do:
如果您想在不输入目录的情况下获取目录中的最新更改,您可以执行以下操作:
$ git -C <Path to directory> pull
回答by Nnaik
Tried and tested this works !
尝试并测试了这个作品!
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>
Hope this was helpful!
希望这是有帮助的!
回答by Abizern
Sometimes, you just want to have a look at previous copies of files without the rigmarole of going through the diffs.
有时,您只想查看以前的文件副本,而无需经历差异的繁琐。
In such a case, it's just as easy to make a clone of a repository and checkout the specific commit that you are interested in and have a look at the subdirectory in that cloned repository. Because everything is local you can just delete this clone when you are done.
在这种情况下,克隆存储库并检出您感兴趣的特定提交并查看该克隆存储库中的子目录同样容易。因为一切都是本地的,所以你可以在完成后删除这个克隆。
回答by D M
For all that struggle with theoretical file paths and examples like I did, here a real world example: Microsoft offers their docs and examples on git hub, unfortunately they do gather all their example files for a large amount of topics in this repository:
对于像我一样在理论文件路径和示例方面的所有挣扎,这里有一个真实的例子:微软在 git hub 上提供了他们的文档和示例,不幸的是,他们确实在这个存储库中收集了大量主题的所有示例文件:
https://github.com/microsoftarchive/msdn-code-gallery-community-s-z
I only was interested in the Microsoft Dynamics js files in the path
我只对路径中的 Microsoft Dynamics js 文件感兴趣
msdn-code-gallery-community-s-z/Sdk.Soap.js/
so I did the following
所以我做了以下
create a
创建一个
msdn-code-gallery-community-s-zSdkSoapjs\.git\info\sparse-checkout
file in my repositories folder on the disk
磁盘上我的存储库文件夹中的文件
git sparse-checkout init
in that directory using cmd on windows
在 Windows 上使用 cmd 在该目录中
The file contents of
文件内容为
msdn-code-gallery-community-s-zSdkSoapjs\.git\info\sparse-checkout
is
是
Sdk.Soap.js/*
finally do a
最后做一个
git pull origin master