我有一个使用Git的项目,我想要像Subversion那样只克隆或拉取特定目录,比如myproject/javascript。
然后进行一些更改,提交并再次推送回去。
这是可能的吗?
我有一个使用Git的项目,我想要像Subversion那样只克隆或拉取特定目录,比如myproject/javascript。
然后进行一些更改,提交并再次推送回去。
这是可能的吗?
git fetch
git checkout HEAD path/to/your/dir/or/file
(3)中的"path/...
"从仓库根目录下的包含".../file
"的目录开始
注意,可以使用特定提交的哈希码替换"HEAD",那么你将会得到该提交所特有的修订版(文件)或多个修订版(目录)。
--
。它强制其后的位被解释为路径。因此,如果您要检出一个名为 --help
的文件,那么需要在前面加上这些连字符:git checkout HEAD -- --help
。 - Tim Viséegit init
git remote add [REMOTE_NAME] [GIT_URL]
git fetch REMOTE_NAME
git checkout REMOTE_NAME/BRANCH -- path/to/directory
fetch
或checkout
命令,我们可以在执行remote add
命令后直接发起拉取请求来获取整个主分支。 - Iqra.大多数答案/技术会下载整个代码库,即使你仅需要看到/使用其中的一部分。我不喜欢这样做,因为我正在处理的一些项目有很多对我没有兴趣的大文件。在寻找答案、发现没有、放弃、再次尝试等过程中,我最终在另一个SO线程中找到了解决方法:
复制粘贴该链接上的内容:
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!
master
分支上。只需确保执行 git branch --set-upstream-to=origin/<branch>
以正确拉取。 - seebiscuitgit read-tree -m -u HEAD
(如此处所示:http://vmiklos.hu/blog/sparse-checkout-example-in-git-1-7)。 - TheMastergit remote
的-f
选项会立即获取整个仓库。请参考此Stack Overflow答案获取更多信息:https://stackoverflow.com/a/4909267/6735035 - undefined如果你想在不进入目录的情况下获取最新的更改,你可以执行以下操作:
$ git -C <Path to directory> pull
git archive --remote=MyRemoteGitRepo --format=tar BranchName_or_commit path/to/your/dir/or/file > files.tar
"Et voilà"
git clone --filter
+ git sparse-checkout
可以仅下载所需文件
我不确定 pull/fetch,但至少对于初始克隆,该选项是与远程协议的更新一起添加的,并且确实防止了从服务器下载对象。
例如,要仅克隆此存储库子目录 small/
所需的对象: https://github.com/cirosantilli/test-git-partial-clone-big-small-no-bigtree,可以执行以下操作:
git clone -n --depth=1 --filter=tree:0 \
https://github.com/cirosantilli/test-git-partial-clone-big-small-no-bigtree
cd test-git-partial-clone-big-small-no-bigtree
git sparse-checkout set --no-cone small
git checkout
我在这里更详细地介绍了:如何仅克隆Git存储库的子目录?
很可能无论在哪个领域实现git clone
,也会有git pull
类似物,但我还没有找到它。
在2021年1月测试了git 2.30.0。
这是不可能的。你需要拉取整个代码库,否则就什么也得不到。
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>
对于像我一样在理论文件路径和示例方面有困难的人,这里提供一个真实世界的例子:微软在GitHub上提供了他们的文档和示例,不幸的是,他们将大量主题的所有示例文件都放在了同一个代码库中:
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
msdn-code-gallery-community-s-zSdkSoapjs\.git\info\sparse-checkout
是
Sdk.Soap.js/*
git pull origin master