Overview
This tutorial explains how to undo changes from a specific past commit using Git’s revert
command and Lazygit’s revert feature. Unlike other undo operations in Git, revert
creates a new commit that undoes the changes made by a previous commit, keeping your project history intact.
Lazygit
Reverting a Commit in Lazygit
-
Accessing the Commits Panel:
- Press
4
to open the 4. Commits panel in Lazygit. This panel lists all your commits.
- Press
-
Selecting the Commit to Revert:
- Use the
up
/down
arrow keys to navigate through the list of commits. - Locate the commit you want to revert.
- Use the
-
Performing the Revert:
- Once you’ve selected the commit, press
t
. - Lazygit will create a new commit that undoes the changes made in the selected commit.
- A new commit is added to your history, effectively reversing the changes. ![Pasted image 20230722132317.png]
- Once you’ve selected the commit, press
Finding a Specific Commit
- Searching for Commits:
- If you need to find a specific commit, use
/
to initiate a search. - Enter keywords or phrases to narrow down the list.
- If you need to find a specific commit, use
Basic Terminal
Reverting a Commit Using Git
-
Identifying the Commit:
- First, find the commit you need to undo:
- Use
git log
to view a list of recent commits. - For a more concise list, use
git log --oneline
. - To see detailed changes (patches), use
git log -p
.
- Use
- First, find the commit you need to undo:
-
Executing the Revert:
- Revert using the commit’s SHA hash or relative position:
# Revert using the commit's SHA hash git revert BADHASH # Revert a commit that's 3 commits ago git revert HEAD~3
- After running the revert command, an editor will open for you to edit the commit message of the new revert commit. Save and exit the editor once done.
- Revert using the commit’s SHA hash or relative position:
Explanation
Understanding Git Revert
- How Git Revert Works:
- Git creates a reverse patch of the commit you’re reverting. This patch is then applied as a new commit.
- This new commit is the inverse of the changes made in the original commit, effectively undoing those changes.
- The original commit remains in your history, preserving the project’s history and ensuring transparency.
Note on Git Revert
- Remember that
git revert
is a safe way to undo changes as it doesn’t alter your project’s history. This is especially important in collaborative environments where changing shared history can lead to conflicts.
Further Enhancements
- Consider adding a demonstration or illustration showing how a reverse patch is created and applied. This can help users visualize the process and understand how
git revert
maintains the integrity of the commit history.