GIT 返回特定提交 ID,而不删除历史记录
这是我的提交日志,我想切换回特定的提交 ID(例如 Second), 当我使用 git checkout 时,没问题,但是,我无法再切换回最后一次提交(第四次)。
HEAD 指向第二次提交,当我记录我的提交时,此后就没有任何内容了。
如何在不删除历史记录的情况下在提交之间切换?
commit 61c71a9e5a6d9e29a4172e687172dd4b8523eb4a (HEAD -> main)
Author: mohhhe <[email protected]>
Date: Fri Feb 25 19:08:36 2022 +0330
Fourth
commit 9c3e8919cfa2c970f14056eef34ca12b49025f65
Author: mohhhe <[email protected]>
Date: Fri Feb 25 19:08:13 2022 +0330
Third
commit d33795596001197f382038a72d20faf0cfbe7ab7
Author: mohhhe <[email protected]>
Date: Fri Feb 25 19:07:55 2022 +0330
Second
commit 2fe7b1d8270fcfb41d73e69293da10734e37b069
Author: mohhhe <[email protected]>
Date: Fri Feb 25 19:07:39 2022 +0330
First
Here is my commit log and I want to switch back to a specific commit id (for example Second),
When I use git checkout , it is ok but, I am no longer able to switch back to the last commit (Fourth).
HEAD points to the second commit and there is nothing after that when I log my commits.
How can I switch between my commits without deleting the history?
commit 61c71a9e5a6d9e29a4172e687172dd4b8523eb4a (HEAD -> main)
Author: mohhhe <[email protected]>
Date: Fri Feb 25 19:08:36 2022 +0330
Fourth
commit 9c3e8919cfa2c970f14056eef34ca12b49025f65
Author: mohhhe <[email protected]>
Date: Fri Feb 25 19:08:13 2022 +0330
Third
commit d33795596001197f382038a72d20faf0cfbe7ab7
Author: mohhhe <[email protected]>
Date: Fri Feb 25 19:07:55 2022 +0330
Second
commit 2fe7b1d8270fcfb41d73e69293da10734e37b069
Author: mohhhe <[email protected]>
Date: Fri Feb 25 19:07:39 2022 +0330
First
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
小巷类比
想象一下,您正处于一座大城市的一条狭窄街道或小巷的入口处,周围都是摩天大楼。沿着小巷往下看,你可以看到一系列的垃圾箱。现在,沿着小巷走到一半,看看前方。一半的垃圾箱不见了!他们去哪儿了?无处:他们就在你身后。
同样的想法也适用于此:Git 不会删除您看不到的提交。您只是看不到它们。移回到您可以看到它们的有利位置,您将再次看到它们。
现实,就像在 Git 中一样
,提交是一个由两部分组成的实体:它保存所有文件的快照 - 好吧,是 Git 知道的所有文件,当时你(或任何人) )制作了该快照以及一些元数据。每个提交都会编号,并带有一个又大又丑的随机哈希 ID,如输出中所示的
61c71a9e5a6d9e29a4172e687172dd4b8523eb4a
。哈希 ID 是 Git 查找提交所需的 ID。提交本身使用提交对象和其他内部支持对象存储为一堆部分。您在此处看到的哈希 ID 是提交对象本身的哈希 ID,它仅保存元数据:快照位于一个树对象中,该对象还有更多子对象。但您通常不需要知道这一点;您确实需要知道的是,Git 有一个包含所有对象的大型数据库,每个对象都有编号,并且 Git 本身需要该编号来检索对象.1
然而,人类在数字方面非常糟糕。这四个哈希 ID 又是什么?无论如何,不值得记住它们。 Git 为您提供了一种非常快速的方法来查找这四个哈希 ID 中的一个:名称
main
,您很容易记住,可以找到一个 > 这些哈希 ID。随着时间的推移,
main
找到的一个哈希 ID 可能会发生变化,但现在,它会为您和 Git 找到61c71a9e5a6d9e29a4172e687172dd4b8523eb4a
。该提交是分支main
上的最新提交。这是根据定义,因为名称main
包含该ID。因此,如果您希望 Git 找到main
上的最新提交,您只需向 Git 询问main
,Git 就会查找名称< code>main 并找到该 ID,从而找到该提交。如果当您进行新提交时,Git 将执行以下操作(以某种顺序;您实际上无法看到是否有任何特定顺序):
Make a Git 知道的每个文件的快照。要使 Git 看到您对 Git 已经知道的文件所做的任何更新,您必须对其运行 git add 。要使 Git 看到之前不存在的您创建的新文件,您必须对其运行
git add
。事情远不止这些,但这就是您必须继续运行 git add 的第一个近似原因:告诉 Git 新快照应该使用 < em>新的或更新的文件。收集大量元数据。 Git 将收集的元数据包括您的姓名(在
user.name
设置中设置)和电子邮件地址(来自您的user.email
设置)。它包括精确到秒的当前日期和时间。而且,它包括您所在分支上的当前最新提交,无论是什么,在本例中为main
。Git 将所有这些写出来以进行新提交,这将获得一个新的、唯一的、以前从未使用过、永远不会再使用的哈希 ID。此哈希 ID 绝不能出现在任何 Git 存储库中,除非用于标识此您刚刚进行的提交。 (这就是哈希 ID 如此大且难看的原因:因此它们可以是唯一的。)
然后 Git 将新提交的哈希 ID 存储在当前分支名称中。所以现在名称
main
选择您的新提交——您刚刚创建的提交。1那是因为这个大数据库是一个键值对store,以哈希 ID 为键。有一种缓慢的方法可以遍历整个数据库并获取每个<键,值>。一对,但是在一个大存储库中这需要很多秒,甚至几分钟:太慢而没有用处。一个键查找需要几毫秒,所以这就是你希望 Git 做的事情。
提交因此形成向后看的链
这一切意味着 name
main
自动且始终选择最后一次提交 > 在名为main
的分支中。根据定义,main
是街道/小巷/高速公路/高速公路/无论它是什么的尽头。您可以通过在这条“道路”上进行新的提交来添加新的提交,从而将“道路”进一步延伸。展示这一点的另一种方法是使用大写字母来绘制提交来代表真实的哈希 ID。在这里,我们有您最初的四个提交,我们将其称为
A
、B
、C
和D
简而言之:name
main
将“指向”(包含其哈希 ID)最后一个提交,即提交D
。提交D
有一个快照(所有文件的副本,永久冻结)和一些元数据,并且D
的元数据表明上一个 提交是提交C
。我们说D
指向C
。Commit
C
当然有快照和元数据。快照保存了 Git 在您创建C
时知道的文件,并永久冻结,元数据保存了日期和时间等,包括早期提交的哈希 ID <代码>B。我们说C
指向B
。提交
B
也保存快照和元数据,并向后指向提交A
,后者保存快照和元数据。但是提交A
是您所做的第一次提交,直到您创建A
为止,这是一个完全空的存储库。所以 commitA
不会进一步向后指向:它不能。这就是您的存储库中的四次提交的情况。他们永远无法改变!它们是完全只读的,并且这四个哈希 ID 现在将永远用完。2名称
main
指向最后一个 — 直到您创建一个新的提交。然后新的提交E
出现,向后指向D
,Git 更新名称main
以指向E
>:2这在技术上是不可能的,Git 并没有真正尝试阻止其他人获得相同的哈希 ID 除非通过使用加密欺骗来使其不太可能我们不必担心。没有人会意外重复使用您的哈希 ID。加密货币也使得故意这样做变得困难。
回到过去
但是当您想要访问旧提交时会发生什么?您运行:
告诉 Git 从您的工作区域中删除永久安全存储在提交
D
中的所有文件,然后返回到提交B
:提取存储的文件-永远将文件从提交B
到您的工作区。 Git 做到了,然后 git log 显示您提交了 B 和 A 并停止了。为什么?Git 使用 HEAD 来查看事物
Git 有一个非常特殊的名称
HEAD
,它根本不是一个分支名称。3相反,此名称HEAD
通常附加到分支名称。这就是您的第一个git log
显示的内容:Git 的名称
HEAD
“指向”这里的名称main
。我喜欢这样画它:名称
HEAD
“附加”到名称main
。 (我也懒得在提交之间绘制箭头。只要记住从 A 到 B 到 C 到 D 的连接线实际上是向后指向的箭头。)运行 git log 告诉 Git:< em>首先,使用
HEAD
查找提交。由于HEAD
附加到main
,Git 使用main< /code> 查找提交
D
。然后,git log
命令会显示您提交的D
,默认情况下会显示它;您可以使用git log
来更改此选项,然后按照D
的箭头返回到C
并显示C< /代码>。然后
git log
跟随C
的箭头到B
,并显示B
,并跟随B< /code> 的箭头指向
A
并显示A
。 CommitA
没有向后箭头,因此git log
终于可以停止了。然而,当您通过哈希 ID
git checkout
提交时,Git 会进入 Git 所谓的分离 HEAD 模式。在这里,名称HEAD
不再附加到分支名称。相反,它直接指向提交。如果您选择提交B
,您会得到以下结果:git log
命令的工作方式与以前一样:它使用HEAD
来查找提交。但这次HEAD
找到提交B
,而不是 namemain
,然后提交D
。因此,git log
显示B
,并按照B
的箭头返回到A
并显示A< /code>,然后用完要显示的提交并停止。
如果您想查看所有提交,您可以:
切换回分支
main
,重新附加您的HEAD
:现在您可以开始
git log
从路的尽头(main
上的最后一次提交)开始,您将看到所有四个提交。 或者,您可以运行:它告诉
git log
它应该使用名称main
来查找要开始的提交。这将找到提交D
,即使HEAD
仍然直接指向提交B
。3技术上可以创建一个名为
HEAD
的分支。不要这样做。多个分支名称
一旦理解了上述内容,您就可以处理多个分支名称了。假设我们有这个:
并且我们创建一个新的名称,例如
develop
,指向提交D
,通过运行:我们现在有这个:
也就是说,两个名称,
develop
和main
,都指向提交D
。不过,特殊名称HEAD
目前已附加到名称main
上。让我们在main
上进行一个新的提交,提交E
,并将其绘制出来:提交
E
现在是 < em>main 上的最新提交,而D
提交仍然是develop
上的最新提交 。如果您现在运行:
或:
切换到分支
develop
,我们会得到:Commit
E
仍然存在,但 Git 将获取所有E
' s 文件移出我们的工作区域,并放入D
的所有文件。名称HEAD
现在附加到名称develop
,而不是名称main
,因此git log
将显示提交D
、C
、B
和A
,然后停止。运行git log main
将显示E
,然后是D
,然后是C
,依此类推。请注意,从
D
开始的提交都在两个分支上。但现在我们处于develop
而不是main
上,让我们进行另一个新的提交:通过
D
提交A
仍在两个分支上,但现在main
和develop
各有一个提交,而另一个分支没有。这两个名称选择最新的提交,即E
和F
。E
是最新的main
分支提交,F
是最新的develop
-分支提交。它们都是“最新提交”!如果我们在
develop
上进行另一个新提交,如下所示:那么两个最新提交现在是
E
和G
。 每个分支名称“意味着”特定的提交,根据定义,这是该分支上的最新提交。此外,所有您(或 Git)可以通过从以下位置开始找到的提交“最新”提交以及向后工作都在该分支“上”。因此,当我们有:我们有三个最新提交,并且通过
H
进行的提交都在所有三个分支上。选择一个名称进行检查,这就是您将通过 git log 看到的一组提交;您工作区中的文件将是来自最新(或提示)提交的文件。请注意,提交永远不会改变:一旦提交,它就永远有效。然而,我们通过分支名称发现提交,并且这些确实移动。如果我们采用最后一个示例并将名称
br2
向后移动一跳:我们可能永远无法再次
找到提交
H,我们也不会丢失L
。它已“丢失”,因为无法恢复其哈希 ID。不过,只要我们能找到J
和K
,即使我们完全删除H
em> 名称main
。删除该名称仅意味着我们不再有直接访问来提交H
:我们必须通过从K
向后退一步来找到它,或者两个来自J
。An alleyway analogy
Imagine, for a moment, that you're at the entrance to a narrow street or alleyway in a big city, surrounded by skyscrapers. Looking down the alleyway, you can see a series of dumpsters. Now, walk halfway down the alleyway and look ahead of you. Half the dumpsters are gone! Where did they go? Nowhere: they're right behind you.
The same idea applies here: Git did not delete the commits you can't see. You just can't see them. Move back to a vantage point from which you can see them, and you'll see them again.
Reality, such as it is
In Git, a commit is a two-part entity: it holds a snapshot of all files—well, all the files that Git knew about, at the time you (or whoever) made that snapshot—and some metadata. Each commit is numbered, with a big, ugly, random-looking hash ID, like
61c71a9e5a6d9e29a4172e687172dd4b8523eb4a
as shown in your output.The hash ID is what Git needs to find the commit. The commit itself is stored as a bunch of parts, using a commit object and other internal supporting objects. The hash ID you see here is that of the commit object itself, which holds only the metadata: the snapshot is in a tree object, which has yet more sub-objects. But you don't normally need to know this; what you do need to know is that Git has a big database holding all of its objects, each of which is numbered, and that Git itself needs the number to retrieve the object.1
Humans, however, are very bad at numbers. What were those four hash IDs again? It's not worth memorizing them anyway though. Git offers you a very fast way to find one of those four hash IDs: the name
main
, which is easy for you to remember, finds one of those hash IDs.Over time, the one hash ID that
main
finds may change, but right now, it finds61c71a9e5a6d9e29a4172e687172dd4b8523eb4a
for you, and for Git. That commit is the latest commit on the branchmain
. It is by definition, because the namemain
holds that ID. So if you want Git to find the latest commit onmain
, you can simply ask Git formain
, and Git will look up the namemain
and find that ID and hence find that commit.If and when you make a new commit, here's what Git will do (in some order or another; you don't really get to see if there's any particular order to this):
Make a snapshot of every file that Git knows about. To make Git see any update you've made to a file that Git already knows about, you must run
git add
on it. To make Git see any new file you created that did not exist until now, you must rungit add
on it. There's a lot more to it than this, but that's the first approximation to the reason you have to keep runninggit add
: to tell Git that the new snapshot should use the new or updated file.Gather up a bunch of metadata. The metadata Git will gather includes your name (as set in your
user.name
setting) and email address (from youruser.email
setting). It includes the current date-and-time down to the second. And, it includes the currently most-recent commit, whatever that is, on the branch you're on—in this casemain
.Git writes all this out to make a new commit, which gains a new, unique, never-used-before, never-will-be-used-again, hash ID. This hash ID must never occur in any Git repository except to be used to identify this commit that you just made right now. (That's why the hash IDs are so big and ugly: so they can be unique.)
Git then stores the new commit's hash ID in the current branch name. So now the name
main
selects your new commit—the one you just made.1That's because this big database is a key-value store, with the hash IDs being the keys. There's a slow method of walking the entire database and getting every <key, value> pair, but this takes many seconds, or even minutes, in a big repository: far too slow to be useful. A key lookup takes milliseconds, so that's what you want Git to be doing.
Commits thus form backwards-looking chains
What this all means is that the name
main
automatically and always selects the last commit in the branch namedmain
. By definition,main
is the end of the street / alleyway / superhighway / motorway / whatever it is. You add new commits by making new commits while you're on that "road", and that extends the "road" a bit further.Another way to show this is to draw the commits using uppercase letters to stand in for the real hash IDs. Here, we have your original four commits, which we'll call
A
,B
,C
, andD
for short:The name
main
will "point to" (contain the hash ID of) the last of these commits, commitD
. CommitD
has a snapshot—a copy of all the files, frozen for all time—and some metadata, andD
's metadata says that the previous commit is commitC
. We say thatD
points toC
.Commit
C
, of course, has a snapshot and metadata. The snapshot holds the files that Git knew about at the time you madeC
, frozen for all time, and the metadata holds the date-and-time and so on, including the hash ID of earlier commitB
. We say thatC
points toB
.Commit
B
holds a snapshot and metadata too, and points backwards to commitA
, which holds a snapshot and metadata. But commitA
was the very first commit you made, in what had been, up until you madeA
, a totally-empty repository. So commitA
doesn't point further backwards: it can't.That's how your four commits are, in your repository. They can never change! They are completely read-only, and those four hash IDs are now used up forever.2 The name
main
points to the last one—until you make a new commit. Then new commitE
springs into being, pointing backwards toD
, and Git updates the namemain
to point toE
:2This is technically impossible, and Git doesn't really try to prevent anyone else from getting the same hash ID except by using cryptographic trickery to make it so unlikely that we don't have to worry about it. Nobody will accidentally re-use your hash IDs. The crypto makes it hard to do it on purpose, too.
Driving back into the past
But what happens when you want to visit an old commit? You ran:
to tell Git to erase, from your work area, all the files that are safely stored forever in commit
D
, and go back to commitB
: extract the stored-forever files from commitB
into your work area. Git did that, and thengit log
showed you commitsB
andA
and stopped. Why?Git uses your HEAD to be able to see things
Git has a very special name,
HEAD
, that is not a branch name at all.3 Instead, this nameHEAD
is normally attached to a branch name. That's what your firstgit log
shows:Git has the name
HEAD
"pointing to" the namemain
here. I like to draw it this way instead:with the name
HEAD
"attached to" the namemain
. (I also got lazy about drawing the arrows between commits. Just remember that the connecting lines, from A to B to C to D, are really backwards-pointing arrows.)Running
git log
tells Git: First, useHEAD
to find a commit. SinceHEAD
is attached tomain
, Git usesmain
to find commitD
. Thegit log
command then shows you commitD
—well, shows it by default; there are options you can givegit log
to change this—and then followsD
's arrow back toC
and showsC
. Thengit log
followsC
's arrow toB
, and showsB
, and followsB
's arrow toA
and showsA
. CommitA
has no backwards arrow, sogit log
can finally stop.When you
git checkout
a commit by its hash ID, however, Git goes into what Git calls detached HEAD mode. Here, the nameHEAD
is no longer attached to a branch name. Instead, it points directly to a commit. If you choose commitB
, you get this:The
git log
command works as before: it usesHEAD
to find a commit. But this timeHEAD
finds commitB
, not namemain
and then commitD
. Sogit log
showsB
, and followsB
's arrow back toA
and showsA
, and then runs out of commits to show and stops.If you want to see all your commits, you can:
which switches back to branch
main
, re-attaching yourHEAD
:and now you're starting
git log
from the end of the road—the last commit onmain
—and you'll see all four commits. Or, you can run:which tells
git log
that it should use the namemain
to look up the commit to start with. This will find commitD
, even thoughHEAD
is still pointing directly to commitB
.3It's technically possible to create a branch named
HEAD
. Don't do it.More than one branch name
Once you understand the above, you're ready to handle multiple branch names. Suppose we have this:
and we create a new name, such as
develop
, pointing to commitD
, by running:We now have this:
That is, both names,
develop
andmain
, point to commitD
. The special nameHEAD
is currently attached to the namemain
though. Let's make a new commit onmain
, commitE
, and draw it in:Commit
E
is now the latest commit onmain
, while commitD
continues to be the latest commit ondevelop
.If you now run:
or:
to switch to branch
develop
, we get:Commit
E
still exists, but Git will take all ofE
's files out of our work area, and put in all ofD
's files instead. The nameHEAD
is now attached to the namedevelop
, not the namemain
, sogit log
will show commitsD
,C
,B
, andA
and then stop. Runninggit log main
will showE
, thenD
, thenC
, and so on.Note that commits up through
D
are on both branches. But now that we're ondevelop
instead ofmain
, let's make another new commit:Commits
A
throughD
are still on both branches, but nowmain
anddevelop
each have one commit that the other branch doesn't have. The two names pick the latest commits, which areE
andF
.E
is the latestmain
-branch commit andF
is the latestdevelop
-branch commit. They're both "the latest commit"! If we make another new commit ondevelop
, like this:then the two latest commits are now
E
andG
. Each branch name "means" that particular commit, which is by definition the latest commit on that branch. Moreover, all the commits you (or Git) can find by starting at that "latest" commit, and working backwards, are "on" that branch. So when we have:we have three latest commits, and commits up through
H
are on all three branches. Pick one name to check out, and that's the set of commits you'll see withgit log
; the files in your work area will be those from that latest—or tip—commit.Note that the commits never change: once you make a commit, it is good forever. However, we find commits through branch names, and those do move about. If we take the last example and move the name
br2
back one hop:we may never be able to find commit
L
again. It has become "lost", as there's no way to recover its hash ID. As long as we can findJ
andK
, though, we can't loseH
, even if we completely delete the namemain
. Deleting that name just means we no longer have direct access to commitH
: we have to find it by working back one step fromK
, or two fromJ
.