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


当前回答

如果您只需要下载文件,则不需要使用Git签出。

GitHub Mate更容易做到这一点,它是一个Chrome扩展,让你点击文件图标下载它。也是开源的

其他回答

最小的指南

Git checkout——<filename>


裁判:https://git-scm.com/docs/git-checkout

Dup:在Git中撤销一个文件的工作副本修改?

如果你有一个本地修改的文件(搅乱了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

Git checkout <other-branch>—<single-file>适用于我的Git .2.37.1。 然而,该文件是(git-魔术)提交的,我不能看到正确的git diff。 然后运行git restore——staging db/structure。SQL来取消它。

这样我就有了我想要的确切版本的文件,我可以看到该文件与其他版本的区别。

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

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

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

首先使用-n选项和——depth 1选项克隆repo,前者抑制所有文件的默认签出,后者意味着它只获取每个文件的最新修订

git clone -n git://path/to/the_repo.git --depth 1

然后签出你想要的文件,就像这样:

cd the_repo
git checkout HEAD name_of_file