How to View Commit History of a File in Git

Azhar Bashir Khan Feb 02, 2024
How to View Commit History of a File in Git

This tutorial will introduce how to view the history of the commits associated with a particular file in Git.

Git is a version control system; we use Git to keep track of changes done to files.

Often we would require to browse and view the changes done to a particular file. Git provides commands to view the history of the commits associated with a file along with complete details, like the particular changes done in the file.

We will now illustrate this with an example.

Using gitk and git log to View the History of Commits Associated With a File in Git

In a collaborative development environment, we often would like to view the changes done to a particular file in the commit history of that file in the Git repository.

We would like to find what particular change was done to a specific file in a given commit.

Say, we have the file named README.txt. We may use this file to keep the project’s information since the project’s inception.

Now, suppose we want to view that particular file’s commit history. We can use the gitk tool provided by Git for this purpose.

Gitk is a graphical repository browser. It is used to explore and visualize the history of the repository.

The syntax to view the commit history of a particular file using gitk is gitk <filename>.

Thus, view history of the file README.txt, we would execute the command as follows.

$ gitk README.txt

It launches Gitk’s graphical user interface (GUI) as below.

gitk-example1

The upper left pane shows the commits to the file README.txt in the repository, with the latest commit on the top.

The lower right displays the list of files impacted by the selected commit. It shows the path with the file name README.txt.

The lower left pane displays the commit details and full diff of the file README.txt.

Thus, the Gitk tool is great for viewing the history of the commits associated with a particular file in Git.

One can also use the command git log for similar purposes. The syntax of the command to view the commit history of a particular file is, git log -p <filename>

Thus, in our case, we would execute the command as follows.

$ git log -p README.txt
commit 8f2aa9af1a34ba8d57f60edcb6a29dfa23401e39 (HEAD -> main, origin/main)
Author: John Doe <johndoe@xyz.com>
Date:   Mon Dec 27 12:52:13 2021 +0530

    updated Readme.txt

diff --git a/project-path/README.txt b/project-path/README.txt
index 870c0a8..d09182c 100644
--- a/project-path/README.txt     
+++ b/project-path/README.txt     
@@ -1 +1,3 @@
-Initial project commit
+Further changes done
+1. This change
+2. That change

commit d25da7f49fae88a50bbc144df2429748077a2063
Author: John Doe <johndoe@xyz.com>
Date:   Mon Dec 27 12:50:53 2021 +0530

    Inital Readme.txt

diff --git a/project-path/README.txt b/project-path/README.txt
new file mode 100644
index 0000000..870c0a8
--- /dev/null
+++ b/project-path/README.txt     
@@ -0,0 +1 @@
+Initial project commit

The git log command with the -p option shows the file’s commit history and the diffs between each commit.

The Gitk tool can be considered a GUI wrapper for the git log command.

Thus, we have elaborated on how to view the history of the commits associated with a particular file in Git.

For more information, please visit -

  1. Gitk
  2. gitk - The Git repository browser
  3. git-log

Related Article - Git History

Related Article - Git Commit