Git

View the Change History of a File Using Git Versioning

Git is a decentralized versioning control system used by developers to track their projects and keep the added changes in the project’s source code files. Developers can perform multiple operations through several commands. Additionally, they can manage their working history and view it whenever needed, along with complete details of changes.

This article illustrates the method of viewing the changing history of a file by utilizing Git versioning.

How to Display the Change History of a File Utilizing Git Versioning?

To show the added change history of a file utilizing the Git versioning, first, navigate to the Git local repository and list the content of the current repository. Open the desired file, add some text, and save it. Track the file and commit changes to the repository. Next, execute the “$ git log — <file-name>” command or the “$ git log -p <file-name>” command to view the change history of a file.

Step 1: Move to Local Repository

First, navigate to the particular Git local repository:

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

Step 2: View Content List

Execute the “ls” command to view the content list of the present repository:

$ ls

Step 3: Update Required File

Now, open the required file by utilizing the “start” command:

$ start file3.txt

After executing the above-stated command, the specified file will be opened in the default editor. Add some text and press the “CTRL + S” keys to save the changes:

Step 4: Track Changes

Next, run the “git add .” command to track all changes to the staging area from the working directory:

$ git add .

Step 5: Update Repository

Now, update the repository with the added changes by committing through the “git commit -m” command and add the desired commit message:

$ git commit -m "file3.txt updated"

Step 6: View File Change History

To view the change history of the file, execute the “git log” command with “” and specify the file name:

$ git log -- file3.txt

As you can see, the below-stated output indicates the entire change history of the specified file:

There is another command that is used to view the change history of a file using Git versioning as follows:

$ git log -p file3.txt

It can be observed that the commit history along with the change history is displayed:

Let’s move ahead to the other method to view the change history of a file using Gitk’s GUI.

How to Display the Change History of a File Utilizing Gitk’s GUI?

Gitk is a graphical repository browser used to visualize the history of the repository or its present files. To know the change history of a specified file using Gitk, run the below command:

$ gitk file3.txt

Here, it can be observed that when the above-provided command is executed, Gitk’s (GUI) is launched with specified file change history details:

We have provided the simplest methods to view the change history of the file with the help of Git versioning.

Conclusion

To view the change history of a file using Git versioning, first, navigate to the desired repository and view the list of the current repository content. Open the desired file, add some text, and save it. Track the file and commit changes to the repository. Next, execute the “$ git log — <file-name>” command. The “$ git log -p <file-name>” command can also be utilized to view the change history of a file. Another way to view the change history is Gitk’s GUI. For this purpose, run the “$ gitk <file-name>” command. This article explained the procedure of viewing the changing history of a Git file utilizing Git versioning.

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.