Mastering Git at Matillion. Git Revert: Undoing changes the smart way

Welcome back to the final installment in our Git series (at least for now)! So far, we've explored essential concepts like branching, merging, and managing uncommitted changes. In this post, we’ll dive into “Git Revert.” Understanding how to undo changes safely and effectively is crucial for maintaining a clean project history and ensuring your codebase remains stable. We'll explore the reasons behind undoing changes, some of the methods available, and the nuances of reverting both locally and remotely. Additionally, we'll discuss the differences between git revert and git reset to give you a comprehensive understanding of these powerful tools.

Why Revert Changes?

In any development process, mistakes are inevitable. You might introduce a bug, implement a feature incorrectly, or make changes that we later realise don’t align with the project’s direction. Reverting allows you to undo these changes safely without rewriting your project's history. This ensures that your commit history remains intact and transparent, which is essential for collaboration and tracking the evolution of your project.

Understanding Git Revert

The git revert command is used to create a new commit that undoes the changes introduced by a previous commit. It maintains a complete history by adding a new commit to the log that reverses the effects of an earlier one. This makes it a safer option, especially when working in a shared repository. This is unlike git reset which doesn’t, on its own, append to or update the commit history.

How to Use Git Revert

To revert a specific commit, you can use the following command:

git revert <commit-hash>

This command creates a new commit that undoes the changes made in the specified commit. If the commit you’re reverting has already been pushed to a remote repository, you can push the new revert commit as well:

git push origin <branch-name>

Reverting a commit is particularly useful when you need to undo changes in a collaborative environment, as it preserves the integrity and history of the repository.

This works whether the commit you are reverting has already been pushed to remote or not.

Differences Between Git Revert and Git Reset

While both git revert and git reset can be used to undo changes, they serve different purposes and have distinct effects on the commit history.

  • Git Revert: Creates a new commit that undoes the changes introduced by a previous commit. It preserves the commit history, making it a safe choice for undoing changes in a shared repository.
  • Git Reset: Moves the current branch to a specified commit, effectively undoing all of the changes since that commit. It can modify the commit history, which can be useful for removing unwanted commits, which changes the record of that branch of the repository. It is highly recommended to avoid using this approach if the changes have already been pushed to a shared repository. Its advised use is for undoing commits that are yet to be pushed to remote.

How to Use Git Reset

The git reset command comes with different options, each serving a different purpose:

  • Soft Reset: Moves the branch pointer to a specified commit without changing the working directory or the staging area. The changes remain staged:

 git reset --soft <commit-hash>

  • Mixed Reset: Moves the branch pointer to a specified commit and updates the staging area to match, but leaves the working directory unchanged:

git reset --mixed <commit-hash>

  • Hard Reset: Moves the branch pointer to a specified commit and updates both the staging area and the working directory to match. This discards all changes after the reset point:

git reset --hard <commit-hash>

 

Note that, in the Data Productivity Cloud - it is the hard reset option that is currently available. The reset, in this context, moves the branch pointer to the last commit i.e. it removes all uncommitted changes from the current working directory.

Summary

Understanding git revert and git reset is essential for managing your project’s history and ensuring a stable codebase. While git revert is the go-to command for undoing changes safely by adding a new commit, git reset can be useful in nuanced situations, for destructive history rewrites. By choosing the right tool for the task, you can maintain a clean, transparent commit history and navigate the complexities of version control with confidence.

Thank you for joining us on our journey through its various features and functionalities. If there are any further topics you are unfamiliar with that you’d like us to cover, why not reach out and request a blog topic. Until then, happy coding and Git Good with the Data Productivity Cloud!

Catch up on parts one-eight here! 

Part One: Commit, Push, and Pull. 

Part Two: Branching 

Part Three: Exploring Common Branching Strategies 

Part Four: An In-Depth Guide To Merging

Part Five: Understanding Hard Reset 

Part Six: Unpushed Commits

Part Seven: Uncommitted Changes 

Part Eight: Merge Strategies 

Bryns Jones
Bryns Jones

Senior Engineering Manager

Bryns Jones is a Senior Engineering Manager working with the DataOps team at Matillion

Get started today

Matillion's comprehensive data pipeline platform offers more than point solutions.