Git Revert Command

Git Revert Command

In Git, the Git Revert command is used to revert some changes. The git revert command is used to apply revert operation. It is an undo type command. However, it is not a traditional undo alternative. It does not delete any data in this process; instead, it will create a new change with the opposite effect and thereby undo the specified commit.

It can be useful for tracking bugs in the project. If you want to remove something from history then git revert is a wrong choice.

The Git Revert command is used for undoing changes to a repository’s commit history. Other ‘undo’ commands like, git checkout and git reset, move the HEAD and branch ref pointers to a specified commit. Git Revert also takes a specified commit, however, Git Revert does not move ref pointers to this commit.

We can say that revert records some new changes that are just opposite to previously made commits. To undo the changes, run the below command:

$ git revert

Git Revert Options:

Revert command allows some additional operations like editing, no editing, cleanup, and more. Let’s understand these options briefly:

< commit>: The commit option is used to revert a commit. To revert a commit, we need the commit reference id. The git log command can access it.

$ git revert <commit-ish>  

<–edit>: It is used to edit the commit message before reverting the commit. It is a default option in revert command.

$ git revert -e <commit-ish>  

-m parent-number /–mainline parent-number: it is used to revert the merging. Generally, we cannot revert a merge because we do not know which side of the merge should be considered as the mainline. We can specify the parent number and allows revert to reverse the change relative to the specified parent.

-n/–no edit: This option will not open a text editor. It will directly revert the last commit.

$ git revert -n <commit-ish>  

–cleanup=<mode>: The cleanup option determines how to strip spaces and comments from the message.

-n/–no-commit: Generally, the revert command commits by default. The no-commit option will not automatically commit. In addition, if this option is used, your index does not have to match the HEAD commit.

The no-commit option is beneficial for reverting more than one commits effect to your index in a row.

Reverting to Previous Commit:

Suppose you have made a change to a file say newfile2.txt of your project. And later, you remind that you have made a wrong commit in the wrong file or wrong branch. Now, you want to undo the changes you can do so. Git allows you to correct your mistakes

So I have upadated one file called cloudnclear.txt using “git add cloudnclear.txt” and  then committed the changes using git commit -m ” updated file cloudnclear.txt”

We can undo it by revert command. To undo the changes, we will need the commit-ish. To check the commit-ish, run the below command:

$ git log  

git revert

In the above output, I have copied the most recent commit-ish to revert. Now, I will perform the revert operation on this commit. It will operate as:

$ git revert 099a8b4c8d92f4e4f1ecb5d52e09906747420814  

 Git Revert Merge

In Git, merging is also a commit that has at least two parents. It connects branches and code to create a complete project.

A merge in Git is a commit that has at least two parents. It brings together multiple lines of development. In a work-flow where features are developed in branches and then merged into a mainline, the merge commits would typically have two parents.

For more Git Related post click here.

  1. Git init Command
  2. Git add Command
  3. Git Commit Command
  4. Git Clone Command
  5. Git Config Command
  6. Git Alias Command
  7. Git Checkout Command
  8. Git Pull Command
  9. Git Push Command
  10. Git Fetch Command
  11. Git Status Command
  12. Git Remove (rm) Command
  13. Git LOG Command
  14. Git Cheat Sheet