To view the history of a file in Git, you can use the
git log command. This command will show you a list of all the previous versions of the file, along with information about the commit that introduced the change, such as the commit message and the author.
To view the commit history of a file, use the following command:
$ git log -- path/to/file
path/to/file with the path to the file, you want to view the history of.
This shows the commits that touch the file.
If you want to see more information, such as what changed in each commit, use the
--patch option, which will show you the changes made in each commit.
$ git log -p -- path/to/file
You can also use the
--follow option to show the history of a file that has been renamed. This will show you the history of the file under its previous name as well as its current name.
$ git log --follow -- path/to/file
Let’s take a look at a concrete example to help understand the process better.
Here’s my example project with a text file:
And here are the three most recent commits each of which changes the test.txt file contents.
Now, let’s inspect the changes introduced in the three most recent commits.
$ git log -p -- test.txt
Here’s what the output looks like:
Pretty neat, isn’t it? This way you can easily see the changes made in each commit in the commit history of the file.
How to Get Author Information for Changes?
To show what revision and author last modified each line of a file in Git, you can use the
git blame command. This command will show you information about each line in a file, including the revision in which the line was last modified and the author who made the change.
git blame, navigate to the directory containing the file you want to view, and use the following command:
$ git blame path/to/file
path/to/file with the path to the file you want to view.
git blame will show you the most recent version of the file. If you want to view an older version of the file, you can specify the revision using the
-L option, followed by the range of lines you want to view.
For example, to view lines 10 through line 20 of an older version of the file, you would use the following command:
$ git blame -L 10,20 path/to/file
You can also use the
-M option to detect lines that have been moved or copied within the file, and the
-C option to detect lines that have been copied from another file.
Let’s see a concrete example of this as well. In my example project, I have the following files:
Besides, I’ve made some changes to the test.txt file in the most recent commits.
Even though I’m the solo contributor to the project, let’s see how the git blame command works and how you can use it to see the author’s information.
Let’s see what the git blame looks like when run on the test.txt file:
$ git blame test.txt
This shows the additions I’ve made to the files in each commit. It shows that the author is me and what the change I made was.
The git blame command can be useful if you’re looking for reasons for some code changes in the past and cannot quite wrap your head around the change. The blame reveals the change history and points you out to the person that might know more about the change.
As another example, let’s only target the first line of the test.txt file. In other words, let’s only see what changes have been made to the very first line of the file.
$ git blame - L 1,1 test.txt
Notice that now the other changes made to this file are not visible. This is because the blame only cares about the first line. In this case, it only shows the commit I did to change the first line once.
Today you learned how to view the file changes made to a file in Git.
To take home, git log — path/to/file shows all commits that change a particular file. To also see what’s changed, use the -p flag: git log -p — path/to/file. And to view who made the change, use git blame path/to/file.
Thanks for reading. Happy Gitting!