>_ IzzyLab

Hands-on task

This task is step 3 of the course “Git: rewriting and rescuing history”

Undo a bad commit with revert

medium~10 min

The project repo lives in GitLab and is cloned to /root/repo (the web sign-in is on the GitLab tab). main has three commits: init, then add bug (it added an unwanted file bug.txt) and add feature (it added feature.txt), and all of them are already in GitLab.

Undo the effect of exactly the add bug commit without rewriting history: bug.txt must disappear, feature.txt must stay, and a new undo commit must appear in the history (4 commits in total). Push the result to GitLab.

The main branch in GitLab is protected: the server will reject push --force, so reset won't help here.

What you'll practise:
  • git revert
  • undoing a commit
  • commit history
  • git log
  • git push
  • GitLab

Topic breakdown

git revert lets you undo changes from the middle of history without rewriting it: it doesn't throw out the old commit but creates a new one introducing exactly the opposite edits. This is safe for already-published branches, unlike reset, which shifts the pointer and rewrites history: the server accepts such a push only with --force, and a protected branch doesn't accept it at all. Each revert needs a specific target commit, found by its hash in git log.

What's checked

  1. bug.txt is gone in GitLab
  2. feature.txt is still there
  3. an undo commit has appeared in GitLab
  4. the top commit undoes add bug

What it looks like