我有一个带有 git 的项目,我只想克隆或提取一个特定的目录,比如 myproject/javascript,就像 subversion 一样。
进行一些更改,提交并再次推回。
这是可能的?
10 回答
- cd 进入你的 repo 副本的顶部
git fetch
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 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 clone --filter
来自 git 2.19 现在可以在 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>
希望这有帮助!
对于所有像我一样在理论文件路径和示例方面遇到的困难,这里有一个真实的示例:Microsoft 在 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
有时,您只想查看以前的文件副本,而无需经历差异。
在这种情况下,克隆存储库并签出您感兴趣的特定提交并查看该克隆存储库中的子目录同样容易。因为一切都是本地的,所以你可以在完成后删除这个克隆。