我如何从一个git回购签出一个文件?


当前回答

我没有看到在这里列出的对我有用的东西,所以我将包括它,如果有人在我的情况下。

My situation, I have a remote repository of maybe 10,000 files and I need to build an RPM file for my Linux system. The build of the RPM includes a git clone of everything. All I need is one file to start the RPM build. I can clone the entire source tree which does what I need but it takes an extra two minutes to download all those files when all I need is one. I tried to use the git archive option discussed and I got “fatal: Operation not supported by protocol.” It seems I have to get some sort of archive option enabled on the server and my server is maintained by bureaucratic thugs that seem to enjoy making it difficult to get things done.

最后我进入了bitbucket的网页界面,看到了我需要的一个文件。我右键点击链接下载文件的原始副本,并从弹出的结果中选择“复制快捷方式”。我不能只是下载原始文件,因为我需要自动化操作,而且我的Linux服务器上没有浏览器界面。

为了便于讨论,结果是URL:

https://ourArchive.ourCompany.com/projects/ThisProject/repos/data/raw/foo/bar.spec?at=refs%2Fheads%2FTheBranchOfInterest

我不能直接从bitbucket存储库下载这个文件,因为我需要先登录。经过一番挖掘,我发现这个方法有效: 在Linux上:

echo "myUser:myPass123"| base64
bXlVc2VyOm15UGFzczEyMwo=

curl -H 'Authorization: Basic bXlVc2VyOm15UGFzczEyMwo=' 'https://ourArchive.ourCompany.com/projects/ThisProject/repos/data/raw/foo/bar.spec?at=refs%2Fheads%2FTheBranchOfInterest' > bar.spec

这种组合允许我下载构建其他所有内容所需的一个文件。

其他回答

Git checkout branch_or_version——path/file

示例:git checkout HEAD——main.c

如果你已经有了一个git回购的副本,你可以使用git日志签出一个文件的版本来找出哈希id(例如3cdc61015724f9965575ba954c8cd4232c8b42e4),然后你只需输入:

git checkout hash-id path-to-file

下面是一个实际的例子:

git checkout 3cdc61015724f9965575ba954c8cd4232c8b42e4 /var/www/css/page.css

这对我很有用。使用git和一些shell命令

git clone --no-checkout --depth 1 git.example.com/project.git && cd project && git show HEAD:path/to/file_you_need > ../file_you_need && cd .. && rm -rf project

如果你有一个本地修改的文件(搅乱了git的文件),只需要这样做:

git checkout origin/master filename

Git签出-切换分支或恢复工作树文件,(这里我们什么都不切换,只是覆盖文件 Origin /master -您当前的分支,或者您可以使用特定的修订号,例如:cd0fa799c582e94e59e5b21e872f5ffe2ad0154b, 包含项目主目录路径的文件名(.git目录所在位置) 所以如果你有结构:

“.git

公共/ index . html

public / css / style . css

供应商

composer.lock”

想要重新加载index.html -只需使用public/index.html

另一个解决方案,类似于使用——filter=blob:none的解决方案是使用——filter=tree:0(你可以在这里阅读关于区别的解释)。

这种方法通常比blob-one更快,因为它不下载树结构,但有一个缺点。考虑到您延迟了树的检索,当您进入repo目录时将受到惩罚(取决于repo的大小和结构,它可能比简单的浅克隆大许多倍)。

如果你是这种情况,你可以通过不进入回购来解决:

git clone -n --filter=tree:0 <repo_url> tgt_dir
git -C tgt_dir checkout <branch> -- <filename>
cat tgt_dir/<filename> # or move it to another place and delete tgt_dir ;)

请考虑到,如果您必须签出多个文件,树填充也会影响您的性能,因此我建议仅在回购足够大的情况下才对单个文件执行此操作。