Git is a version control system and code management tool. Git allows us to store different versions of our project with the help of Git Commits. Commits are like a snapshot of the state of our repository. Different commits contain different revisions of a file.
We may sometimes need to view a previous version of a file. Or, we need to undo some changes and take our file back to a specific revision. Let's learn how to restore a file to a previous revision.
Sometimes, we may want to view the changes between two versions of a file. We don't want to reset or revert our file to the previous state. For such scenarios, we can use the Git Diff command.
The Git Diff command tells us the differences between two entities. These entities can be commits, branches, or even versions of a particular file.
To view the differences between the current version and a previous version of a file, we need to pass the hash of the commit in which the previous version is stored.
git diff <commit-hash> <file-name>
Use the following code to view the differences between two versions present in different commits(not the current commit, HEAD).
git diff <commit-1-hash> <commit-2-hash> <file-name>
For example, consider a repository with four commits(A, B, C, and D).
A -- B -- C -- D <- master <- HEAD
We have a file called demo.txt. We can view the contents of the file by using the cat command.
cat demo.txt
This line was added in Commit A.
This line was added in Commit B.
This line was added in Commit C.
This line was added in Commit D.
We need to view the changes between its current version and the version stored in commit B. We will use the Git Diff command and pass the file name(demo.txt) and the hash of commit B. Use the Git Log command to find the commit hash.
git diff <commit-B-hash> demo.txt
Output
diff --git a/demo.txt b/demo.txt
index 8a3968d..2e423b4 100644
--- a/demo.txt
+++ b/demo.txt
@@ -1,2 +1,4 @@
This line was added in Commit A.
-This line was added in Commit B.
\ No newline at end of file
+This line was added in Commit B.
+This line was added in Commit C.
+This line was added in Commit D.
\ No newline at end of file
Let's view the differences between commit A version and commit C version of the demo.txt file.
git diff <commit-A-hash> <commit-C-hash> demo.txt
Output:
diff --git a/demo.txt b/demo.txt
index 155851d..a06c6cb 100644
--- a/demo.txt
+++ b/demo.txt
@@ -1 +1,3 @@
-This line was added in Commit A.
\ No newline at end of file
+This line was added in Commit A.
+This line was added in Commit B.
+This line was added in Commit C.
\ No newline at end of file
The Git Checkout command can revert a file to a previous version. First, we will go back to the earlier version using the following Git Checkout command. We need to pass the file name. If file is not in the root directory, then pass the complete file path. We also need the hash of the commit that stores the required file version.
git checkout <commit-hash> -- <file-name>
Next, we need to commit to this change. It is required because the Git Checkout command only fetches the previous version and updates our file in the working tree. The changes in the file are staged. We need to use the Git Commit command so that our repository reflects these changes.
git commit -m "commit message"
Consider a repository with four commits(A, B, C, and D).
A -- B -- C -- D <- master <- HEAD
We have a file called demo.txt. The current contents of the file are shown below. Use the cat command to view a file.
cat demo.txt
This line was added in Commit A.
This line was added in Commit B.
This line was added in Commit C.
This line was added in Commit D.
We want to restore the version of demo.txt that was included in commit B. We will use the following Git Checkout command. Pass the file name and the hash of commit B. Use the Git Log command to find the commit hash.
git checkout <hash-of-commit-B> demo.txt
Now, we can check the contents of the file.
cat demo.txt
This line was added in Commit A.
This line was added in Commit B.
These changes in the demo.txt file are staged for committing. We can verify this by running the Git Status command.
git status
Output:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: demo.txt
Finally, we can create a new commit to store these modifications.
git commit -m "Reverted demo.txt back to its commit B version"
Git stores different versions of our project and allows us to go back to a previous revision. We can also reset files to their earlier versions. We can use the Git Diff command to compare changes between different versions of a file. To restore a previous version of file, we can use the Git Checkout.
The following steps summarize how to reset a file to an earlier version.