Loading...
GitVCS

Git Reset Vs Git Revert

In this article I will explain the usage of two important git commands git reset and git revert. Both the commands are used to undo changes or commits, but there are significant differences in the way they work.

Git Reset

Git reset is a powerful command that is used to undo local changes and delete commits from repository depending on how it is invoked.

Undo Local Changes:

Let’s say, you have done changes (modified some files, added new files etc.) in your local repository and you want to undo the changes. You can do it by using following reset command-

git reset --hard

# Resetting particular file
git reset <file-name>

The --hard option will undo all the changes or modifications that you have done in files, and move files back to working area from staging area.

If you want to keep the changes but remove files from staging area, use --soft flag.

git reset --soft

Deleting Commits:

If we do git reset to a particular commit, it will move the HEAD pointer to that commit and delete all the commits above in the git history.

Let’s consider the following sample git commit history.

git log --oneline

# Output:
5986dd4 (HEAD -> dev, origin/dev) Modify invalid login message
8ff6ad5 Typo fix
bb80604 CSS fix
c64b176 Modify login page design 
0c633c0 Create SignUp Page
0bad73e Login Validation
052e288 Implement Login

We have couple of commits in the dev branch. The HEAD is currently pointing to the latest commit (5986dd4).

Now, suppose you want to undo the last commit. Following command will serve our purpose-

git reset --hard <commit-hash>

# Example:
git reset --hard 8ff6ad5

# Latest commit can also be deleted using Head
git reset --hard Head~1

`After resetting, let’s check the git commit history:

git log --oneline

# Output:
8ff6ad5 (HEAD -> dev, origin/dev) Typo fix
bb80604 CSS fix
c64b176 Modify login page design 
0c633c0 Create SignUp Page
0bad73e Login Validation
052e288 Implement Login

As you can see, the latest commit (5986dd4) – Modify invalid login message, has been deleted and there is no trace/record in git log. Now the HEAD is pointing to commit (8ff6ad5), where we did reset.

Let’s try to reset to 0bad73e Login Validation commit and check git log.

git reset --hard 8ff6ad5
git log --oneline

# Output:
0bad73e- (HEAD -> dev, origin/dev) Login Validation
052e288 Implement Login

Oops! What did you notice. All the commits above 0bad73e has been deleted. This is why we should not perform git reset for the commits which has been pushed to remote repository. It should be done in local repository only, if needed.

To revert commits pushed to remote repository we should always use git revert command,

Never perform git reset in remote repository commits, it might result in data loss.

Git Revert

Git revert works a little different way compared to git reset. If we revert a particular commit, it will undo all the changes associated with that particular commit and it is recorded in the repository. It will add a new commit as – Revert <Reverted Commit Message> in the git commit history.

git log --oneline

# Output:
8ff6ad5 (HEAD -> dev, origin/dev) Typo fix
bb80604 CSS fix
c64b176 Modify login page design 
0c633c0 Create SignUp Page
0bad73e Login Validation
052e288 Implement Login

Let’s revert the 8ff6ad5 commit and check log.

git revert bb80604 
git log --oneline

# Output:
8sr6ad5 (HEAD -> dev, origin/dev) Revert "CSS fix" 
8ff6ad5 Typo fix
bb80604 CSS fix
c64b176 Modify login page design 
0c633c0 Create SignUp Page
0bad73e Login Validation
052e288 Implement Login

As we can see in the log, an additional commit has been performed by git and all changes associated with the commit has been undone.

In this way we can safely revert commits which has been already pushed to remote repository.

Summary

So, we can conclude that git revert should be used to undo changes on a public branch, and git reset should be reserved for undoing changes on a private branch (i.e. your working directory). You can also think of git revert as a tool for undoing committed changes, while git reset HEAD is for undoing uncommitted changes.

Share this article with your friends