我如何从一个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本质上不是这样的——它是分布式的。如果你想使用git存储库,你可以克隆它。然后,您就拥有了工作树的所有内容和所有历史记录(好吧,至少是指向当前分支顶端的所有内容),而不仅仅是单个文件或单个提交的快照。
git clone /path/to/repo
git clone git://url/of/repo
git clone http://url/of/repo
如果您编辑了文件的本地版本,并希望恢复到中央服务器上维护的原始版本,可以使用Git扩展轻松实现。
最初文件将被标记为提交,因为它已被修改 在文件树菜单中选择(双击)该文件 列出了单个文件的修订树。 选择树的顶部/HEAD,然后右键单击save as 保存该文件以覆盖修改后的文件本地版本 文件现在有正确的版本,将不再被标记为提交!
简单!
是的,你可以通过这个命令下载一个特定的文件
wget -o <DesiredFileName> <Git FilePath>\?token\=<personalGitToken>
例子
wget -o javascript-test-automation.md https://github.com/akashgupta03/awesome-test-automation/blob/master/javascript-test-automation.md\?token\=<githubPersonalTone>
另一个解决方案,类似于使用——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 ;)
请考虑到,如果您必须签出多个文件,树填充也会影响您的性能,因此我建议仅在回购足够大的情况下才对单个文件执行此操作。
如果你有一个本地修改的文件(搅乱了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