Git相当于hg更新

Git相当于hg更新,git,version-control,mercurial,Git,Version Control,Mercurial,下面是Mercurial和Git的一个小例子。 我无法理解如何使用Git进行hg更新: 我有一个小型Mercurial设置,有4次提交,其中我后退一次提交 hg init echo "1" > a.txt; hg commit -A -m "1. commit" a.txt echo "2" >> a.txt; hg commit -m "2. commit" a.txt echo "3" >> a.txt; hg commit -m "3. commit" a.t

下面是Mercurial和Git的一个小例子。 我无法理解如何使用Git进行
hg更新

我有一个小型Mercurial设置,有4次提交,其中我后退一次提交

hg init
echo "1" > a.txt; hg commit -A -m "1. commit" a.txt
echo "2" >> a.txt; hg commit -m "2. commit" a.txt
echo "3" >> a.txt; hg commit -m "3. commit" a.txt
echo "4" >> a.txt; hg commit -m "4. commit" a.txt
hg update -r 3
thg # or hg view`
这就是这张照片

请注意,我看到了所有四个提交-即前历史记录和以下提交

让我试着用Git做同样的例子

git init
echo "1" > a.txt; git add a.txt; git commit  -m "1. commit" a.txt
echo "2" >> a.txt; git commit -m "2. commit" a.txt
echo "3" >> a.txt; git commit -m "3. commit" a.txt
echo "4" >> a.txt; git commit -m "4. commit" a.txt # gives for me [master 57bb375]
让我看看下面的例子:

git log --graph --pretty=format:'%h -%d %s (%cr) <%an>' 

 * 57bb375 - (HEAD, master) 4. commit (14 minutes ago) <Peter Toft>
 * 724a493 - 3. commit (14 minutes ago) <Peter Toft>
 * bb38732 - 2. commit (14 minutes ago) <Peter Toft>
 * 879c593 - 1. commit (15 minutes ago) <Peter Toft>
现在git日志呢

git log --graph --pretty=format:'%h -%d %s (%cr) <%an>' 

 * 724a493 - (HEAD) 3. commit (19 minutes ago) <Peter Toft>
 * bb38732 - 2. commit (19 minutes ago) <Peter Toft>
 * 879c593 - 1. commit (19 minutes ago) <Peter Toft>
git log--graph--pretty=格式:“%h-%d%s(%cr)”
*724a493-(头部)3。提交(19分钟前)
*bb38732-2。提交(19分钟前)
*879c593-1。提交(19分钟前)
gitk还会显示前3个提交吗


所以“git checkout”是而不是,与“hg update”类似。以下提交在哪里?

hg update-r 3
为您提供了一个可以继续提交的自动分支头。在git中,
checkout
将您带到正确的提交,但不会给您一个新的分支头。如果你想,你可以说

git checkout -b new_branch_name 724a493
当然可以用你喜欢的名字。如果你不使用
-b
,就像你的问题一样,你会进入头部分离的状态。。。这正是
hg update-r3
git checkout 724a493
之间的区别。请注意,当您签出时Git会打印一条消息(来自运行示例的我):

另一种解决方案变体: 添加

然后,“git恢复”将使我在时间上向后提交一次,而不需要我进行人工分支

如果我想回到主分支

git checkout master

简单地说,当您运行git checkout 724a493时,
57bb375
commit没有到达任何地方。您看到的行为很简单,
git log
只显示作为签出提交祖先的提交

在Mercurial术语中,
git log

$ hg log -r ::.
这意味着“向我显示
的祖先提交,即工作副本父版本”。要在Git中获得等价的
hg log
,只需运行

$ git log --all

这一点差异是Git的一个关键特性,因为它允许您从许多其他存储库中拉入提交,而不会在默认情况下看到它们。只有签出一个分支(或者您的案例中的一个提交),您才会看到下载到存储库中的提交。

您可以使用
git checkout“HEAD^”
签出以前的提交。好的一个和“gitk--all”确实显示了将来的提交(就像您的git日志--all)Commit 57bb375仍在
master
的顶端。要取回它,您只需
git checkout master
。如果你想把它放到你刚刚创建的新分支上,你可以
git merge master
git cherry pick 57bb375
,同时在新分支上。
git checkout master
$ hg log -r ::.
$ git log --all