Sitemap

Git Revert and Git Reset

3 min readMar 21, 2023

--

Git is a powerful and popular version control system that allows developers to manage their codebase efficiently. Two essential Git commands that are used to manage changes made to a repository are “git revert” and “git reset.” Both of these commands are used to undo changes, but they work in different ways and are used for different purposes.

Git Revert:

The “git revert” command is used to undo a specific commit or a range of commits. This command creates a new commit that undoes the changes made in the original commit(s). This means that the original commit(s) remains in the commit history, but the changes made in it are reversed.

To use the “git revert” command, you need to specify the commit or range of commits you want to undo. For example, to undo the last commit, you would run the following command:

git revert HEAD

This will create a new commit that undoes the changes made in the last commit. You can also revert a specific commit by specifying its SHA-1 hash, for example:

git revert ece998f

This will create a new commit that undoes the changes made in the commit with the SHA-1 hash “ece998f”.

Git Reset:

The “git reset” command is used to undo changes made to the local repository by resetting the repository’s state to a specific commit. This command removes commits from the commit history and can be used to discard changes made to a branch.

Get Sankalp Salve’s stories in your inbox

Join Medium for free to get updates from this writer.

There are three modes of the “git reset” command: soft, mixed, and hard.

  • Soft reset: This mode moves the HEAD pointer to the specified commit, but keeps the changes made in the commits after that. This is useful when you want to undo the last commit but keep the changes made in it.
git reset --soft HEAD~1
  • Mixed reset: This mode moves the HEAD pointer to the specified commit and resets the index to that commit, but does not modify the working directory. This is useful when you want to undo the last commit and unstage the changes made in it.
git reset --mixed HEAD~1
  • Hard reset: This mode moves the HEAD pointer to the specified commit, resets the index to that commit, and modifies the working directory to match the state of the repository at that commit. This is useful when you want to discard all changes made in the commit and go back to the state of the repository at the specified commit
git reset --hard HEAD~1

Note that the “git reset” command can be dangerous as it discards commits and changes permanently. It is recommended to use this command with caution.

In conclusion, both “git revert” and “git reset” commands are essential for managing changes made to a repository in Git. “Git revert” undoes specific commits by creating new commits that reverse the changes made in them, while “git reset” can be used to undo changes made to a branch by resetting the repository’s state to a specific commit. It is important to understand how these commands work and when to use them, as they can have a significant impact on the repository’s history and the changes made to it.

END OF THE ARTICLE.

Do you know ?, Git Branching is a powerful and essential feature in Git that allows developers to work on different versions of a project simultaneously. It enables you to experiment with new features and ideas without affecting the main codebase. Learning about Git Branching can be a game-changer in your workflow and career as a developer. With Git Branching, you can collaborate with other developers, manage complex projects with ease, and track changes more efficiently. By mastering Git Branching, you can gain a deeper understanding of version control and enhance your coding skills. So, if you’re passionate about programming and want to take your skills to the next level, learning about Git Branching is a must.

--

--

Sankalp Salve
Sankalp Salve

Written by Sankalp Salve

Tech, history, and physics enthusiast. Writing stories that explore the evolution of technology, the impact on society, and the fundamental laws of our universe