Git-Introduction to mobile recorder and reflog, git-recorder reflog

Source: Internet
Author: User

Git-Introduction to mobile recorder and reflog, git-recorder reflog

Reflog is really a Caring little cotton jacket

Preface

In the previous article, data is deleted. The topic of this article is data recovery. After learning these two skills, you can be more confident and bold.

What is reflog?

Reflog, which can be divided into two words: Reference log and Reference log. When the reference in the Local warehouse is moved, reflog will record the movement behavior, which is similar to that of a mobile recorder.

What is reference? What is the reference movement? How did this guy record this movement? Let's put these questions first. Let's first execute them in my test project.Git reflog.

Because you have done a lot of operations, there are many records, and there is too much data, you may not be able to understand them, so we just need to simplify the analysis. What does each data in a single record represent?

41b9778 is the SHA1 value of commit. It is a moving commit.HEAD @ {2} indicates that this is the content pointed to before the HEAD pointer moves (3rd operations from the current back), that is, the above41b9778.Commit indicates the cause of this movement and is the result of the commit operation.The 11.txt file is created to indicate the operation content, and the commit operation corresponds to the commit message.

After a single record is clear, you can view the entire record at a glance. It's easy to know what I 've done to this project using Git.

What is a reference? What is the reference movement? How did this guy record this movement?

Now, do you have an answer to these three questions?

In the figure above, the reference refers to the HEAD pointer; the reference Movement refers to the movement of the HEAD pointer; by recording the operation commands, operation order, and Operation content, the submitted information after the operation to record the pointer movement caused by the entire operation.

Now I want to ask a few more questions:
-Are there other references besides the HEAD pointer?
-If so, what are they? Will it be reflog records?
-How can I view records?

Reference logs, or “reflogs”, record when the tips of branches and other references were updated in the local repository.

This is the reflog description in the official git-reflog document. It is clear that the record is an update change of "branches and other references (branches or other references.

Obviously, references refer to not only HEAD pointers, but also branch pointers.

git reflog 

The default value is the HEAD pointer. If we want to view other references, we can change it to the corresponding content.

// View the movement record of the master branch pointer git reflog master // view the movement record of the feature/test branch pointer git reflog feature/test

Healing power of reflog

As mentioned in the previous blog, this is a relatively healing command. Why? After data is lost due to misoperations, most of them can be retrieved through it.
For example:
-Deleted unmerged branches by mistake.
-Be careful when reset -- hardHEAD ^HEAD ^, too many regresses in history
-Wait, all kinds of mistakes & just willful, just want to recover

All the operations involving pointer movement can be found through reflog.

The recovery process is basically the same:

Step 1:Git reflog to retrieve the commit before the error operation.
Step 2: depending on your own needs, do something for this submission.

The following is a simulation of the reset operation and the restoration process of the accidentally deleted branch, and the mental trauma caused by misoperations. The reflog has a powerful healing power.

Restorereset --hard

1. First simulateReset -- hard operation.

2. FindCommit before reset operation

3.Git reset -- hard Directly restore the history before the operation. This step uses Git reset -- hard HEAD @ {1} is also acceptable.

Restoregit branch -D

1. First simulateGit branch-D

2. the delete operation will not change the HEAD pointer, and the feature/test pointer will be lost after the deletion. What should I do? This should be viewed from the record of the HEAD pointer, find the operation record of the previous branch. If the Branch has been deleted for the latest operation, it is generally well found.

Find the checkout operation, it is easy to analyze that the feature/test Pointer Points to the commit is41b9778。

3.Git checkout -B The commit is checked out as a new branch, and the new branch is named feature/test, which is equivalent to restoring the accidentally deleted branch.

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.