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


当前回答

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

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

其他回答

是的,你可以通过这个命令下载一个特定的文件

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>

在git中,你在更新文件之前不会“签出”它们-这似乎是你所追求的。

许多系统,如clearcase, csv等,都要求您在对文件进行更改之前“签出”文件。Git不需要这个。克隆存储库,然后在存储库的本地副本中进行更改。

一旦你更新了文件,你可以做:

git status

查看修改了哪些文件。你先添加你想要提交给index的对象(index就像一个要签入的列表):

git add .

or

git add blah.c

然后do git status会告诉你哪些文件被修改了,哪些文件在索引中准备提交或签入。

要将文件提交到存储库副本,请执行以下操作:

git commit -a -m "commit message here"

参见git网站上的手册和指南链接。

非常简单:

git checkout from-branch-name -- path/to/the/file/you/want

这将不会签出from-branch-name分支。您将停留在您所在的分支上,并且只有该文件将从指定的分支签出。

下面是git-checkout手册的相关部分

git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...
       When <paths> or --patch are given, git checkout does not switch
       branches. It updates the named paths in the working tree from the
       index file or from a named <tree-ish> (most often a commit). In
       this case, the -b and --track options are meaningless and giving
       either of them results in an error. The <tree-ish> argument can be
       used to specify a specific tree-ish (i.e. commit, tag or tree) to
       update the index for the given paths before updating the working
       tree.

向Ariejan de Vroom致敬,他在这篇博客文章中教会了我这些。

通常不可能像第一个回答中建议的那样,只从git下载一个文件,而不下载整个存储库。 这是因为Git不像您想象的那样存储文件(像CVS/SVN那样),而是根据项目的整个历史生成文件。

但在特定情况下有一些变通办法。下面的例子为用户、项目、分支和文件名设置了占位符。

GitHub

wget https://raw.githubusercontent.com/user/project/branch/filename

GitLab

wget https://gitlab.com/user/project/raw/branch/filename

GitWeb

如果你在服务器上使用Git - GitWeb,那么你可以在示例中尝试(将其更改为正确的路径):

wget "http://example.com/gitweb/?p=example;a=blob_plain;f=README.txt;hb=HEAD"

GitWeb 和 drupalcode.org

例子:

wget "http://drupalcode.org/project/ads.git/blob_plain/refs/heads/master:/README.md"

googlesource.com

有一个未记录的特性允许你下载base64编码的原始文件版本:

curl "https://chromium.googlesource.com/chromium/src/net/+/master/http/transport_security_state_static.json?format=TEXT" | base64 --decode

在其他情况下,检查Git存储库是否使用任何web界面。

如果它不使用任何web界面,你可以考虑将你的代码推送到外部服务,如GitHub, Bitbucket等,并将其用作镜像。

如果你没有安装wget,试试curl -O (url)。

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

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

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