Share backend development and cloud computing related technologies, explain the love and hate between programmer Bear and his boss.

[resources] programmers can get the necessary skills atlas, hundreds of popular programming electronic books.

“The new intern covered his functions for a month and asked me for help. Do you want to help him? – Three minutes of programming”

Finding the Lost Code

The new intern [Bei Guo] always lost his code because he was not familiar with the use of Git. This time, he lost all the functions he had been working on for a month. It’s a good thing I helped him recover. Let’s discuss two code loss scenarios.

Restores previously committed records

Git reset –hard commit — id specifies a commit for your workspace. Git reset –hard commit — id specifies a commit for your workspace. Git reset –hard commit — id specifies a commit for your workspace.

Git reflog: Git reflog: Git reflog: Git reflog

Just now we mentioned a keyword directory tree. What is a directory tree?

Our main line is usually a straight line, one more branch is equivalent to one more fork, countless branches crisscross like a tree structure, so we call it a directory tree. Commit rebase reset merge These operations change the directory tree.

  1. This method also recovers lost code only if there is an operation to change the directory tree
  2. The git log is the same, and you can also see the history of all branch commits, but not the deleted onescommitRecord andreset rebase mergeWe can see the operation ofgit reflogThe one in front iscommit idWe can use it nowThe method that was introduced beforeBack and forth version

Git reflog: commit id = 00 commit id = 00 commit id = 00 commit id = 00 $git reset –hard commit — id $git reset –hard commit — id $git reset — id Then we can roll back through the commit ID we remember!

The steps in the figure above are:

  • According to thegit reflogThe result returned withgit reset --hard commit_idBack to the856a740This version
  • git log -1Looking at the next line of the log, you can see that the current back856a740This version, the last version
  • According togit reflogThe result of usinggit reset --hard 35b66edRun back to the latest of this submission
  • git log -2After seeing the logs submitted twice, we just went back and forth, that’s how good it was

Git cherry-pick commitid: merge merge git cherry-pick commitid: merge merge merge

Recover a record you forgot to commit

If your code file was not committed, it was deleted or reset.

It’s not just a simple git reflog command to retrieve, but if you’ve added it to the staging area before, we can retrieve it. What? If you haven’t even done add, then you have to start preparing for a new round of interviews.

Let’s create a disaster site.

  • Create a file calledlose_file.txtAnd write contents to the filemy lose messageAnd add it to the staging area with git add
  • withgit reset --hard 35b66ed8Restore the current workspace to by discarding all changes35b66ed8Version, which has not been committed yet, reverts to the current version (headVersion).
  • We usestatusandlsAnd let’s see, this is calledlose_file.txtThe file is really gone, screwed, first response to use the command just learnedgit reflogWill findIt doesn’t work at allSo now you have to get ready to run? Don’t panic. Let’s move on to the next video

Let’s take a look at git’s underlying principles

Git FSCK –lost-found — git FSCK –lost-found — git FSCK –lost-found — git FSCK –lost-found

We can see blob, COMMIT, tree, tag, etc. What do they mean?

Git storage: Git storage:

  • commitThe data structure is generated after each commit as we proceedcommitAfter that, one is created firstcommitComponent and then create onetreeComponent, where all file information is stored, eachblobRepresents a file that can all be intreeFound in
  • blobThe component does not store file information, but only the contents of the file. The file information is stored in the tree

Git reset –hard — lose_file. TXT

Git FSCK –lost-found git FSCK –lost-found git FSCK

The contents of lose_file. TXT are the original contents of the file we lost, so it’s back!

The hierarchy between the commit and the tree is as follows:

  • withgit cat-file -pYou can see the contents of the commit, and you can choose to merge the commit into our branch, orreset merge rebase cherry-pickThese commands come togethercommit
  • git ls-treeList the file names under tree andidYou can then restore the file based on the BLOB ID

Afterword.

Git FSCK –lost-found: git FSCK –lost-found: git FSCK –lost-found: git FSCK –lost-found

  • Here withfind .git/objects -type f | xargs ls -lt | sed 3qHe is looking for the meaning of this command. The git/objects normal file folder According to the time after sorting Print sed in terminal 3 q is to print 3 line sed 100 q is to print 100 line, as you like.
  • git cat-file -t

    7f5965523d2b9e850b39eb46e8e0f7c5755f6719You can see the file type. Copy everything from objects/ after the last/and put it after -t
  • git cat-file -p idYou can see the contents of the file, isn’t it cool? With a few Linux tricks, you can batch print all the file types

summary

After some operation, the intern [Guo] no longer needs to take the blame. Let’s summarize:

1. If the file has been submitted, run git reflog to retrieve the file

Git FSCK –lost-found git FSCK –lost-found git FSCK –lost-found

3. Use the find success, didn’t find. Git/objects -type f | xargs ls – lt | sed 3 q this command to output the modified files recovered soon.

The prerequisite for code recovery is to ensure that your.git folder is intact at the root of your project. If you manually delete something in it, you’re done. Make sure your code has been traced before, or at least added!

As long as one person can get help, Little Bear will keep trying

Let’s play together if you like