Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I blame a deleted file in Git?

Tags:

git

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?

like image 371
Vincent Scheib Avatar asked May 07 '16 03:05

Vincent Scheib


People also ask

How do I blame a file in git?

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.

What to do when git deleted files?

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.

Does git add include deleted files?

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.

What does blame button do in GitHub?

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.


1 Answers

git blame

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

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)

like image 83
Vincent Scheib Avatar answered Sep 21 '22 00:09

Vincent Scheib