如何稀疏地从git存储库中只签出一个文件?
如何从git repo中只签出一个文件?首先使用-n选项克隆repo,该选项禁止对所有文件进行默认签出,--depth 1选项,这意味着它只获取每个文件的最新版本如何稀疏地从git存储库中只签出一个文件?,git,file,git-checkout,sparse-checkout,Git,File,Git Checkout,Sparse Checkout,如何从git repo中只签出一个文件?首先使用-n选项克隆repo,该选项禁止对所有文件进行默认签出,--depth 1选项,这意味着它只获取每个文件的最新版本 git clone -n git://path/to/the_repo.git --depth 1 然后只签出您想要的文件,如下所示: cd the_repo git checkout HEAD name_of_file 这听起来像是你试图从集中式版本控制中继承一个想法,而git本质上并不是,它是分布式的。如果您想使用git存储库
git clone -n git://path/to/the_repo.git --depth 1
然后只签出您想要的文件,如下所示:
cd the_repo
git checkout HEAD name_of_file
这听起来像是你试图从集中式版本控制中继承一个想法,而git本质上并不是,它是分布式的。如果您想使用git存储库,可以克隆它。然后,您将拥有工作树的所有内容和所有历史记录(至少是指向当前分支尖端的所有内容),而不仅仅是单个提交中的单个文件或快照
git clone /path/to/repo
git clone git://url/of/repo
git clone http://url/of/repo
在git中,您在更新文件之前不会“签出”文件——看起来这就是您所追求的 许多系统,如clearcase、csv等,都要求您先“签出”文件,然后才能对其进行更改。Git不需要这样做。克隆存储库,然后在存储库的本地副本中进行更改 更新文件后,您可以执行以下操作:
git status
查看已修改的文件。您首先使用(index
类似于要签入的列表)添加要提交到index
的内容:
或
然后dogit status
将向您显示哪些文件已被修改,哪些文件已在索引中
准备提交或签入
要将文件提交到存储库副本,请执行以下操作:
git commit -a -m "commit message here"
有关手册和指南的链接,请参见。最初,我在2012年提到过(请参见和),因为: 但是:在2013年,这已经不可能了。
请参阅旧页“” 当前(2018)页面“”建议使用第三方服务,如或
因此,您还可以处理本地副本/克隆: 如果您有如中所述的裸存储库的本地副本,您也可以执行以下操作:
或者您必须首先克隆回购协议,这意味着您将获得完整的历史记录: -在.git回购协议中 -在工作树上
- 但是,您可以执行一个(如果您使用的是Git1.7+):
- 启用稀疏签出选项(
)git config core.sparsecheckout true
- 添加希望在
文件中看到的内容.git/info/sparse checkout
- 重新读取工作树以仅显示所需内容
- 启用稀疏签出选项(
$ git read-tree -m -u HEAD
这样,您将得到一个工作树,其中精确地包含您想要的内容(即使它只是一个文件)
点()到“” 一个bash函数,用于避免下载历史记录、检索单个分支以及检索所需文件或目录的列表 在GIT 1.7.2.2中工作 例如,您有一个远程一些带有分支branch1,branch32 因此,要签出特定文件,请调用以下命令:
git checkout remote/branch path/to/file
例如,它将是这样的
git checkout some_remote/branch32 conf/en/myscript.conf
git checkout some_remote/branch1 conf/fr/load.wav
这个checkout命令将把整个文件结构conf/en和conf/fr复制到当前目录中,您可以在其中调用这些命令(当然,我假设您以前在某个时候运行过git init)git checkout branch\u或\u version--path/file
示例:
git checkout HEAD--main.c
如果您已经有了git repo的副本,您可以始终使用git日志
签出文件的一个版本,以找出散列id(例如3CDC61015724F9965575BA954C8CD422C8B42E4),然后您只需键入:
git checkout hash-id path-to-file
下面是一个实际的例子:
git checkout 3cdc61015724f9965575ba954c8cd4232c8b42e4 /var/www/css/page.css
通常,如果不按照第一个答案中的建议下载整个存储库,就不可能从
git
下载一个文件。
这是因为Git并不像您所想的那样(像CVS/SVN那样)存储文件,但它根据项目的整个历史生成文件
但对于具体的案例,有一些变通办法。下面的示例带有占位符,用于用户
,项目
,分支
,文件名
GitHub
wget https://raw.githubusercontent.com/user/project/branch/filename
wget https://gitlab.com/user/project/raw/branch/filename
GitLab
wget https://raw.githubusercontent.com/user/project/branch/filename
wget https://gitlab.com/user/project/raw/branch/filename
GitWeb
wget https://raw.githubusercontent.com/user/project/branch/filename
wget https://gitlab.com/user/project/raw/branch/filename
如果您正在使用,则可以在示例中尝试(将其更改为正确的路径):
drupalcode.org上的GitWeb
例如:
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界面,您可以考虑将代码推到外部服务,例如,。把它当作一面镜子
如果您没有安装
wget
,请另外尝试curl-O(url)
。现在我们可以了由于这是谷歌上的第一个结果,我想我应该更新到最新的状态。随着git 1.7.9.5的出现,我们有了git archive
命令,它允许您从远程主机检索单个文件
git archive --remote=git://git.foo.com/project.git HEAD:path/in/repo filename | tar -x
请参见此处的完整答案关于已给出内容的两种变体:
git archive --format=tar --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -O -xf -
以及:
它们将文件写入标准输出。如果您只需要下载文件,无需使用Git签出 更容易做到这一点,它是一个Chrome扩展,可以让你点击文件图标下载它。你也可以通过
git archive --format=tar --remote=origin HEAD | tar xf -
git archive --format=tar --remote=origin HEAD <file> | tar xf -
git-archive--format=tar--remote=origin-HEAD | tar-xf-
git存档--格式=tar--远程=源文件头| tar xf-
非常简单:
git checkout from-branch-name -- path/to/the/file/you/want
这将不会从分支名称签出。您将停留在您所在的任何分支上,并且只有该文件将从指定的分支签出
以下是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.
git签出[-p|--patch][[-]。。。
当给出或--patch时,git checkout不会切换
分支机构。It更新
git checkout from-branch-name -- path/to/the/file/you/want
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.
git checkout --theirs 123.txt
git checkout --theirs "A/123.txt"
git archive --remote=git://git.example.com/project.git refs/heads/mybranch path/to/myfile |tar xf -
git archive --remote=git://git.example.com/project.git HEAD path/to/myfile |tar xf -
git archive --remote=git://git.example.com/project.git mytag path/to/myfile |tar xf -
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
#!/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
# 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
https://ourArchive.ourCompany.com/projects/ThisProject/repos/data/raw/foo/bar.spec?at=refs%2Fheads%2FTheBranchOfInterest
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
https://github.com/hubotio/hubot/blob/ed25584f/src/adapter.coffee
https://bitbucket.org/cofarrell/stash-browse-code-plugin/src/06befe08
git clone --no-checkout <git url>
git reset
git checkout origin/master <path to file>
git add <path to file>
git commit -m <message text>
git push