>_ IzzyLab

Hands-on task

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

Drop an unwanted commit with rebase

hard~10 min

In /root/repo there are four commits, each of which added its own file: a.txt, secret.txt, c.txt, d.txt. The commit that added secret.txt got into the history by mistake. Remove that commit entirely from the main branch history (don't just delete the file in a new commit — it must not remain in the past either), keeping the other three commits and their files. In the end the history should have three commits left, and secret.txt must not appear in any revision.

What you'll practise:
  • git rebase -i
  • rewriting history
  • dropping a commit
  • GIT_SEQUENCE_EDITOR
  • --root

Topic breakdown

Git history isn't carved in stone: an interactive rebase replays the chain of commits according to a script-list, where each line is one commit with an action (pick, drop, etc.). To make a commit disappear from the history entirely (rather than just deleting the file in a new commit on top), its line is removed from the script. The flag for rebasing from the very root lets you capture the first commit too, and non-interactively the script is edited by supplying your own editor through an environment variable.

What's checked

  1. secret.txt removed from the tree
  2. secret.txt is in no commit
  3. three commits
  4. the other files are in place

What it looks like