Instructor: Git Reflog helps developers recover from their mistakes. I'm going to check out the Lodash Repository, make some changes, delete those changes, and demonstrate how Reflog can help me restore them.
I'll start by making a new branch called Better Lodash. Then I'm going to go in the package.json file and make a few modifications. I'll change the name to Better Lodash, up the version number, and set the required node version a little bit higher, then I'll save the file. I'll now commit that change with a commit message, Better Lodash.
Now I'll type Git Log -N1 to review the last commit. You can see the commit hash that was saved started with 60864. Now type Git Reflog. The Reflog keeps track of any changes or commits to a branch I've made since cleaning the repository.
You can see in the middle that I cloned the repository, then I checked out the Better Lodash branch, and finally, I made the commit, Better Lodash.
On the left-hand side, you can see the commit hash associated with each of those changes. In this case, it's 60864 just as it was when I made the commit originally. The other two commits share the commit hash 238E76.
To create a new branch, I'll type Git Checkout -B100. Type npm version minor and npm version major to create two new commits in our history. Let's run Git Log again. Now you can see we have three commits -- 100, 99, and Better Lodash.
Type the command Git Branch to reveal our current branch. We'll now go back to the master branch with Git Checkout Master and actually destroy some of our earlier work.
Type Git Branch -D Better Lodash to remove the Better Lodash branch. Then we'll check out the 100 branch and reset it to the contents of master. Type Git Log.
You can see that we no longer have those three commits that we added before. In fact, a Git Diff reveals that there's no different between this branch and what's on master. Type Git Branch, and you'll see we have two branches left -- 100 and master -- and those branches are now exactly the same.
Now type Git Reflog to see how Git kept track of all these changes. Notice our initial clone of the repository moving to the Better Lodash branch, the initial commit we made, Better Lodash, then moving to the 100 branch, the subsequent 99 and 100 commits.
Notice when we went back to master and then back to 100, and then when we reset the 100 branch to origin master. Let's restore our missing commits now.
Type Git Branch, and you'll see we're still on the 100 branch. If you look at our Reflog on the left-hand side, the commit hash for our 100 commit is 34BEFC.
We use Git Reset --Hard to restore that and previous commits to this branch. Git Log now shows all of our missing commits restored to the 100 branch -- 100, 99, and Better Lodash.
Now let's work on restoring the Better Lodash branch. Let's run Git Reflog again and look for the last commit we made on that branch, 60864. From here, we type Git Checkout -B Better-Lodash and use the commit hash we found in our Reflog, 60864.
If you type Git Branch, you'll see we're back on the Better Lodash branch. If you type Git Log, you can see that our commit has been restored. We've used Git Reflog to restore all of our missing commits.