Git

How to Revert a File to a Previous Commit in Git

Git is a decentralized system that helps individuals and teams to track and save changes made in the entire project. Multiple members often work on the same project files; they make changes and commit them to the Git repository. Sometimes Git users want to undo changes that they made. This operation can be performed using the “$ git revert <commit-ref>” command.

This write-up will explain the procedure of reverting a file to a recent commit in Git.

How to Revert a File to a Previous Commit in Git?

Suppose you have created a new file in the Git repository and updated it. After that, you commit changes with a message and save it to the repository. Now, you want to revert a file to the most recent commit in Git. To do so, check log history, copy the reference of the commit which you want to revert, and execute the “$ git revert <commit>” command with reference.

To understand the procedure of reverting a file to a previous commit in Git, check out the provided example.

Step 1: Launch Git Bash

Launch the “Git Bash” terminal with the help of the “Startup” menu:

Step 2: Navigate to Git Repository

Move to the Git directory using the “Git” command:

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

Here, “Git” is our directory name:

Step 3: Create Git Directory

Now, execute the “mkdir” command to create the new Git directory:

$ mkdir linux

Step 4: Move to Git Directory

Navigate to the newly created “linux” directory by utilizing the “cd” command:

$ cd linux

Step 5: Initialize Git Directory

Initialize the Git directory with the help of the provided command:

$ git init

 

Step 6: Create File

Now, run the “touch” command to create a new file:

$ touch documentation.txt

Step 7: Add File

Next, add the newly created file to the Git repository:

$ git add documentation.txt

Step 8: Commit Changes

To commit changes, run the “git commit” command with “-m” flag to specify a commit message:

$ git commit -m "file added"

Step 9: Check Log History

Now, check the log history of the current Git directory:

$ git log --oneline

Here, the “–oneline” option is used to display the one commit per line. According to the below output, directory HEAD is pointing to the most recent commit ref “3b070f5”:

Step 10: Update File

Open up the file using the “$ start” command for editing:

$ start documentation.txt

As you can see, our “documentation.txt” is opened in the default editor in which we will add some text:

After editing the file, click on the “File” option from the menu bar and hit the “Save” option:

Step 11: Commit Changes

Now, commit all changes to the Git directory for storing purposes:

$ git commit -m "documentation.txt file updated"

Step 12: Check Log History

Check the entire log history of the current Git directory by utilizing the “git log” command:

$ git log

Above command will display the commit history of the directory from where you can copy the reference of the most recent commit:

Step 13: Revert File

Now, execute the “git revert” command with copied commit ref:

$ git revert 72065bc84d46254d9226017e155c763abe2caba8

Add any text, press “CTRL + S” to save it, and then exit the default editor:

Below output indicates that our file “documentation.txt” is reverted successfully to previous commit:

Step 14: Verify Revert Operation

Lastly, execute the “git log” command to verify the revert operation:

$ git log --oneline

We have briefly explained the easiest method of reverting a file to a previous commit in Git.

Conclusion

To revert a file to a previous commit in Git, open the Git terminal, navigate to the Git repository, and create a new directory. Then, initialize it, create, and add new files to it. Update files and commit changes. Next, check the log history of the Git repository and copy the reference of the commit which we want to revert back and execute the “$ git revert <commit-ref>”. This write-up elaborated on the procedure of reverting a file to a recent commit in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.