Restore git reset-hard misoperations

Source: Internet
Author: User
Tags using git
Restore git reset-hard misoperations

Post:

Sometimes you have to be careful when using git, especially when it involves some advanced operations, such as reset, rebase, and merge. Even some small operations, such as deleting a branch, are worried about data loss.

Not long ago, before I made some major operations (rebasing), I always backed up the entire version library, just in case. It was not until recently that I found that git's history record cannot be modified, that is, you cannot change anything that has happened. Any operation you perform is only modified on the original operation. That is to say, even if you delete a branch, modify a commit, or force reset, you can still roll back these operations.

Let's look at some examples:

$ Git init
$ Touch foo.txt
$ Git add foo.txt
$ Git commit-M "Initial commit"

$ Echo 'new data'> foo.txt
$ Git commit-a-m "more stuff added to foo"

Now you can view the history of git. You can see two commits:
$ Git log
* 98abc5a (Head, Master) More stuff added to foo
* B7057a9 initial commit

Now let's reset the status of the first submission:
$ Git reset -- hard b7057a9
$ Git log
* B7057a9 (Head, Master) Initial commit

It seems that we have lost our second submission and there is no way to retrieve it. But reflog is used to solve this problem. Simply put, it records the history of all headers, that is, when you perform reset, checkout, and other operations, these operations will be recorded in reflog.

$ Git reflog
B7057a9 head @ {0}: Reset: Moving to b7057a9
98abc5a head @ {1}: commit: more stuff added to foo
B7057a9 head @ {2}: Commit (initial): initial commit

Therefore, to retrieve our second commit, you only need to perform the following operations:
$ Git reset -- hard 98abc5a

Let's take a look at the GIT record:
$ Git log
* 98abc5a (Head, Master) More stuff added to foo
* B7057a9 initial commit

Therefore, if you lose a commit because of Reset and other operations, you can always retrieve it. Unless your operation has been disposed of as garbage by git, it is usually 30 days later.

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.