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:
Here, “Git” is our directory name:
Step 3: Create Git Directory
Now, execute the “mkdir” command to create the new Git directory:
Step 4: Move to Git Directory
Navigate to the newly created “linux” directory by utilizing the “cd” command:
Step 5: Initialize Git Directory
Initialize the Git directory with the help of the provided command:
Step 6: Create File
Now, run the “touch” command to create a new file:
Step 7: Add File
Next, add the newly created file to the Git repository:
Step 8: Commit Changes
To commit changes, run the “git commit” command with “-m” flag to specify a commit message:
Step 9: Check Log History
Now, check the log history of the current Git directory:
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:
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:
Step 12: Check Log History
Check the entire log history of the current Git directory by utilizing the “git log” command:
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:
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:
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.