Mastering Git Revert: Undoing Changes Made Easy!
This video explains the differences between Git Reset and Git Revert, focusing on how these commands affect commit history and collaboration workflows. It highlights the risks of modifying commit history and demonstrates why git revert is often preferable for undoing changes in shared branches.
1. Overview of Git Reset
Git Reset is a command used to undo commits by moving the HEAD pointer to a specific previous commit state. It effectively removes commits from the commit history, making it seem as though those commits never happened.
- Git Reset rewrites the commit history by deleting commits after the specified commit.
- Useful when you want to discard recent commits locally.
- However, modifying history on shared branches (e.g., master) can cause significant issues during collaboration.
2. Risks Associated with Git Reset on Shared Branches
When working in a team environment, using Git Reset on shared branches creates discrepancies between your local repository and teammates' repositories. This leads to:
- Conflicts during pull and merge operations because teammates still have the old commits which you deleted.
- Difficulties reconciling divergent commit histories.
- Potentially complicated merges and risk of data loss or confusion about the current state of the branch.
Key Insight: Because Git Reset deletes commits from history, pushing such changes causes teammates to face conflicts, making collaboration problematic.
3. Introduction to Git Revert
Git Revert offers a safer alternative for undoing changes because it does not change commit history. Instead, it creates a new commit that reverses the effects of a previous commit.
- Revert keeps the commit history intact.
- It essentially performs the opposite operation of the target commit, thereby "undoing" its changes.
- Especially useful for undoing changes on shared/public branches without disrupting teammates' workflows.
4. Demonstration Example
The video presents a simple Git repository example with three commits applied to a file named Hobbies:
- Using Git Reset on the third commit deletes the last commit (
Gym). This alters history by removing that commit. - Using Git Revert on the third commit creates a new commit that removes the
Gymline but retains all previous commits in the history.
5. How Git Revert Works Practically
- When running
git revert <commit>, Git automatically generates a new commit that inverses the changes made by that commit. - The revert commit requires a commit message, and the editor opens automatically for confirmation.
- After revert, the commit log shows all previous commits intact plus the new revert commit.
- The actual file content is updated to reflect the undo, e.g., removing "Gym" but retaining "Read" and "Games".
6. Handling Conflicts with Git Revert
- If you attempt to revert a commit that conflicts with changes in later commits, merge conflicts can occur.
- These conflicts must be resolved manually by the user, similar to regular merge conflict resolution.
- Despite the potential for conflicts, revert avoids history rewrites, which minimizes collaboration problems.
7. Summary Table: Git Reset vs Git Revert
8. Key Takeaways
- Do not use Git Reset to undo commits on shared branches like
masterif your team members are actively working on them. - Prefer Git Revert to undo changes safely while preserving commit history and reducing merge conflicts.
- Revert commits leave a clear history of what was undone, which is useful for tracking and auditing changes.
- Be prepared to resolve conflicts if reverting commits that are no longer at the branch's HEAD.
- Understanding these commands is vital for smooth collaboration and maintaining integrity of version history in Git workflows.
This foundational knowledge helps developers make informed decisions when managing and undoing changes in Git, especially in team environments.