Skip to Main Content






Git Basics

Exercise 12: Reverting a Commit

  • Objective: Practice reverting a commit using git revert.

  • Steps:

    1. Make a commit that introduces a bug:

      echo "buggy code" > bug.txt
      git add bug.txt
      git commit -m "Add buggy code"

    2. Revert the commit:

      git revert <COMMIT_HASH>

  • Outcome: You will learn how to safely revert a commit and undo changes.

In Step 2 of the exercise, you should replace <COMMIT_HASH> with the unique identifier (hash) of the commit you want to revert.

Here’s how to find the commit hash:

  1. View your commit history using:

    git log

    • This command displays a list of commits in reverse chronological order, including their commit hashes (a long string of characters) and commit messages.
  2. Identify the commit you want to revert by looking at the commit message or other details. The commit hash will look something like this: a1b2c3d4.

  3. Use the first few characters of the hash (usually 7-10 characters) to revert the commit:

    git revert a1b2c3d4

    • Replace a1b2c3d4 with the actual commit hash you found in the log.

This will create a new commit that undoes the changes introduced by the specified commit, effectively "reverting" it.

Exercise 13: Fill in the Blanks

  • Objective: Fill in the blanks to reset changes and undo a commit.

  • Exercise:

    git reset ____ HEAD~1
    git reset --____ <COMMIT_HASH>

  • Answers:

    • Line 1: --soft
    • Line 2: hard