Table of Contents
Git Reset Commit
The git revert command must be the action that distributed version control users misunderstand the most. To clarify the difference between the git reset and git revert commands, let’s go through an example of how to undo a Git commit.
To undo all the changes made to your source code repository by a single commit, use the git revert command. For instance, if a previous commit added the file index.html to the repository, a git revert on that transaction will delete the file from the repository. A git revert on a previous commit will erase any additional code lines that were introduced by that commit to any Java files.
Git Revert: What is it?
We must first describe the command before we can respond to the question, “How do you revert a Git commit?” The Git revert commit command is an “undo” operation that allows users to carefully undo changes in a less invasive way.
When there is a chance of losing work, Git revert is a secure, forward-looking alternative to Git reset. Git revert erases all the modifications made to the source code repository by a single commit. For instance, the Git revert command can delete a file uploaded to the repository by a commit with the name wombat.html. Alternatively, a Git revert performed on a prior commit will remove the offending line if it was introduced by the previous commit to a Python file.
Reverse a commit in Git
What would happen, in your opinion, if we performed a git revert on the third commit bearing the ID 4945db2? This git commit was responsible for including the Charlie.html file.
git@commit /c/revert example/
$ git revert 4945db2
Will the git revert of commit 4945db2 remove Charlie.html, Delta.html, and Edison.html copies from the local workspace?
Or will this git revert example delete only the Charlie.html file and leave the other four files in the local workspace?
You would be on the right if you selected the final result. This is why.
Only the changes related to a particular commit will be undone by using the git revert command. The third commit in this git revert example introduced the Charlie.html file. Charlie.html is the sole file that was deleted from our repository when we undoed the Git commit.
git@commit /c/revert example/
alpha.html beta.html delta.html edison.html
Another crucial point for developers to be aware of is that when they git revert a commit, the reverted commit is removed from the developer’s local workspace but not from the local repository. Since the code related to the reversed Git commit is still kept in the repository’s history of modifications, the reversed code can still be used as a reference if it needs to be later retrieved or reviewed.
Using git checkout, reverse uncommitted changes.
- Any changes to the content are rolled back to the commit’s original state.
- The commit history won’t be altered as a result of this.
- Use this to switch between branches or move the HEAD pointer to a given commit.
for example: First, we’ll set up a brand-new repository called “git-tut.” In this repository, we build a file called “form.html” that has a layout for the login functionality.
You can see that the “git-tut” repository has been initialized in the image above. The ‘form.html’ file located in the git-tut repository will be tracked using the git status command. Using the command git add filename, you can now add the file ‘form.html’ to the staging area. ‘form.html’ appears like this.
Once more, you added a “reset-password” box to the “form.html” file.
The ‘reset-password’ field should be removed; your peers will advise after some time. Now, you can quickly undo that change by deleting the entire “reset-password” field code, but when programming in the real world, you might have hundreds of lines of codes changed. Because doing so by hand would be very laborious, you should use the command “git checkout — filename>.”
As you can see, ‘form.html’ has been changed. Let’s execute the git checkout — form.html command now.
Our changes will be undone by this command. Let’s look at it.
However, there is a simple alternative: use the dot (.) instead of using the same command git checkout — filename> numerous times with different file names. What if you have multiple files that have been changed? A dot will undo all the file modifications you want to undo, such as those made by git checkout.
Multiple commits undone.
You can revisit any earlier revision using the same method:
$ git reset –hard 0ad5a7a6
But always remember that executing the reset command reverses any commits made after the one you returned from:
In Git, should you use reset or revert?
Reset should really only be used when a commit only exists locally. This command modifies the commit history, which could potentially replace previous versions that remote team members rely on.
Reverting is preferable if the commit to undo has already been posted to a shared repository because it doesn’t corrupt the commit history and creates a new commit that undoes the changes.
Compared reset, compare git revert.
Only the modifications linked to a particular commit are undone when your git reverts it. The cumulative changes made by later commits are unaffected. You should not use revert but rather a hard git reset if you want to undo all changes made after a specific commit.
Although they apply to different use scenarios, the git reset and revert commands are frequently mixed up. Developers should use the git revert command to undo changes related to a particular commit. Use git reset to reverse all modifications made after a specific commit. The git reverse or git reset commands are both appropriate when a developer merely needs to undo the prior commit.
A Git commit’s reversal process
As a recap, the procedures to remove undesirable modifications and revert a commit in Git are as follows:
- With the git log or reflog command, get the ID of the commit that has to be undone.
- Give the commit ID that is relevant when using the git revert command.
- To explain why the revert was required, include a compelling Git commit message.
The git reverse command makes it easy to fix a defect that was accidentally introduced into the version control system or to roll back a client-unfavorable feature upgrade. Git’s revert command is the right course of action to use if you want to reverse changes that were made in a particular commit.