Git
How to Revert a Previous Commit in Git?
Mistakes in code are inevitable, and version control tools like Git make it easy to recover from them. One of the most common tasks in Git is undoing a previous commit, whether it’s to remove unwanted changes, reverse a mistake, or improve code structure.
In this post, we’ll walk through the different methods for reverting a previous commit in Git, covering both safe reversion techniques that preserve commit history and options for complete removal of unwanted changes.
Understanding Git Commit Reversion Options
In Git, there are multiple ways to undo commits, each suited to different needs and scenarios:
- Revert: Creates a new commit that undoes changes from a previous commit. This method is safe for shared branches because it preserves commit history.
- Reset: Moves the branch pointer backward to a specified commit, effectively removing the specified commit(s) from the history. This is more suitable for local or feature branches since it rewrites commit history.
- Soft, Mixed, and Hard Resets: Allows control over which changes are kept in the working directory or staged area.
Method 1: Using git revert
to Undo a Previous Commit
The git revert
command is ideal when you want to undo a previous commit without altering the commit history. It creates a new commit that reverses the changes introduced by the targeted commit.
Step 1: Identify the Commit to Revert
First, find the hash of the commit you want to revert by listing recent commits:
git log --oneline
Each commit is displayed with a unique hash and commit message. Copy the hash of the commit you want to undo.
Step 2: Run the Revert Command
Once you have the commit hash, use git revert
:
git revert commit-hash
Replace commit-hash
with the actual hash of the commit you want to revert. This command will open a text editor to enter a commit message. By default, Git will include a message like “Revert ‘commit message of the original commit’.”
Step 3: Save and Close the Commit Message
Once you’ve edited or confirmed the commit message, save and close the editor (typically Ctrl+O
to save and Ctrl+X
to exit in nano, or :wq
to save and close in vim). The reversion commit will be added to your branch.
Step 4: Verify the Reversion
To ensure the commit was successfully reverted, check your commit history with:
git log --oneline
You should see a new commit at the top with a message indicating the reversion.
Method 2: Using git reset
to Undo a Commit (Local Branches Only)
If you need to completely remove a commit from history (for instance, on a private or local branch), git reset
is a powerful option. Be cautious with reset
, especially on shared branches, as it rewrites commit history.
Step 1: Identify the Commit to Reset To
Use git log
to view your commit history and identify the commit to reset back to:
git log --oneline
Step 2: Run the Reset Command
Choose the appropriate reset type based on your needs:
- Soft Reset (
--soft
): Moves the branch pointer back, keeping changes staged. - Mixed Reset (
--mixed
): Moves the branch pointer back, keeping changes unstaged. - Hard Reset (
--hard
): Moves the branch pointer back and discards all changes.
To reset to a specific commit, use:
git reset --hard commit-hash
Replace commit-hash
with the hash of the commit you want to reset to. For example, git reset --hard HEAD~1
will move the branch pointer back by one commit.
Step 3: Verify the Reset
Check your commit history to ensure the reset was successful:
git log --oneline
The unwanted commit(s) should no longer appear.
Warning:
--hard
resets delete changes permanently from the working directory and cannot be undone with a simple command. Use with caution.
Method 3: Using git reset --soft
to Keep Changes Locally
If you want to remove a commit but keep its changes in the staging area for editing, use git reset --soft
. This is useful if you need to make further changes or combine the modifications with other commits.
git reset --soft HEAD~1
This command moves the branch pointer back by one commit but keeps the changes in the staging area. You can then edit the files or recommit the changes as needed.
Method 4: Using git checkout
and git commit
for Selective Reversions
If you want to revert specific files within a commit, you can manually check out specific files from a previous commit and commit them.
Step 1: Identify the Commit and File
Use git log
to find the commit, and git show
to list its files:
git show commit-hash --name-only
Step 2: Restore Specific Files
Restore a specific file from a commit using:
git checkout commit-hash -- path/to/file
This command will revert the file to the state it was in at the specified commit. Once restored, add and commit the changes:
git add path/to/file
git commit -m "Revert file to previous state"
This approach is useful for partial reversion without affecting other files in the commit.
Summary and Best Practices
Choosing the right method for reverting a commit depends on your project’s needs and whether you’re working in a shared branch. Here’s a quick summary:
- Use
git revert
for safe, history-preserving reversion on shared branches. - Use
git reset
(with--soft
,--mixed
, or--hard
) when you need to remove commits entirely, especially on local or feature branches. - Use
git checkout
for selective file reversion within a commit.
Keeping your Git commit history clean and meaningful is essential, so only remove or alter commits when necessary. Following these guidelines ensures that your team can trace changes effectively while allowing you to recover quickly from unwanted changes.