>_ IzzyLab

Hands-on task

This task is step 12 of the course “Everyday Git”

Undo a commit while keeping changes staged

medium~5 min

In the repository /root/repo there are two commits: first (added file1.txt) and second (added file2.txt). Undo the last commit so that HEAD points at first again, but the changes from second (the file file2.txt) stay staged for commit (in the index). The file file2.txt must remain on disk.

What you'll practise:
  • git
  • git reset
  • --soft
  • index
  • HEAD~1

Topic breakdown

git reset moves the branch pointer to another commit, and its mode determines the fate of the index and the working tree. Here you need to undo the last commit but keep its changes staged for commit — that's the gentlest reset mode, which moves HEAD back and leaves all the content of the undone commit already in the index. The target commit is specified with a relative reference like HEAD~1.

What's checked

  1. one commit remains
  2. file2.txt is staged
  3. file2.txt on disk

What it looks like