Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the new path of a renamed file whose original name is known?

Consider following usecase:

I'm working on my local branch while refactoring has been done on the main branch. Now some files (classes) have been renamed and/or moved to new locations. When merging i get lots and lots of import-errors due to missing classes.

--A--B--C--D--E-- (master)
   \           \
    F--G--H--I--J  (topic)

In A there are the old names wich i used in F--G--H--I.

In B--C--D--E the files are refactored resulting in new file names in E. This includes chained renames like

   B: path/to/File.java              
-> C: path/to/BetterName.java
-> D: better/package/BetterName.java 
-> E: final/package/FinalName.java

Now merging results in J with many (compilation) errors due to the missing refactorings on my branch. (Because i still refer to path.to.File instead of final.package.FinalName

In order to fix the broken build, i need to know the new names for the old classes.

Is there a git command to get all the renames that have been applied to a particular file?

like image 225
helt Avatar asked Apr 17 '12 13:04

helt


People also ask

How do I find the original file name after rename?

Look in the Metadata panel and at the top, change it to EXIF & IPTC view so that you see the right fields. The original filename should be listed there.

Can you see if a file has been renamed?

There's no way to tell for sure whether a file has been renamed. When a file is renamed, its inode number doesn't change. (This may not be true for “exotic” filesystems, such as network filesystems, but it's true for all “native” Unix filesystems.)

What happens if you rename a file in Git?

Git keeps track of changes to files in the working directory of a repository by their name. When you move or rename a file, Git doesn't see that a file was moved; it sees that there's a file with a new filename, and the file with the old filename was deleted (even if the contents remain the same).

Can we rename a file in Git?

You can use the command line to rename any file in your repository. Many files can be renamed directly on GitHub, but some files, such as images, require that you rename them from the command line.


1 Answers

Git should find the renames of the files. However, if the file has had 50% or more lines change in that file, it will no longer be considered a rename. You can change this threshold if you wish.

Here is the command to do it:

git log -M --diff-filter=R --stat

It will only show you the renames. If you want to change the threshold to something else other than the default 50%, you can just add the number to the M option:

git log -M90 --diff-filter=R --stat

will only consider a file as renamed if the contents have changed by no more than 10%

UPDATE:

To skip all the intermediate steps, use diff instead. Git will follow the renames for you:

git diff -M --diff-filter=R --name-status ..master | cut -f2-

when you are on your topic branch.

You can then pipe this to make a bunch of sed instructions to adjust your references.

like image 136
Adam Dymitruk Avatar answered Oct 21 '22 07:10

Adam Dymitruk