Restore Commits with git reflog

Jamund Ferguson
InstructorJamund Ferguson
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

git reflog helps developers recover from their mistakes. The reflog keeps track of any changes or commits to each branch since cloning a repository. It's frequently used to keep track of changes made locally that are sometimes wiped out when pulling in remote changes.

This lesson demonstrates how to use git reflog to restore git commits and branches that have been lost. We will clone a repo, make some changes with commits and branches, remove all of those changes, and then use data from the reflog to restore them.

Instructor: [00:00] 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.

[00:10] 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.

[00:35] 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.

[00:52] 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.

[01:00] 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.

[01:14] 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.

[01:33] 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.

[01:44] 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.

[01:56] 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.

[02:13] 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.

[02:27] 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.

[02:37] 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.

[02:48] 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.

[03:03] 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.

[03:22] 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.

egghead
egghead
~ 11 seconds ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today