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


当前回答

在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致敬,他在这篇博客文章中教会了我这些。

最初,我在2012年git档案中提到(见Jared Forsyth的答案和Robert Knight的答案),自git1.7.9.5(2012年3月)以来,Paul Brannan的答案是:

git archive --format=tar --remote=origin HEAD:path/to/directory -- filename | tar -O -xf -

但是:在2013年,这对于远程https://github.com url来说已经不可能了。 请参阅旧页面“我可以存档存储库吗?”

当前(2018)页面“关于GitHub上的内容和数据存档”建议使用第三方服务,如GHTorrent或GH Archive。


所以你也可以处理本地拷贝/克隆:

如果您有本回答中提到的裸存储库的本地副本,您也可以执行以下操作,

git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file

或者你必须首先克隆回购,这意味着你得到完整的历史:

在.git回购中 在工作树中。 但是你可以做一个稀疏签出(如果你使用Git1.7+),: 启用稀疏签出选项(git config core.)sparsecheckout真的) 在.git/info/sparse-checkout文件中添加您想要看到的内容 重新读取工作树以只显示您需要的内容

要重新阅读工作树:

$ git read-tree -m -u HEAD

这样,您就得到了一个工作树,其中精确地包含了您想要的内容(即使它只有一个文件)。


Richard Gomes指出(在评论中)“如何从git存储库中克隆、获取或稀疏签出单个目录或目录列表?”

避免下载历史记录的bash函数,它检索单个分支并检索所需的文件或目录列表。


在Git 2.40 (Q1 2023)中,通过检查稀疏模式来判断我们是否使用了“锥”模式的逻辑已经收紧,以避免将单个文件命名为指定锥的模式。

参见William Sprent (williams-unity)提交的5842710(2023年1月03日)。 (由Junio C Hamano—gitster—在commit ab85a7d中合并,2023年1月16日)

目录:检查单文件圆锥模式 署名:威廉·斯普伦特 致谢:维多利亚·戴伊

The sparse checkout documentation states that the cone mode pattern set is limited to patterns that either recursively include directories or patterns that match all files in a directory. In the sparse checkout file, the former manifest in the form: /A/B/C/ while the latter become a pair of patterns either in the form: /A/B/ !/A/B/*/ or in the special case of matching the toplevel files: /* !/*/ The 'add_pattern_to_hashsets()' function contains checks which serve to disable cone-mode when non-cone patterns are encountered. However, these do not catch when the pattern list attempts to match a single file or directory, e.g. a pattern in the form: /A/B/C This causes sparse-checkout to exhibit unexpected behaviour when such a pattern is in the sparse-checkout file and cone mode is enabled. Concretely, with the pattern like the above, sparse-checkout, in non-cone mode, will only include the directory or file located at '/A/B/C'. However, with cone mode enabled, sparse-checkout will instead just manifest the toplevel files but not any file located at '/A/B/C'. Relatedly, issues occur when supplying the same kind of filter when partial cloning with '--filter=sparse:oid=<oid>'. 'upload-pack' will correctly just include the objects that match the non-cone pattern matching. Which means that checking out the newly cloned repo with the same filter, but with cone mode enabled, fails due to missing objects. To fix these issues, add a cone mode pattern check that asserts that every pattern is either a directory match or the pattern '/*'. Add a test to verify the new pattern check and modify another to reflect that non-directory patterns are caught earlier.

在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网站上的手册和指南链接。

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

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 2.19克隆过滤器

这个选项实际上会跳过从服务器获取大多数不需要的对象:

git clone --depth 1 --no-checkout --filter=blob:none \
  "file://$(pwd)/server_repo" local_repo
cd local_repo
git checkout master -- mydir/myfile

服务器应该配置:

git config --local uploadpack.allowfilter 1
git config --local uploadpack.allowanysha1inwant 1

v2.19.0版本没有服务器支持,但是已经可以在本地进行测试。

TODO:——filter=blob:none跳过所有blob,但仍然获取所有树对象。但在正常的回购中,与文件本身相比,这应该很小,所以这已经足够好了。问:https://www.spinics.net/lists/git/msg342006.html开发人员回答——filter=tree:0是在工作中做到这一点。

请记住—深度1已经暗示—单分支,请参见:如何在Git中克隆单个分支?

file://$(path)需要克服git克隆协议的欺骗:如何浅克隆一个本地git存储库与相对路径?

——filter的格式记录在man git-rev-list中。

对Git远程协议进行了扩展以支持此功能。

Git树中的文档:

https://github.com/git/git/blob/v2.19.0/Documentation/technical/partial-clone.txt https://github.com/git/git/blob/v2.19.0/Documentation/rev-list-options.txt#L720 https://github.com/git/git/blob/v2.19.0/t/t5616-partial-clone.sh

测试一下

#!/usr/bin/env bash
set -eu

list-objects() (
  git rev-list --all --objects
  echo "master commit SHA: $(git log -1 --format="%H")"
  echo "mybranch commit SHA: $(git log -1 --format="%H")"
  git ls-tree master
  git ls-tree mybranch | grep mybranch
  git ls-tree master~ | grep root
)

# Reproducibility.
export GIT_COMMITTER_NAME='a'
export GIT_COMMITTER_EMAIL='a'
export GIT_AUTHOR_NAME='a'
export GIT_AUTHOR_EMAIL='a'
export GIT_COMMITTER_DATE='2000-01-01T00:00:00+0000'
export GIT_AUTHOR_DATE='2000-01-01T00:00:00+0000'

