Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

Guided: Git Reflog and Recovery

At some point, most users of Git will need to find or recover work that they lost. Maybe you have accidentally committed work you didn't want to commit or maybe you just changed your mind. Either way, its important to understand how to undo, redo, and recover commits in Git. This hands-on lab will help you get up to speed with advanced Git recovery techniques. You'll start by learning Git's revert and reset commands - two commands that will allow you to "undo" actions and recover to a previous point in time. You will then build on your knowledge by gaining a firm understanding of the lesser-known reflog command and its ability to help you recover commits that you thought were long gone!

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 32m
Published
Clock icon Mar 27, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    ### Introduction

    It is inevitable that you will want to try and recover lost work with Git. In fact, this is one of the most helpful aspects of the Git version control system. In this guided code lab, you are going to learn about the three, primary Git recovery techniques. These techniques include the following commands:

    • git revert
    • git reset
    • git reflog

    In the first step of this course, you are going to learn about git revert and how it can be used to undo a previous commit. The git revert command is the perfect command when you have accidentally committed some changes or if you accidentally introduced a bug that you want to quickly revert while also retaining your commit history.

    Building on this knowledge, in step three you will learn about the git reset command and its power to completely undo, not just changes, but the commit history of a branch itself. The git reset command has the power to bring your branch back in time to a specific commit, cleaning your git history of commits in the process.

    Lastly, in step four, you are going to learn about the git reflog command. This lesser-known command can give you the power to restore lost work in your local Git repository. You will learn how to use git reflog in tandem with the git reset command to restore work that you otherwise might have lost forever!

    Please feel free to reference the git revert, git reset and git reflog documentation as you complete this Guided Code Lab. Should you get stuck, you will find a solution.txt file within the solution directory on the master branch. This file details the commands to run in order to successfully complete each task.

    Note: Changes made to this filesystem using Git may not automatically be conveyed in the filetree at times. If this happens, you can visualize changes to files by exiting out of the file and reopening it.

  2. Challenge

    ### Reverting Commits

    The stage is set. You just finished committing a new feature to your local feature branch. You have submitted a pull request which was subsequently approved after a review. However, something slipped past the cracks and you have accidentally introduced a bug into the production environment. This particular bug can directly impact the business revenue of your company so it's imperative that the change is reverted quickly. Enter git revert.

    The git revert command is the go-to command when you want to undo changes made by a particular commit. In practice, the git revert command's functionality is extremely simple – given a commit SHA, this command will create a new commit that is the exact inverse of the specified commit SHA. For example, if you have a commit that adds a new file to a repo, the git revert command will create a new commit that will remove that added file. The cool thing about this command is that it will undo changes made by a previous commit while still retaining the full git history of a repository – something that is very important, especially for public changes to open-source repositories.

    In this step, you are going to use the git revert command to undo local commits that you are going to “accidentally” commit to your branch. In the next task you will be inspecting your commit history via the command git log. The git log command is the “public” version of the git reflog command that you will learn about in a future step. You can use git log to browse the commit history of a given branch – something that is almost always needed when you are looking for specific commit SHAs. Uh oh! You didn’t mean to commit that button change after all! You have the SHA encompassing the commit you want to undo... now you need to undo it! This is where the git revert command shines. It can undo a single commit or range of commits using the .. syntax. But even more importantly, git revert does not alter your commit history so you get full transparency. Well done! You now have a good grasp of the git revert command. It’s a powerful command that you can use to easily revert changed introduced by any commit. But what if you have a whole series of commits that are just plain wrong and you don’t want to keep the commit history? In the next step, we’ll dive a bit deeper into Git recovery techniques by looking at the git reset command.

  3. Challenge

    ### Resetting Commits

    In this step, you are going to learn about the power of the git reset command. If the git revert command is like a surgical scalpel, the git reset command is like a hammer.

    Unlike the git revert command, git reset does not directly retain your commit history when you rollback. The git reset command is also not a great choice when you are working with a public repository. However, git reset is a great tool for rolling back changes within a local repository. When you use the git reset command, it will be like a commit or series of commits never happened as far as your branch history is concerned. You have decided that you don’t want any changes introduced by any of the last 3 commits and you don’t want to have your git history clogged up by any of these commits either. This is a perfect use case for git reset. In the last task you accomplished what you wanted by reverting back 3 commits. But you still have changes in your working directory! It turns out that you don’t want those! In the last task you used git reset but that command kept changes around that you really didn’t want. You were able to get rid of these by using git checkout. But there is an easier way! This is a perfect use case for running git reset –hard. Adding the --hard flag will clean up those changes for you too! Good job! You now have a good grasp of the git reset command. It’s a great command to use when you want to undo a lot of commit history and local changes. But what if you want to redo the changes you made via git reset? Aren’t those changes lost forever? Let’s find out in the next step where you will learn about the git reflog command.

  4. Challenge

    ### Recovering Commits

    So far, you have learned how to undo work in a Git repository. But what if you want to redo work that you’ve managed to remove via git reset? How can you recover this supposedly lost work? Enter git reflog. If the git log command shows you a public record of commit history, you can think of git reflog as unveiling the private record of your commit history.

    The git reflog command gives you a window into the history of all local commits for a given branch including supposedly deleted commits (like those wiped away using git reset). There are some drawbacks with using git reflog that you should be aware of:

    • It is not guaranteed that git reflog will keep track of every commit to a branch forever... By default, this private commit history is kept track of for 90 days.

    • The git reflog command is only helpful for you in your local repository... This isn’t a command that you can use on a remote repository.

    In this step, you are going to use git reflog to browse the private commit history of your branch and bring back changes that you thought were lost! To understand git reflog and how to use it, first you need to understand the concept of HEAD in Git. In Git, HEAD corresponds to the latest commit in your current branch. Simply put, its purpose is to show you where your next commit will be applied to! Understanding that, git reflog simply shows you all of the occasions that HEAD changed in recent history. The git reset command is very useful when it comes to recovering lost work in a clean way. When combined with git reflog, it allows you to reset the git history of a branch back to a specified commit that was lost. The git reflog command is also quite useful when recovering accidentally deleted branches. When recovering a deleted branch, it is important to understand the concept of a detached HEAD state. In Git, being in a detached HEAD state means that you are not currently on any branch – instead you are pointing directly to a commit. This is a useful state when recovering lost work and/or branches since it allows you to reference a supposedly lost commit and then create or recreate a new branch from this state and continuing applying new work to the specified commit. A detached HEAD state is not a place you want to remain. Think of it as just a little pit stop on your journey to a clean working state. The reason you don’t want to stay in a detached HEAD state is that it is very easy to lose new work that you apply to the detached HEAD commit. In your use case, you want to recreate a branch that you accidentally deleted. You can do that via git checkout along with git reflog.

  5. Challenge

    ### Conclusion

    Nicely done! You’ve made it to the end of this guided code lab on git reflog. In this lab, you first learned about the simplest form of recovery in Git by way of the git revert command. You learned that this command will perform the inverse of a given commits or range of commits making it perfect for quickly undoing certain commits all while keep your Git history intact.

    You then learned about a more intense version of git revert in the form of git reset. You learned the basics of using git reset to undo sweeping local changes to both your commit history and local file changes.

    Finally, with the building blocks acquired, you learned about git reflog - the key to recovering lost work with Git. You learned how to use the git reflog command in tandem with git reset –hard to bring your local branch state back to a place in history that you thought was lost forever. You also learned how to configure git reflog in order to keep this private history around longer.

    In summary, you learned these primary, Git recovery techniques:

    Use git revert to undo both local and remote commit history while keeping all history intact.

    Use git reset to undo local commit history – wiping your commit history clean

    Use git reflog in tandem with git reset to redo changes made via git reset and/or git revert

    From here, you can be confident when it comes to integrating git reflog and other recovery techniques via git revert and git reset into your specific Git workflow. I recommend that you continue your journey learning Git by pursuing both video courses and more guided code labs here, at PluralSight.

Zach is currently a Senior Software Engineer at VMware where he uses tools such as Python, Docker, Node, and Angular along with various Machine Learning and Data Science techniques/principles. Prior to his current role, Zach worked on submarine software and has a passion for GIS programming along with open-source software.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.