Welcome to our comprehensive guide on how to undo public commits using Git! In this lesson, we'll explore the power of Git's revert and reset commands to correct mistakes, and understand the implications of each command.
Public commits are changes that you've pushed to a remote repository (like GitHub, Bitbucket, or GitLab) for others to see and collaborate on. They form the project's history, and once pushed, these commits are visible to all collaborators.
Mistakes happen, and sometimes we might push changes to the remote repository that we wish to undo. This could be due to accidental commits, typos, or unwanted changes. In such cases, learning how to undo public commits is essential for maintaining a clean and organized project history.
revert and reset 📝Git offers two primary commands to undo public commits: revert and reset. Understanding the differences between these commands is crucial for proper usage.
revert 💡The revert command is used to undo changes introduced in a specific commit, by creating a new commit that inverts the changes made in the original commit. This new commit will have a clear, descriptive message explaining the revert action.
Here's a simple example:
git revert <commit-hash>Replace <commit-hash> with the hash of the commit you wish to revert.
reset 💡The reset command is a more powerful tool that allows you to move the tip of the current branch to a different commit. By doing so, it removes all commits between the current branch and the commit you are resetting to.
There are three modes of reset: soft, mixed, and hard. For undoing public commits, we'll focus on the mixed and hard modes.
git reset <commit-hash> (mixed mode) - This command moves the branch tip to the specified commit, but the changes are still in the work-tree and index. You can still choose to keep or discard the changes using git add and git commit --amend, respectively.git reset <commit-hash> --hard - This command moves the branch tip to the specified commit, discards all changes in the work-tree and index, effectively erasing the changes from your local repository.What does the `git revert` command do?
Let's dive into some practical examples to help you better understand how to undo public commits using Git's revert and reset commands.
revert 💡In this example, we'll use the revert command to undo the changes made in a specific commit:
git checkout <branch-name> # Checkout the branch containing the commit to revert
git revert <commit-hash> # Start the revert processThe revert process will open a text editor, allowing you to write a descriptive commit message for the revert. Save and close the editor to complete the revert.
reset (mixed mode) 💡In this example, we'll use the reset command (mixed mode) to undo the changes made in a specific commit, while keeping the changes in the work-tree:
git checkout <branch-name> # Checkout the branch containing the commit to reset
git reset <commit-hash> # Move the branch tip to the specified commit (mixed mode)Since we are in mixed mode, the changes are still in the work-tree and index. Now you can decide whether to keep or discard the changes using git add and git commit --amend, respectively.
What does the `git reset <commit-hash> --hard` command do?
In this tutorial, we've explored the power of Git's revert and reset commands, and learned how to undo public commits in a Git repository. By understanding the differences between these commands and practicing their usage in practical examples, you'll be well-equipped to handle mistakes and maintain a clean, organized project history.
Happy coding! 🤓