How to find the git log or commit history for a specific file:
There are different ways to find the commit history for a specific file. We can use gitk or git to get the history of a specific file.
For graphical view of the log, we can use gitk:
It will open one GUI with the history for the file which file is provided in filepath.
Note that, you need to install gitk to use this command.
If you want the events of rename, i.e. to show when the file was renamed, you can use this command:
gitk --follow <filepath>
If you want to view all in the terminal itself, you can use the below command:
git log -p <filepath>
-p is used to show all patches, i.e. the code changes. If you don’t use -p, it will show only the commit names.
You can also use —follow with this command to get the information including file name changes.
git log -p --follow <filepath>
Other third party Apps:
git log for a specific file is supported by almost all popular git GUI clients. For example, for sourcetree, if you select one commit, it shows the file names. You can right click on a file name and click on Log Selected to view all git commit history.
We can use any one of these methods to find the history of a file. If the file has too many history, GUI client gives an easier way to view the changes than command line.