13. Undoing Local Changes with Git: Revert and Reset

When we work with code versioning using Git, it is common to make changes that, for some reason, we want to undo. Whether because we made a mistake or because we want to return to a previous state of the project, Git offers powerful tools for undoing local changes: git revert and git reset.< /p>

The git revert

Command

The git revert command is used to undo the changes introduced by a specific commit, creating a new commit with the reverted contents. This is a safe way to undo changes as it does not alter the repository history. In other words, git revert is a way to "go back in time" without erasing the history of how you got there.

git revert [commit_hash]

To use git revert, you need to specify the hash of the commit you want to revert. Git will then create a new commit that is the exact inverse of the specified commit. This is useful when you need to keep track of what has been undone, or when you are working in a shared repository where deleting the history would be problematic for other contributors.

The git reset

Command

The git reset command, on the other hand, is a more drastic tool. It is used to reset the current state of your repository to a previous commit, discarding changes that were made after that commit. There are three main ways to use git reset: --soft, --mixed and --hard.

Soft Reset

git reset --soft [commit_hash]

--soft keeps all your current changes in the "staged changes" state. This is useful when you want to reset your repository's HEAD to a previous commit, but want to keep the changes you made to make a new commit with them.

Reset Mixed

git reset --mixed [commit_hash]

--mixed is the default git reset option. It will undo the commit and the changes you made will be kept in your working directory, but will not be staged. This means you will have to add the changes again before you can commit them.

Reset Hard

git reset --hard [commit_hash]

--hard is the most radical option. It will reset the HEAD, index and working directory to the state of a previous commit. All changes made after the specified commit will be lost. This option should be used with caution as there is no way to recover discarded changes.

Choosing between Revert and Reset

The choice between git revert and git reset depends on the situation and what you want to achieve. If you are working on a shared repository and want to undo a commit without affecting other contributors, git revert is the appropriate choice. If you're working locally and have done something you want to completely undo, git reset might be the way to go.

Practical example

Let's assume you made a commit that, upon review, you realized shouldn't have been made. To revert this commit, you can use:

git revert [commit_hash]

Git will create a new commit that undoes the changes from the specified commit. If you want to go back to a previous state of the project and discard all commits made after a certain point, you can use:

git reset --hard [commit_hash]

This will change the state of your working directory to the state of the specified commit, discarding all changes made since then.

Be careful when undoing changes

It is important to be careful when undoing changes, especially when using git reset --hard. Once changes are discarded with this option, they cannot be recovered unless you have backed them up in some way. Always check twice before using commands that may result in data loss.

Conclusion

Git is a powerful tool for version control, and understanding how to undo changes is critical to managing your code effectively. git revert and git reset are valuable commands that give you different levels of control over your repository's history and the state of your project. By using them with care and understanding, you can navigate your code history and manage your changes with confidence.

Now answer the exercise about the content:

Which of the following statements about the `git revert` and `git reset` commands is correct?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Branching: what are branches?

Next page of the Free Ebook:

14Branching: what are branches?

6 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text