Revert A Commit
Part of the Version Control section of Coddy's Terminal journey — lesson 43 of 58.
If a commit has already been shared with others, amending it is risky because the original ID is gone. The safer option is git revert.
git revert HEADgit revert creates a brand-new commit that undoes the changes from the target commit, leaving the original in place. Your history reads as: "we did X, then we undid X".
You can revert any commit by ID, not just the latest one:
git revert a1b2c3dUse --no-edit to skip the editor and accept the default revert message:
git revert --no-edit HEADThe default message looks like Revert "Original subject", with the original message echoed in the body.
Challenge
EasyThe folder contains app.txt. Initialize the repo and create two commits:
- Add
app.txtwith messageAdd app. - Append
brokentoapp.txtand commit with messageAdd broken line.
Revert the latest commit (no editor) to undo the broken line. Print the contents of app.txt.
Cheat sheet
git revert creates a new commit that undoes changes from a target commit, preserving history:
git revert HEAD # revert latest commit
git revert a1b2c3d # revert any commit by ID
git revert --no-edit HEAD # skip editor, use default messageThe default revert message follows the format: Revert "Original subject".
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Version Control
2Getting Started
Initialize A RepositoryThe .git FolderConfigure Your IdentityGit StatusRecap - First Repo8Merging
What Is A MergeFast-Forward MergeThree-Way MergeMerge ConflictsResolve A ConflictRecap - Merge Master11Feature Branch Project
Project OverviewInitialize Main3Tracking Changes
The Staging AreaGit AddGit CommitModifying A Tracked FileGit LogRecap - First Commits6Recipe Site Project
Project OverviewInitialize And Ignore9Undoing Changes
Discard Unstaged ChangesUnstage A FileAmend The Last CommitRevert A CommitReset A BranchRecap - Time Machine