Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify Git Rename after changes

Tags:

git

Due to the tools I usually use, I often rename a file, and make changes to it, before getting ready to commit.

At this point, git no longer detects the file as a simple rename, because the file contents are different.

Is there a command I can use to specify that some file, is actually also a rename of another file, even though the contents appear different?

like image 816
Robert Christ Avatar asked Mar 04 '15 16:03

Robert Christ


People also ask

How does git handle file rename?

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).

Is there a git rename?

We use the git mv command in git to rename and move files. We only use the command for convenience. It does not rename or move the actual file, but rather deletes the existing file and creates a new file with a new name or in another folder.

How does git detect rename?

So to detect renames is a matter of comparing hashes. To detect small changes to a renamed file, Git uses certain algorithms and a threshold limit to see if this is a rename. For example, have a look at the -M flag for git diff . There are also configuration values such as merge.


2 Answers

Not in Git. Git uses similarity-based heuristic rename detection (you might need to enable it for viewing changes: git diff -M, or git config diff.renames true to always use it (or git diff -C and git config diff.renames copies to also detect copies). It detects renames on-the-fly.

It doesn't do rename tracking, and does not store information about renames anywhere.


That's assuming that you did not forget to include renamed file in the commit, i.e. you used git mv <old> <new>... which is just mv <old> <new> && git add <new>.

This means that if you have renamed file using other tools (e.g. IDE, filemanager, etc.), then you need to git add new file (after rename), and delete old file if it exists (git rm to remove it entirely, git rm --cached to keep it in the working directory).

like image 111
Jakub Narębski Avatar answered Oct 20 '22 13:10

Jakub Narębski


For git, you should never really rename and make file changes in the same commit. you should run a git mv oldName newName to ensure that git knows that it was a simple move of a file, then commit your changes.

like image 28
Ryen Nelsen Avatar answered Oct 20 '22 11:10

Ryen Nelsen