Git blame helps when investigating why code in a file is a certain way. git gui is even better in that it allows you to step backwards in time to see the context of the file when code was added.
However, git blame <file>
and git gui blame <file>
do not work after a file has been deleted. An error will appear as:
fatal: cannot stat path 'file': No such file or directory
How does one blame a deleted file?
The git blame command is used to examine the contents of a file line by line and see when each line was last modified and who the author of the modifications was. The output format of git blame can be altered with various command line options.
Git provides ways to recover a deleted file at any point in this life cycle of changes. If you have not staged the deletion yet, simply run `git restore <filename>` and the file will be restored from the index.
Add All Files using Git Add. The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area.
From GitHub: The blame command is a Git feature, designed to help you determine who made changes to a file. Despite its negative-sounding name, git blame is actually pretty innocuous; its primary function is to point out who changed which lines in a file, and why.
git blame
works when providing a commit reference that contains the file. Find the most recent one with log:
$ git log -2 --oneline -- example/path/file.txt fffffff deleting file.txt eeeeeee Last change to file.txt before deleting.
Then blame the parent commit:
$ git blame eeeeeee -- example/path/file.txt
git gui blame
won't work this way, however. A work around is to browse the repository at the last commit that contained the file, then from the GUI select the file and launch the blame viewer:
$ git gui blame eeeeeee example/path/file.txt
(Note: Use log -2
and eeeeeee
instead of fffffff^
because git gui blame
can not handle fffffff^:example/path/file.txt
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With