我有一个带有 git 的项目,我只想克隆或提取一个特定的目录,比如 myproject/javascript,就像 subversion 一样。进行一些更改,提交并再次推回。这是可能的?
cd 进入您的仓库副本的顶部 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
fetch
或 checkout
命令,我们只需在 remote add
命令之后发出拉取请求即可获得整个 master 分支。
经过多次寻找答案,没有找到,放弃,再次尝试等等,我终于在另一个 SO 线程中找到了解决方案:
How to git-pull all but one folder
要复制粘贴那里的内容:
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>
以正确拉动
如果您想在不输入目录的情况下获取目录中的最新更改,您可以执行以下操作:
$ 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 年 9 月 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: How do I clone a subdirectory only of a Git repository?
很可能在该区域中为 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
有时,您只想查看以前的文件副本,而无需经历差异。
在这种情况下,克隆存储库并签出您感兴趣的特定提交并查看该克隆存储库中的子目录同样容易。因为一切都是本地的,所以你可以在完成后删除这个克隆。
--
虽然不是必需的。它强制将其后的位解释为路径。所以,是的,如果您有一个名称为--help
的文件要签出,则需要在其前面加上这些连字符:git checkout HEAD -- --help