| by Arround The Web | No comments

How to Revert Back to a Previous Commit in the Repository in Git

The independent versioning system Git is most widely used among people while working on a huge project as a team. Users work in the local repo and after that push it to save and share it with other team members. When users add, remove, or update the project files, they are required to commit all changes to the remote repository and update it. After synchronizing the local repository with the remote repository, sometimes users don’t want to keep changes/updates and attempt to revert back to a previous commit. They can do it using the “git revert” command.

In this guide, we will talk about how to revert to a recent commit in the repository in Git.

How to Revert Back to a Previous Commit in the Repository in Git?

To revert to a recent commit in the repository in Git, go through the following example.

Suppose we have added a commit to the Git repository. After that, we realize that made changes are not suitable and we want to remove them. For this specified purpose, we need to revert to a recent commit in the repository.

Now, let’s head toward the procedure.

Step 1: Open Git Bash

Open the Git terminal using the “Startup” menu:


Step 2: Navigate to Git Directory

Execute the “cd” command to move to the specific directory to revert back to the previous commit in that repository:

$ cd "C:\Users\nazma\Git\test\first_demo"

 

Step 3: Create New File

Next, create the new file in Git local directory:

$ touch file3.txt

 
Our new file “file3.txt” is created successfully:


Step 4: Add File into Git Repo

Execute the “git add” command to track the file from working directory to the staging area:

$ git add file3.txt

 

Step 5: Commit Changes

Now, commit changes to save the updates to Git repo with a commit message:

$ git commit -m "file3 added"

 

Step 6: Check Log History

To check the log history, execute the provided command:

$ git log

 
As you can see, all commit history is displayed on the screen, now copy the commit ref of the previous commit:


Step 7: Revert to Previous Commit

Now, paste the commit ref and run with “git revert” command:

$ git revert 1d3315e

 
It will open the editor, add the comment in the opened file that will display in the commit history, save changes and exit the editor:


As you can see, our commit file “file2” is deleted successfully:


Step 8: Verify Revert Commit

Lastly, to verify the revert back to a previous commit, execute the “git log” command:

$ git log --oneline

 

We have provided the easiest way to revert back to a previous commit in the repository in Git.

Conclusion

To revert to a recent commit in the repository in Git, first, navigate to the specific directory in which you want to make changes. Then create and add the file to the Git local repo. Next, commit changes with the “$ git commit -m” command with a message and check the log history by executing the “$ git log” command. Copy the commit ref of the previous commit, execute the “$ git revert” command with the commit ref and verify it. This guide illustrated the method of reverting to a recent commit in the repository in Git.

Share Button

Source: linuxhint.com

Leave a Reply