方法一
http://stackoverflow.com/questions/25815202/git-fetch-a-single-commit
The
git fetch
command delivers
references
(names, not raw commit-IDs) to the remote, more or less.
(More specifically, use
git ls-remote
remotename
to see what the remote is willing to give you in terms of names.
This produces a list of SHA-1s on the left with names on the right, and the only thing your
fetch
can
ask
for is the names-on-the-right.
At which point you’ll get the ID-on-the-left
if
the name on the remote still points to that ID, so it depends on how actively that remote gets updated.)
It is possible, in various ways, to deliver raw commit-IDs to a remote and ask that remote what is visible starting from that point,
and sometimes working backwards through history as well, but not via
git fetch
.
(You can use
git archive
but the remote can decide whether or not to allow you to access via raw commit-IDs;
or with remotes that have web server access, including to specific commits, you can often just view the top-level contents of a commit,
and use that to “drill down”, as they say, to the various pieces. But that is a very slow way to do it.)
If you’d like to use
git fetch
to get some particular commit, probably the easiest way to do that is to have someone with access to the remote attach a name—most likely a tag—to that commit ID.
Then you can have your
git fetch
bring over that refspec, and put it under any other refspec you like.
For instance, suppose you can ssh directly to whatever hosts
origin
:
$ ssh our.origin.host 'cd /repos/repo.git; git tag temporary f1e32e1'
[enter password, etc; observe tag created]
$ git fetch origin refs/tags/temporary:refs/heads/newbranch
[observe fetch happen; now you have local branch 'newbranch']
$ ssh our.origin.host 'cd /repos/repo.git; git tag -d temporary'
Note that the name need not be a branch, it need only be a reference you can pull over with
git fetch
and see with
git ls-remote
.
You then use a name that will match that on the left-hand-side of your refspec when fetching.
The name created in
your
repo is controlled by the right-hand-side of the refspec (
refs/heads/newbranch
in the example above).
This is also the answer to your last paragraph question: you can only name things that have names on the remote (this is partly intended to avoid “leaking” unnamed commits that remain in a repository before garbage-collection, so it’s considered a feature rather than a bug). These names go on the LHS of the refspec. Your own names go on the right.
Your name on the right is assumed to be a branch or tag name (based on what the name on the left matches, though you can explicitly spell out
refs/heads/
or
refs/tags/
to override it), so even though
f1e32e1...
is a valid SHA-1, it’s treated as a branch name here—the missing name on the left translates to
HEAD
, as missing names almost always do—and
git fetch
creates a branch whose name is disturbingly SHA-1-ish. (Incidentally I once created a branch name that looked like an SHA-1, and later confused myself. I forget exactly what the name was, something like
de-bead
without the hyphen. I renamed it to the hyphenated version just to make it clear I didn’t mean a raw commit ID! 🙂 )
貌似是无解的,远端必须给第一个commit起了名字,或者创建分支,或者Tag
才能通过fetch来获取
1. 本地初始化一个版本库
git init
2.添加远端
git remote add origin https://github.com/git/git.git
3.参照上面的链接处理
git ls-remote origin >origin.txt
把git ls-remote origin的结果输出到origin.txt的文件中
d6602ec5194c87b0fc87103ca4d67251c76f233a
refs/tags/v0.99
$ git pull origin v0.99
remote: Counting objects: 4508, done.
remote: Compressing objects: 100% (234/234), done.
emote: Total 4508 (delta 1498), reused 1409 (delta 1409), pack-reused 2865R
Receivjects: 100% (4508/4508), 980.00 KiB | 334.00 KiB/s
ing objects: 100% (4508/4508), 1.08 MiB | 334.00 KiB/s, done.
Resolving deltas: 100% (3056/3056), done.
From https://github.com/git/git
* tag v0.99 -> FETCH_HEAD
error: Trying to write non-commit object d6602ec5194c87b0fc87103ca4d67251c76f233
a to branch refs/heads/master
fatal: Cannot update the ref ‘HEAD’.
或者
git fetch origin v0.99
这个命令不会出错,但是本地没有代码,也找不到方法把代码弄出来。【因为本地一次提交都没有,是没有HEAD的】
$ git rev-list FETCH_HEAD –count
1076
//在版本0.99的时候,一共有1076次提交
$ git reset –hard d6602ec5194c87b0fc87103ca4d67251c76f233a 【这个也没啥用】
HEAD is now at a3eb250 [PATCH] alternate object store and fsck
//HEAD的指向和master的指向是一致的
8d530c4d64ffcc853889f7b385f554d53db375ed HEAD
//5个分支
ee6ad5f4d56e697c972af86cbefdf269b386e470 refs/heads/maint
8d530c4d64ffcc853889f7b385f554d53db375ed refs/heads/master
c07a1e8782dadcedeffd389aa9bce4fda5b0983c refs/heads/next
9b9e9adbccf975e4ffc7af213fbf55f187e752bf refs/heads/pu
49a1e5ee48904bfe562388041bdcdb3d8ad21d10 refs/heads/todo
//这些可能是合并记录
f0d0fd3a5985d5e588da1e1d11c85fba0ae132f8 refs/pull/10/head
3fed6331a38d9bb19f3ab72c91d651388026e98c refs/pull/10/merge
d604669e32e847c2ba5010c89895dd707ba45f55 refs/pull/100/head
1101dcb2eb2a20bb8009422d9760cd475c1b4590 refs/pull/100/merge
5867fc2ad19dd9dc49a9bde7704cf9cf7e539a78 refs/pull/101/head
a1fb527ff90f5df3bfb5d46e8ec5d05c66a1c824 refs/pull/101/merge
//这一串看起来似乎也是分支
6e41cb3e3fed66e67c91f92710be792ee001702a refs/remotes/github/html
cd2b8ae983a277fb3f3c2b2c6747b0a075af1421 refs/remotes/github/maint
e3cf854918ce6092220c9e9d43802b117618d3b5 refs/remotes/github/man
4b5eac7f03f411f75087e0b6db23caa999624304 refs/remotes/github/master
3be2039f06f131442a1ff8fe0759f05176d17143 refs/remotes/github/next
b4bcbacefc5233afca24b70b29448373293c8a4f refs/remotes/github/pu
4bee7490f9dab6e968086f7c3f1f2cc02caf91f3 refs/remotes/github/todo
4b5eac7f03f411f75087e0b6db23caa999624304 refs/remotes/origin/HEAD
5723afaf3a61cef537e1f4dfa88f8faf31060ef0 refs/remotes/origin/html
b15b5b10a75e202f3f90e000925545243267cbee refs/remotes/origin/maint
e193e4189407968b3c46992e3eac60d38424286e refs/remotes/origin/man
4b5eac7f03f411f75087e0b6db23caa999624304 refs/remotes/origin/master
5a144a28891926e235a49e4850b4ec5edb6db2cc refs/remotes/origin/next
55af6ac9def8b4fc2f65f627a84451d3bf17aa12 refs/remotes/origin/pu
4bee7490f9dab6e968086f7c3f1f2cc02caf91f3 refs/remotes/origin/todo
//gitgui tag
d5aef6e4d58cfe1549adef5b436f3ace984e8c86 refs/tags/gitgui-0.10.0
3d654be48f65545c4d3e35f5d3bbed5489820930 refs/tags/gitgui-0.10.0^{}
33682a5e98adfd8ba4ce0e21363c443bd273eb77 refs/tags/gitgui-0.10.1
729ffa50f75a025935623bfc58d0932c65f7de2f refs/tags/gitgui-0.10.1^{}
ca9b793bda20c7d011c96895e9407fac2df9648b refs/tags/gitgui-0.10.2
95dcfa3633004da0049d3d0fa03f80589cbcaf31 refs/tags/gitgui-0.10.2^{}
8c178f72b54f387b84388d093a920ae45b8659dd refs/tags/gitgui-0.11.0
1c1fe1005c9dacc05a34eb892ae20ebb1904a33b refs/tags/gitgui-0.11.0^{}
//junio tag
3a1c74404d544d20c0d1f0b9c072b471a20b04c0 refs/tags/junio-gpg-pub
7214aea37915ee2c4f6369eb9dea520aec7d855b refs/tags/junio-gpg-pub^{}
//v tag
d6602ec5194c87b0fc87103ca4d67251c76f233a refs/tags/v0.99
a3eb250f996bf5e12376ec88622c4ccaabf20ea8 refs/tags/v0.99^{}
f25a265a342aed6041ab0cc484224d9ca54b6f41 refs/tags/v0.99.1
78d9d414123ad6f4f522ffecbcd9e4a7562948fd refs/tags/v0.99.1^{}
c5db5456ae3b0873fc659c19fafdde22313cc441 refs/tags/v0.99.2
2779fad61302da0fe3a24995e343ab894f32fa42 refs/tags/v0.99.2^{}
方法二
仅仅检出Tag所对应的commit
http://stackoverflow.com/questions/20280726/how-to-git-clone-a-specific-tag
链接中的第二个方法
本地只有一个commit,无法用git status,git log,git branch
$ git clone -b v0.99 –depth 1 https://github.com/git/git.git
Cloning into ‘git’…
remote: Counting objects: 243, done.
remote: Compressing objects: 100% (237/237), done.
rRemote: Total 243 (delta 17), reused 69 (delta 5), peceiving objects: 96% (234
Receiving objects: 98% (239/243), 220.00 KiB | 191.00 KiB/s
Receiving objects: 100% (243/243), 323.47 KiB | 191.00 KiB/s, done.
Resolving deltas: 100% (17/17), done.
Checking connectivity… done.
Note: checking out ‘a3eb250f996bf5e12376ec88622c4ccaabf20ea8’.
You are in ‘detached HEAD’ state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
===================方法二=============
方法三
http://stackoverflow.com/questions/20280726/how-to-git-clone-a-specific-tag
链接中的第一个方法
git clone –branch v0.99 https://github.com/git/git.git git5
这个不太可行,是先clone,然后再自动切换,还是把整个版本库弄下来了
===================方法三=============
方法四
http://stackoverflow.com/questions/791959/download-a-specific-tag-with-git
mkdir git6
cd git6
git init
git remote add origin https://github.com/git/git.git
提取Tag
d6602ec5194c87b0fc87103ca4d67251c76f233a
refs/tags/v0.99
git fetch origin v0.99
$ git fetch origin v0.99
remote: Counting objects: 4508, done.
remote: Compressing objects: 100% (234/234), done.
rRemote: Total 4508 (delta 1498), reused 1409 (delta 1409), pack-reused 2865ecei
R
Receiving objects: 100% (4508/4508), 1.08 MiB | 503.00 KiB/s, done.
Resolving deltas: 100% (3056/3056), done.
From https://github.com/git/git
* tag v0.99 -> FETCH_HEAD
git tag v0.99 FETCH_HEAD
之后
使用tortoisegit
切换到Tag v0.99
在这个目录下,各种命令好像都无法执行了。命令和文件的名字貌似重复了
先用tortoisegit在第一个commit上创建分支first 【其实可以直接创建成master】
切换到first分支,然后重命名分支为master
git branch -m first master
===2015年10月18日 凌晨一点更新===
实战二:linux系统的代码获取
1.在github上新建一个版本库LinuxStudy
2.然后clone到本地
3.git remote add torvalds https://github.com/torvalds/linux.git
4.git ls-remote origin >origin.txt
定位到的最小的Tag是 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c
refs/tags/v2.6.11
5.
git fetch torvalds v2.6.11
remote: Counting objects: 18014, done.
remote: Compressing objects: 100% (4917/4917), done.
remote: Total 18014 (delta 484), reused 73 (delta 73), pack-reused 13024 eceivin
g objects: 100% (18014/18014), 49.82 MiB | 369.00 KiB/s
Receiving objects: 100% (18014/18014), 49.84 MiB | 369.00 KiB/s, done.
Resolving deltas: 100% (1699/1699), done.
From https://github.com/torvalds/linux
* tag v2.6.11 -> FETCH_HEAD
6.
git tag v2.6.11 FETCH_HEAD
尝试
checkout这个tag失败
重新fetch v2.6.12
$ git fetch torvalds v2.6.12
remote: Counting objects: 31617, done.
remote: Total 31617 (delta 0), reused 0 (delta 0), pack-reused 31616 eceiving ob
jects: 100% (31617/31617), 53.06 MiB | 273.00
Receiving objects: 100% (31617/31617), 53.13 MiB | 265.00 KiB/s, done.
Resolving deltas: 100% (13062/13062), done.
From https://github.com/torvalds/linux
* tag v2.6.12 -> FETCH_HEAD
$ git checkout v2.6.12
Checking out files: 100% (17360/17360), done.
Note: checking out ‘v2.6.12’.
You are in ‘detached HEAD’ state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 9ee1c93… Linux 2.6.12