Ione Souza Junior

Git Reset and Git Revert: Managing Mistakes in Git

March 22, 2024 | 5 Minute Read | Translations: pt | #git

In the software development process, mistakes are inevitable. Whether it's a misplaced line of code or a misguided commit, every developer has encountered the sinking feeling that accompanies an error in their work. Yet, in the realm of version control systems, such as Git, these missteps need not spell disaster. Git reset and Git revert serve as beacons of hope amidst the tumultuous sea of code changes, offering developers the means to rectify errors, backtrack on misguided decisions, and steer their projects back on course. In this post, we'll embark on a journey into the heart of Git, exploring the nuances of Git reset and Git revert, uncovering their capabilities, and arming ourselves with the knowledge needed to navigate the choppy waters of version control with confidence. So, let's dive in and discover how Git reset and Git revert can be our steadfast companions in the pursuit of flawless code and seamless collaboration.

Understanding git reset

Git reset is a versatile command that allows developers to manipulate the state of their repository’s staging area and working directory. At its core, Git reset serves as a tool for undoing changes and resetting the current HEAD to a specified state. However, its functionality extends far beyond mere undoing, offering developers a range of options for navigating their project’s history and managing the progression of commits.

One of the primary functions of Git reset is to move the HEAD pointer to a different commit, effectively resetting the state of the repository to that particular snapshot in time. This action can be performed with varying degrees of granularity, allowing developers to reset the HEAD to a specific commit, branch, or even a previous state of the working directory. Let’s see the command and its main variations.

Moving HEAD to a specific commit

Git reset moves the HEAD pointer to the specified commit, resetting the staging area and working directory to match the state of that commit. Changes made after this commit will be unstaged. All the files that you have changed between the pointer you move and the pointer you were before will be unstaged.

git reset <commit-hash>

Soft reset

Soft reset moves the HEAD pointer to the specified commit while preserving changes in the staging area. This allows you to recommit the changes or make further modifications before committing.

git reset --soft <commit-hash>

Hard reset

A hard reset moves the HEAD pointer to the specified commit and resets both the staging area and working directory to match. Any changes made after this commit will be permanently discarded.

git reset --hard <commit-hash>

Hard reset with HEAD as the argument discards all local changes and resets the working directory to match the current commit. This is useful for reverting to a clean state without preserving any modifications.

git reset --hard HEAD

Unstaging changes

Suppose you already put some changes into staged area and want to move to the unstaged area. The following command unstages a specified file, removing it from the staging area while preserving the changes in the working directory.

git reset HEAD <file>

Also, you can omit the file name to apply the command for all staged files.

Understanding git revert

While git reset modifies the commit history by moving the HEAD and branch references to a specified commit, git revert takes a different approach. Instead of altering the commit history, git revert creates a new commit that undoes the changes introduced by a specified commit.

git revert <commit-hash>

When you run the command above, Git will create a new commit that inversely applies the changes made by a specified commit. This effectively undoes the changes introduced by that commit without altering the commit history. It’s important to note that git revert operates on the working directory and staging area, creating a new commit with changes that reverse those introduced by a specified commit.

For example, suppose you have a commit with hash “abc123” that introduced a bug, and you want to revert the changes introduced by that commit. You can use git revert abc123 to create a new commit that undoes the changes made by “abc123”, effectively fixing the bug without altering the commit history.

Unlike git reset, which can be used to rewind the commit history, git revert is best suited for undoing specific commits while preserving the integrity of the commit history. It’s a safer option when working in shared repositories or when you need to maintain a clean and linear commit history.

You can revert more than one commit at a time.

git revert <commit-hash-1> <commit-hash-2>

Or a range of commits, from oldest to newest.

git revert <oldest-commit-hash> <newest-commit-hash>

Git reset vs. git revert: choosing the right tool

For those who prefer visual aids, here’s a table highlighting key distinctions between the commands.

Feature git reset git revert
Operation Rewrites commit history Creates new commit to reverse changes
Effect on history Rewinds to specified commit Reverts specified commit’s changes
Commit hash Moves HEAD and branch pointer Creates new commit with reverted changes
Collaboration Can be disruptive in shared repositories Safe for shared repositories, preserves history
Use case Undo local changes or prepare for recommit Revert changes in shared history
Loss of history Can result in loss of uncommitted changes Preserves commit history, creates new revert commit

Harnessing Git’s undo powers

In the world of software development, mistakes are inevitable, but they needn’t be permanent. With Git’s powerful “reset” and “revert” commands at your disposal, you hold the keys to managing mistakes and navigating through your project’s history with confidence. By understanding the nuances of each command and applying best practices, you can wield Git’s time-traveling capabilities to your advantage, ensuring a smoother and more efficient development process. So embrace the power of Git, learn from your mistakes, and continue your journey towards becoming a Git guru.