The difference between reset and checkout in Git

Source: Internet
Author: User
Tags using git
Overview: The difference between reset and checkout in git preface: learn git's command reset and checkout after, some silly don't know the difference between the two, roughly understand, want to write a blog to deepen understanding of the text: 1.reset

As shown in the picture, this is the final destination diagram, the Blogger learning the Reset command when the textbook is introduced to reset command, after the Feature-a and master merged with

git reset--hard "hash value of original master"

and explain to this command to go back to the feature-a before the creation, and then to create a fix-b, the blogger does not understand why not checkout command to the head to point to the original master, and then build a new called "Fix-b" branch, until you see the following figure, Why before the feature-a and the branch of the merge disappeared, just started to think that the book is easy to look good only deleted, thought that will be added later, the results of the search for a certain information is not, to understand the need to know the three areas of git: Work area, buffer, history area, These three areas are no longer explained, please refer to the links below: https://www.cnblogs.com/liujiaq/p/5670069.html

After understanding the three regions, we can understand what the reset actually does, as shown in the following figure:

When we point the head pointer at a branch called hotfix, we execute the following command:

git reset head~2
The diagram at this point will become:

Head and branch all point to the position of Head 2, if you do not know where the HEAD2 corresponds to

Git reflog

See, do not know the careful classmate has not found the head2 on the right side of the two branches blurred, here for backtracking, can also be understood as time reversal, after head2 operations can be considered not carried out, but these operations may be saved to a region, Here's a look at the three mark--hard,--mixed,--softed of reset.

Abstract materialization: The individual compares these three regions to three different space-time, and the three space-time can be shuttled back and forth between each other through certain instructions. Reference to the relationship of transformation in the following figure:

When you use Reset (space-time), the scope of the three different tags is different, as shown in the following figure:

The scope of the three markers is as follows, three space-time may have only one space to receive the effect of time and space reversal, but it is possible that all space-time will be time and space flow backwards.

Example: When using git reset--soft hash The other two time and space is not affected, buffer space and time still retains the previous information that time those branches have been added into the buffer space and time, if you want to restore the history of space and time, only need to commit (submit) once
2.checkout

Here's a checkout directive as shown in the following illustration:

When you want to point head to Hotfix, you can use checkout that is: Git checkout Hotfix results are as follows:

The head pointer changes but not like reset, after the head change points to->hotfix, the branch created after hotfix does not disappear, which is the biggest difference between checkout and reset if, as I mentioned above, using Git checkout Master's command so master's feature-a is not going to disappear.


On the characteristics of reset, the individual uses a specific concept of time and space to explain it but feels that it may be somewhat abstract for many people, and here's an example to deepen the understanding: if there are 1 2 3 of these branches in the history area, then the three files commit in the order of 1,2,3. Set 1 corresponding hash value of 1998 at this time we perform git reset--hard 1998 after execution, because the scope of the hard includes all areas, so if you want to add 2 and 3 again, you need to add the files (add and commit) from the beginning, If this is a--soft tag because there is no backtracking in the buffer, then only a commit can be made.

This article reference link: https://www.cnblogs.com/houpeiyong/p/5890748.html conclusion: Feel git some of the principle structure is really very abstract, I hope we can communicate a lot of common progress






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.