简体   繁体   English

Git相当于hg更新

[英]Git equivalent to hg update

Below is a small example with Mercurial and similarly with Git. 以下是Mercurial的一个小例子,与Git类似。 I cannot understand how to make a hg update with Git: 我无法理解如何使用Git进行hg update

I have a small Mercurial setup with 4 commits - where I step back one commit 我有一个小的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`

This gives this picture 这给了这张照片

THG

Note that I see all four commit - ie both the pre-history and the following commit(s) 请注意,我看到所有四个提交 - 即前历史和后续提交

Let me try to do the same example using Git 让我尝试使用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]

Let me see the commits: 让我看看提交:

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>

Good - four commits as expected. 好 - 按预期四次提交。 Let me go back one commit (similar as hg update) 让我回去一次提交(类似于hg更新)

git checkout 724a493

What about the git log now? 现在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>

gitk will also just show the first 3 commits? gitk还会显示前3个提交?

So "git checkout" is NOT just similar to "hg update". 所以,“混帐结账” 不仅仅是类似“汞柱更新”。 Where are the following commit(s)? 以下提交在哪里?

hg update -r 3 is giving you an automatic branch head to which you could continue committing. hg update -r 3为您提供了一个可以继续提交的自动分支头。 In git, checkout takes you to the right commit, but doesn't give you a new branch head. 在git中, checkout会将您带到正确的提交,但不会为您提供新的分支头。 If you want that you can say 如果你想要,你可以说

git checkout -b new_branch_name 724a493

of course using whatever you like as the name. 当然使用你喜欢的任何名称。 If you don't use -b , as in your question, you get into the state of a detached HEAD ... which is exactly the difference between hg update -r 3 and git checkout 724a493 . 如果你不使用-b ,就像你的问题一样,你会进入一个分离的HEAD的状态......这正是hg update -r 3git checkout 724a493之间的区别。 Note the message Git prints when you do your checkout (from me running your example): 注意结帐时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

To put it simply, the 57bb375 commit did not go anywhere when you ran git checkout 724a493 . 简单地说,当你运行git checkout 724a493时, 57bb375提交没有去任何地方。 The behavior you're seeing is simply that git log only shows commits that are ancestors of the checked out commit. 您看到的行为只是git log只显示作为签出提交的祖先的提交。

In Mercurial terms, git log is the same as 在Mercurial术语中, git log

$ hg log -r ::.

which means "show me commits that are ancestors of . , ie, the working copy parent revision". 这意味着“向我展示作为.祖先的提交,即工作副本父版本”。 To get the equivalent of hg log in Git, simply run 要在Git中获得等效的hg log ,只需运行即可

$ git log --all

This little difference is a key feature for Git since it allows you to pull in commits from lots of other repositories without seeing them by default. 这个小差异是Git的一个关键特性 ,因为它允许您从许多其他存储库中提取提交,而不会默认情况下看到它们。 It is only if you checkout a branch (or a commit in your case) that you will see the commits you've downloaded into your repository. 只有当您签出分支(或您的案例中的提交)时,您才会看到已下载到存储库中的提交。

Another solution variant: Adding 另一种解决方案:添加

[alias]
    restore = "!f() { git checkout $(git rev-list -n 1 HEAD -- $1)~1 -- $(git diff --name-status $(git rev-list -n 1 HEAD -- $1)~1 | grep ^D | cut -f 2); }; f"

then "git restore" will move me one commit backwards in time without requiring me to make an artificial branch. 那么“git restore”会让我向后移动一个提交,而不需要我做一个人工分支。

If I want to return to the master branch 如果我想返回主分支

git checkout master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM