| by Arround The Web | No comments

git-revert Command in Git | Explained

Git is the distributed version control system that is used for tracking development project source code files locally, then pushes them to the centralized system. All made changes are pushed into the Git repository through the commit. Each commit has a unique id that represents the commit message. Users can view any commit changes through the commit SHA hash and also revert if needed. For this purpose, the “git revert” command can be used.

This post will talk about:

What is the “git revert” Command in Git?

The “git revert” command is utilized for removing the changes to a Git repository’s commit history. It takes the particular commit SHA hash, reverts the made changes from that commit, and generates a new “revert commit” commit. Additionally, the HEAD pointer is updated and points to the new revert commit making it the tip of the current working branch.

How to “git revert” Commit Changes in Git?

To revert any specific commit changes, follow the provided instructions:

  • Navigate to the Git local repository.
  • List the current repository content and choose the file which needs to revert from the Git repository.
  • View Git log history and select the particular commit SHA hash.
  • Run the “git revert <commit_id>” command.

Step 1: Move to Git Repository

Initially, use the “cd” command and go to the particular Git local repository:

cd "C:\Users\nazma\Git\Coco"

Step 2: List Existing Content

To list the repository’s content, run the “ls” command:

ls

From the given output, we have selected the “file1.py” file for further processing:

Step 3: Check Git Log History

Execute the “git reflog .” command to view the current repository log history:

git reflog .

Here, we have selected the “59bd8e1” commit SHA hash to revert this commit:

Step 4: Revert Commit Changes

To revert the selected commit changes, run the “git revert” command:

git revert 59bd8e1

When the above command has been executed, the default text editor will open. Add a commit message, save changes, and close the text editor. For instance, we have typed the “Revert “1st file added”” as the commit message:

As you can see, all the changes that exist in the particular commit have been deleted successfully:

Step 5: Verify Revert Operation

To ensure that the particular changes are reverted or not, use the “git reflog .” command:

git reflog .

As you can see, HEAD points to the most recent commit which indicates that commit changes have been reverted:

That’s all! We have compiled the details about the “git revert” command in Git.

Conclusion

The “git revert” command is used to revert specific commit changes. To do so, navigate to the Git local repository and list the current repository content. Then, view the Git log history and select the particular commit SHA hash. After that, execute the “git revert <commit_id>” command. This post demonstrated the usage of the “git revert” command in Git.

Share Button

Source: linuxhint.com

Leave a Reply