Revert Commit | Bondar Academy
Course: Git and GitHub Fundamentals
Module: Working with Git and GitHub
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore how to revert changes in Git by using two primary commands: git reset and git revert . These commands allow you to navigate back to previous commits in your Git history. Understanding HEAD The HEAD in Git is a pointer that indicates the current commit. By default, it points to the latest commit, but you can move it back and forth to view different versions of your code. Using git reset git reset : This command, by default git reset --mixed , allows you to move the HEAD pointer to a previous commit without losing any data. The changes after the specified commit become unstaged. git reset --hard : This command deletes all changes after the specified commit. Use it with caution, as it cannot be undone. Considerations for Remote Branches When working with remote branches, you cannot simply reset the origin branch to an earlier commit. Instead, you would need to use git push origin master --force , which is generally not recommended for the master branch due to potential data loss. Using git revert The git revert command creates a new commit that undoes the changes made in a specified commit without altering the commit history. This is safer for collaborative environments as it maintains the integrity of the commit history. Key Points to Remember: Use git reset for local changes where you can afford to lose data. Use git revert to safely undo changes while preserving commit history. Be cautious with git reset --hard as it permanently deletes changes. By mastering these commands, you can effectively manage your code versions in Git.