Git is equivalent to updating hg

Below is a small example with Mercurial and similarly to Git. I can't figure out how to do hg update using Git:

I have a small Mercurial installation with 4 commits - where I back off one commit

 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` 

It gives this image.

thg

Note that I see all four commits - that is, both the backstory and the next commit (s)

Let me try to do the same example using 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 fix as expected. Let me get back to one commit (similar to updating hg).

 git checkout 724a493 

What about git log?

 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> 

Will gitk also show the first 3 commits?

So, "git checkout" NOT is like "hg update". Where are the following commits?

+6
source share
4 answers

hg update -r 3 gives you an automatic branch branch to which you could continue to commit. In git, checkout takes you to the right, but does not give you a new branch branch. If you want you to be able to say

 git checkout -b new_branch_name 724a493 

Of course, using what you like as a name. If you don't use -b , as in your question, you end up in a detached HEAD state ... which is exactly the difference between hg update -r 3 and git checkout 724a493 . Please note that the Git message prints when you do your check (your example runs from me):

 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 
+7
source

Simply put, fixing 57bb375 failed when you ran git checkout 724a493 . The behavior you see is simply that git log only shows commits that are the ancestors of the checked commit.

In Mercurial terms, git log same as

 $ hg log -r ::. 

which means "to show that I committed the committing being the ancestor . , that is, the parent revision of the working copy." To get the equivalent of hg log in Git, just run

 $ git log --all 

This slight difference is a key feature for Git, as it allows you to commit from many other repositories without seeing them by default. This is only if you check the branch (or commit in your case), in which you will see the commits that you uploaded to your repository.

+2
source

Another solution: 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 turn back without requiring me to make an artificial branch.

If I want to return to the main branch

 git checkout master 
0
source

In git gui, in Visualize History

Change set command: Reset, answer here

image

-2
source

All Articles