Reverting Changes

Introduction to Git & Markdown

Returning the repository to a previous state.

Page contents

Last commit

Introduction

To undo changes that have been introduced since a given commit, we can use git reset command. By default, this command reverts the repository to the state of the HEAD revision, which is the last committed version. Alternatively, we can specify a commit to return to other than the most recent.

There are 3 mutually exclusive options we can set for the git reset command:

--hard
With this option, Git discards all the changes that have you’ve made to repository contents since the specified commit. Important: These discarded changes are not recoverable through Git.
--mixed
This is the default option. In a git reset --mixed, Git doesn’t modify any file contents, but any files added to the staging area (using git add) since the specified commit are removed from the staging area.
--soft
This option doesn’t modify anything in the staging area or the working contents of the repository, but only changes the internal reference to the current commit (called the HEAD pointer), so that it refers to the specified commit. Obviously, this option is of no use at all when we’re simply returning the state of the repository to the most recent commit (which is the one the HEAD pointer is already referring to).

Steps

  1. If it’s not still open in your text editor, open the README.md file for editing.

  2. At the end of the file, after the content below the ## Links line, add at least one blank line, and include the following Markdown to create a blockquote. (Blockquotes can be nested, and can contain other Markdown elements, including headers, lists, code blocks, etc.)

     > A blockquote indicates that the content is a quote, an e-mail being replied to, etc.
     > 
     > Every line (blank or not) of a blockquote begins with `>`, followed by a space.
    
  3. Save your changes and close the README.md file in your text editor.

  4. In your shell program, execute the following command to add your saved changes to the staging index:

      git add .
    
  5. Now, let’s assume that we’ve decided not to include the blockquote in your GitHub Pages site. We can remove it from the staging index with git reset or git reset --mixed. Alternatively, we can actually discard the change—returning the state of the README.md file to that of the most recent commit—by using git reset --hard.

    Let’s do the latter, by executing the following in your shell program:

     git reset --hard
    
  6. Verify the effects of the git reset --hard command by re-opening README.md in your text editor, and examining the contents to see that the blockquote section at the bottom is now gone.

Important: Be careful when using the --hard option with git reset, since it does discard any local changes you have made. If you simply want to remove changes from the staged index, use --mixed instead. Also, if you’re reverting a commit previous to the most recent (introduced in the next section), git revert is a much safer command to use.

Earlier commit

Introduction

The git revert command is used to undo the effects of a previous commit, by recording a new commit in the repository; this new commit contains changes that reverse the changes introduced in the specified commit. In other words, the git revert command doesn’t actually delete any commits (which git reset is capable of doing). On the contrary: it creates a new commit that reverses the effects of one or more specified commits.

Steps

  1. If it’s not still open in your text editor, open the README.md file for editing.

  2. At the end of the file, make the same change you did in the previous section; that is, add the following:

     > A blockquote indicates that the content is a quote, an e-mail being replied to, etc.
     > 
     > Every line (blank or not) of a blockquote begins with `>`, followed by a space.
    
  3. Save your changes and close the README.md file in your text editor.

  4. In your shell program execute the following Git commands, one at a time. Type the passphrase for your SSH private key if/when directed to do so.

      git add .
      git commit -m "Add blockquote"
      git push
    
  5. Review the updated content on your GitHub Pages website. Your page should now appear something like this:

    Alicia Q. Student

    Introduction

    I’m a self-motivated quick learner, currently attending the Deep Dive Coding Java + Android Bootcamp. My plan after graduation is to look for employment in Java enterprise-level development.

    Current projects

    A blockquote is use to indicate that the content contained is a quote, an e-mail being replied to, etc.

    Every line (blank or not) of a blockquote begins with >, followed by a space.

    Now, even though the blockquote change has been committed to Git, pushed to GitHub, and published in GitHub Pages, we can use git revert to create a new commit that reverses the change; once we push that commit to GitHub, the updated content will be published (usually after a lag of a minute or two) to GitHub Pages, and the blockquote will disappear from the GitHub Pages website.

  6. Let’s assume that we do, in fact, want to remove the blockquote that was previously committed to the repository. Since the associated commit is the most recent, we can refer to it as HEAD; alternatively, we could use git log to find the SHA-1 hash of the commit, and specify that hash value (or a shortened form of it). For simplicity, we’ll do the former; we’ll also use the --no-edit option to instruct Git to generate a default message for the new commit.

    In the shell program, execute the following commands. As always, type in the passphrase of your SSH private key if/when instructed to do so.

     git revert HEAD --no-edit
     git push 
    
  7. To see how the reversion shows up in the revision history, let’s examine the history with the git log command. For brevity, we’ll format the log output with the --oneline option so that each commit appears on a single line with an abbreviated SHA-1 hash value.

    In the shell program, type this command:

     git log --oneline
    

    You should see something like this (of course, the hash values at the start of each line will not match those here).

     04c5cba Revert "Added blockquote"
     d2efba9 Added blockquote
     9907d03 Links to tutorial projects
     0996ead Link to LinkedIn profile
     d3c79b4 Current projects in bullet list
     8bfebb6 Introduction paragraph
     bbb10f1 Initial headings
     f4b2c34 Initial commit
    

    Note that by default, the most recent commit appears first. In this case, it is the commit created by the git revert command.

  8. In the text editor, open README.md, and verify that the blockquote is not longer in the content.

  9. Review the updated content on your GitHub Pages website to verify that the blockquote no longer appears:

    Alicia Q. Student

    Introduction

    I’m a self-motivated quick learner, currently attending the Deep Dive Coding Java + Android Bootcamp. My plan after graduation is to look for employment in Java enterprise-level development.

    Current projects

Summary

You’ve now completed this tutorial! Of course, there are many different options we can specify with the git clone, git add, git commit, git push, git revert, git reset, and git log commands—and all of the other Git-related commands we haven’t mentioned here. Nonetheless, you’ll find that with practice and attention to detail, a small set of Git commands and options will handle the great majority of your version control tasks.