Home > OS >  Get all the old names of a renamed file in git
Get all the old names of a renamed file in git

Time:12-24

I want to check using git if a file was renamed anytime in the past or not, and if yes, retrieve all the old names of the file. Is there a way to do this?

CodePudding user response:

Git tracks content, not files. However, there is a built-in way to ask git to try to reconstruct back this history by following renames :

git log --follow -- <path/to/file>

(doc)


So, to get only the list of names the file had, filter your log output and sort it out like this for example :

git log --pretty=format:"" --name-only --follow -- <path/to/file> | sort -u

where
--pretty=format:"" inhibits all commit output except for... (see just below)
--name-only outputs file names only
--follow -- <path/to/file> turns on the rename detection feature mentioned above
and finally
| sort -u sorts the output and keeps only unique entries

  •  Tags:  
  • git
  • Related