rm -rf server_repo local_repo
mkdir server_repo
cd server_repo

# Create repo.
git init --quiet
git config --local uploadpack.allowfilter 1
git config --local uploadpack.allowanysha1inwant 1

# First commit.
# Directories present in all branches.
mkdir d1 d2
printf 'd1/a' > ./d1/a
printf 'd1/b' > ./d1/b
printf 'd2/a' > ./d2/a
printf 'd2/b' > ./d2/b
# Present only in root.
mkdir 'root'
printf 'root' > ./root/root
git add .
git commit -m 'root' --quiet

# Second commit only on master.
git rm --quiet -r ./root
mkdir 'master'
printf 'master' > ./master/master
git add .
git commit -m 'master commit' --quiet

# Second commit only on mybranch.
git checkout -b mybranch --quiet master~
git rm --quiet -r ./root
mkdir 'mybranch'
printf 'mybranch' > ./mybranch/mybranch
git add .
git commit -m 'mybranch commit' --quiet

echo "# List and identify all objects"
list-objects
echo

# Restore master.
git checkout --quiet master
cd ..

# Clone. Don't checkout for now, only .git/ dir.
git clone --depth 1 --quiet --no-checkout --filter=blob:none "file://$(pwd)/server_repo" local_repo
cd local_repo

# List missing objects from master.
echo "# Missing objects after --no-checkout"
git rev-list --all --quiet --objects --missing=print
echo

echo "# Git checkout fails without internet"
mv ../server_repo ../server_repo.off
! git checkout master
echo

echo "# Git checkout fetches the missing file from internet"
mv ../server_repo.off ../server_repo
git checkout master -- d1/a
echo

echo "# Missing objects after checking out d1/a"
git rev-list --all --quiet --objects --missing=print

GitHub上游。

Git v2.19.0中的输出:

# List and identify all objects
c6fcdfaf2b1462f809aecdad83a186eeec00f9c1
fc5e97944480982cfc180a6d6634699921ee63ec
7251a83be9a03161acde7b71a8fda9be19f47128
62d67bce3c672fe2b9065f372726a11e57bade7e
b64bf435a3e54c5208a1b70b7bcb0fc627463a75 d1
308150e8fddde043f3dbbb8573abb6af1df96e63 d1/a
f70a17f51b7b30fec48a32e4f19ac15e261fd1a4 d1/b
84de03c312dc741d0f2a66df7b2f168d823e122a d2
0975df9b39e23c15f63db194df7f45c76528bccb d2/a
41484c13520fcbb6e7243a26fdb1fc9405c08520 d2/b
7d5230379e4652f1b1da7ed1e78e0b8253e03ba3 master
8b25206ff90e9432f6f1a8600f87a7bd695a24af master/master
ef29f15c9a7c5417944cc09711b6a9ee51b01d89
19f7a4ca4a038aff89d803f017f76d2b66063043 mybranch
1b671b190e293aa091239b8b5e8c149411d00523 mybranch/mybranch
c3760bb1a0ece87cdbaf9a563c77a45e30a4e30e
a0234da53ec608b54813b4271fbf00ba5318b99f root
93ca1422a8da0a9effc465eccbcb17e23015542d root/root
master commit SHA: fc5e97944480982cfc180a6d6634699921ee63ec
mybranch commit SHA: fc5e97944480982cfc180a6d6634699921ee63ec
040000 tree b64bf435a3e54c5208a1b70b7bcb0fc627463a75    d1
040000 tree 84de03c312dc741d0f2a66df7b2f168d823e122a    d2
040000 tree 7d5230379e4652f1b1da7ed1e78e0b8253e03ba3    master
040000 tree 19f7a4ca4a038aff89d803f017f76d2b66063043    mybranch
040000 tree a0234da53ec608b54813b4271fbf00ba5318b99f    root

# Missing objects after --no-checkout
?f70a17f51b7b30fec48a32e4f19ac15e261fd1a4
?8b25206ff90e9432f6f1a8600f87a7bd695a24af
?41484c13520fcbb6e7243a26fdb1fc9405c08520
?0975df9b39e23c15f63db194df7f45c76528bccb
?308150e8fddde043f3dbbb8573abb6af1df96e63

# Git checkout fails without internet
fatal: '/home/ciro/bak/git/test-git-web-interface/other-test-repos/partial-clone.tmp/server_repo' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

# Git checkout fetches the missing directory from internet
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1/1), 45 bytes | 45.00 KiB/s, done.
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1/1), 45 bytes | 45.00 KiB/s, done.

# Missing objects after checking out d1
?f70a17f51b7b30fec48a32e4f19ac15e261fd1a4
?8b25206ff90e9432f6f1a8600f87a7bd695a24af
?41484c13520fcbb6e7243a26fdb1fc9405c08520
?0975df9b39e23c15f63db194df7f45c76528bccb

结论:除d1/a外,其余斑点均缺失。例如f70a17f51b7b30fec48a32e4f19ac15e261fd1a4, d1/b,签出d1/后就没有了。

注意,root/root和mybranch/mybranch也丢失了,但是——depth 1从丢失的文件列表中隐藏了它们。如果你删除-depth 1,那么它们会显示在丢失文件列表中。