github summary (4)--The painful lesson about the git reset --hard command

Background narrative:

A few days ago, when uploading my own website to git, I used the git reset --hard xxxxxx command in a dizzy hand. Since only one index.html page was passed in online (I didn't know what I was thinking, I just did it, O__O"...), and only one index.html file was committed when I committed for the first time, other files It's just local tracking via the git add --all command.

Then, on a whim, I want to revert to when I first committed (God knows what I thought)! So naturally (probably only God knows how the hands were so natural and casual at the time) I used the git reset --hard xxxxxx command, and the result was ~~~~~~~(>_<)~~~~ , my darling, wait for my response Come here, all the files except the index.html of the first commit are still there, the others, the others, are gone! ! ! !

Well, I was so scared that I was about to break a cold sweat (Come on, hammer yourself with a small fist, how can you be so unwilling!)! ! ! The only summary of feeling is: if crying down the Great Wall can make the document grow back, I must be very determined to try it! ! ! !

=====================A dividing line that is painful to change the past ======================= ======

Let's take a look at what the git reset --hard xxxxxx command looks like.

ps: return to the code before a commit

git reset --hard xxxxxx //Forcibly return to the source state before a commit

==================Use git reset --hard xxxxxx with caution =========================

In the next few hours, I started a bleak and long journey to find solutions on the Internet! ! ! ! ! !

Fortunately, after reading a lot of information, finally, finally, I finally found a solution! ! ! ! ! !

 

===================================================================================================================================================================================== _ _

part 1:

First found this article, http://www.tuicool.com/articles/mqm2uiF, thanks to the author! !

There is a passage in the middle, as follows:

Translate:

If you haven't committed your local changes (even if you haven't tracked the files via git add, when they're deleted, git reset --hard is destructive for those changes that haven't been committed or git added). Sexual, destructive!!)

But, if you're lucky enough to have tracked these files through the git add command, just didn't commit them! Then try the command git fsck --lost-found! Then you can find them in the local project file whose path is .git/lost-found/other (this is the path that the landlord himself tested)! ! And, it contains all the files that have not been committed (designated to a certain commit), and may even include the version of your git add each time (the word version really doesn't know how to translate it here, so let's just think of it as a version. Bar)!

Using the command git fsck --lost-found, through the path .git/lost-found/other, you can restore any files you git added! pass againfind .git/objects -type f | xargs ls -lt | sed 60q这个命令,你就可以找到最近被你add到本地仓库的60个文件,综上所述,希望对你有所帮助!

Of course, it's the but of but, unfortunately, if you don't have the files that git added, after being reorganized by the command git reset --hard, it's just like executing the delete command yourself, and it's hard to die. Looking for it (that is, there is really no hair left!! Three seconds of silence)! ! ! !

part 2:

First of all, the landlord really used the git fsck --lost-found command, except that I saw a lot of ID numbers in the command line window (my little heart!! Because I really deleted a lot of files,~~~ ~(>_<)~~~~ ), but do you really need to generate one by one ID number? Presumably the inner voice of rejection is also a lever! ! But there is no other way. I really tried it. After trying one, I decided to abandon it! ! (Inner OS: Let’s go back and write it slowly!!)

ps: here is also found on the Internet

git show 2e43cd56ee4fb08664cd843cd32836b54fbf594a

git merge 2e43cd56ee4fb08664cd843cd32836b54fbf594a

There is no special response to these two commands (that is, a file is not automatically generated!)! !

(Of course, the landlord also learned a new command git reflog, a bunch of information found that this is useful for restoring files, but I just saw the list of submission records, which is really annoying!!)

part 3:

In desperation, I triedfind .git/objects -type f | xargs ls -lt | sed 60q(这个里面的60参数可以更改成任意你喜欢的数字,比如250啦,它只是代表你想找回的最近的多少次add过的文件)。其实期间已经多次试过这个命令,windows命令行窗口告诉我 xargs不是什么什么可以执行的操作,然后我就困惑在什么叫做什么什么不是可执行的操作中不能自拔!!!

part 4:

Finally wrote a happy ending position, Sahua! ! Well, although it is not so happy, it is a perfect ending in comparison! ! Compared with rewriting, compared to adding and generating IDs one by one, this takes much less time! ! !

Step 1: Find the file in the path of .git/lost-found/other

 

Step 2: Copy these files to another place (after all, if you are careful with the debris, you need to take a long snack to protect it!)

Step 3: Open it in sublime or any programming software, and you can see the essence of each file! ! ! ! !

① Pictures or documents such as doc, when sublime opens it, it will look like this, so you can rename it yourself! Change 00fffbbcab92ec62f7709de8df1fdbb3bbe001b8 to xxx.png or xxx.jpg, if it is a picture, you can preview it immediately! If not, try xxx.doc! !

②html, css, javascript and other files, open it to know what it is, and then change the name yourself! 00fffbbcab92ec62f7709de8df1fdbb3bbe001b8

Change it to xxxx.html or xxx.css or xxx.js! !

part 5:

Finally, to sum up, don't use the git reset --hard xxxxxx command casually. This forced recovery to a certain commit is really overbearing! ! Well, another sentence, git fsck --lost-found is really a powerful command! ! !

git fsck --lost-found //Retrieve the contents of the file that have been added by git but no longer exist (I don't know if this translation is right, I understand it)

Last but not least, keep good commit habits! Well, anyway, commits don’t cost money, so why don’t you commit a few more times, or at least git add a few more times! ! ! ! O(∩_∩)O hahaha~

(Small return, the landlord also found a good article about this unpacking trip. http://www.itwendao.com/article/detail/370021.html, explaining git fsck --lost-found implementation mechanism)!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324927298&siteId=291194637