Git revert

Git revert

Git Revert: Undo Changes Without Losing History

git revert is a safe way to undo changes by creating a new commit that reverses a previous commit. Unlike git reset, it does not modify the commit history, making it ideal for collaborative projects.

1. Basic Syntax

git revert <commit-hash>

This creates a new commit that undoes the changes introduced by <commit-hash>.

2. Example Usage

🔹 Revert a Specific Commit

Let's say you made a mistake in commit abc1234 and want to undo it:

git revert abc1234

🔹 This opens a text editor for the commit message.
🔹 Save and close the editor to complete the revert.
🔹 A new commit is created that negates the changes from abc1234.

🔹 Revert Multiple Commits

To revert the last 3 commits, use:

git revert HEAD~3..HEAD

🔹 This will revert commits from 3 commits ago up to the latest one.

🔹 Revert Without Committing

To undo a commit but not create a new one immediately:

git revert --no-commit abc1234

🔹 The changes will remain staged, allowing you to modify them before committing.

3. When to Use git revert?

Undo a commit safely without losing history
Fix mistakes without affecting others' work
Reverse changes in a shared repository

🔴 Use git reset instead if you need to permanently remove commits from history.

4. Alternative: Undo the Last Commit

If you want to revert only the most recent commit:

git revert HEAD

This undoes the last commit without affecting previous commits.

🔹 Conclusion

git revert is the best way to undo changes in Git safely, especially in a shared project. Unlike git reset, it does not erase history but creates a new commit to reverse the changes.

Need help choosing between git revert and git reset? Let me know! 🚀

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close