Recover git reset-hard error operation

Source: Internet
Author: User


Not long ago, before I made some big moves (rebasing), I always backed up the entire repository, just in case. Until recently I found that git's history is immutable, meaning you can't change anything that has happened. Any action you make is only modified on the original operation. That is, even if you delete a branch, modify a commit, or force a reset, you can still roll back these operations.

Let's take a 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 that you see Git's history, you can see two commits:
$ git log
* 98ABC5A (HEAD, master) more stuff added to Foo
* B7057A9 Initial Commit

Now let's reset back to the state of the first commit:
$ git reset--hard b7057a9
$ git log
* B7057A9 (HEAD, master) Initial commit

It looks like we lost our second submission, and there's no way to get it back. But Reflog is used to solve this problem. Simply put, it records the history of all head, which means that when you do reset,checkout and so on, these actions are recorded in Reflog.

$ git Reflog
b7057a9 [Email protected]{0}: reset:moving to B7057a9
98abc5a [Email Protected]{1}: Commit:more stuff added to Foo
b7057a9 [Email protected]{2}: Commit (initial): initial commit

So, we need to get back our second commit, just do the following:
$ git reset--hard 98abc5a

Take a look at the git record:
$ git log
* 98ABC5A (HEAD, master) more stuff added to Foo
* B7057A9 Initial Commit

So, if you lose a commit because of reset, you can always get it back. Unless your operation has been disposed of by git as garbage, it is usually 30 days later.

Recover git reset-hard error operation

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.