Git相当于hg更新

Pet*_*oft 7 git version-control mercurial

以下是Mercurial的一个小例子,与Git类似.我无法理解如何hg update使用Git:

我有一个小的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`
Run Code Online (Sandbox Code Playgroud)

这给了这张照片

THG

请注意,我看到所有四个提交 - 即前历史和后续提交

让我尝试使用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]
Run Code Online (Sandbox Code Playgroud)

让我看看提交:

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>
Run Code Online (Sandbox Code Playgroud)

好 - 按预期四次提交.让我回去一次提交(类似于hg更新)

git checkout 724a493
Run Code Online (Sandbox Code Playgroud)

现在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>
Run Code Online (Sandbox Code Playgroud)

gitk还会显示前3个提交?

所以,"混帐结账"的只是类似于"汞更新".以下提交在哪里?

Wol*_*olf 7

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

git checkout -b new_branch_name 724a493
Run Code Online (Sandbox Code Playgroud)

当然使用你喜欢的任何名称.如果你不使用-b,在你的问题,你进入一个分离的头......这正是之差的状态hg update -r 3git checkout 724a493.注意结帐时Git打印的消息(从我运行你的例子):

Note: checking out '2ffb5e0'.

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 2ffb5e0... 3. commit
Run Code Online (Sandbox Code Playgroud)