Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git doesn't recognize renamed and modified package file

Tags:

java

git

I had a java file called package/old/myfile.java. I had committed this file through git. I then renamed my package to new so my file was in package/new/myfile.java.

I now want to commit this file rename (and content changes) to git.

When I do git status I get

# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    package/old/myfile.java
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       package/new/myfile.java

I've tried adding the new and rming the old and vice versa, I keep getting

$ git status
# On branch develop
# Changes to be committed: 
#        delete:    package/old/myfile.java
#        new file:  package/new/myfile.java

I can't do mv old new because the old file doesn't exist and so I get bad source error.

Is there anything else I can try?

I've tried some of the multiple answers on SO for similar problem, but they haven't worked.

like image 213
Sotirios Delimanolis Avatar asked Jan 25 '13 17:01

Sotirios Delimanolis


People also ask

How does git detect renamed files?

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 you rename a file in git?

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 do I change a file name in git bash?

Step 1: Open GitHub. Step 2: Open the repository to rename any file in that repository. Step 3: Open the file which we want to rename. Step 4: Click the edit button and rename the file.


2 Answers

The relevant section from the git book explains this.

Unlike many other VCS systems, Git doesn’t explicitly track file movement. If you rename a file in Git, no metadata is stored in Git that tells it you renamed the file. However, Git is pretty smart about figuring that out after the fact — we’ll deal with detecting file movement a bit later.

What this means is that if you move a file and then make significant changes git will not know that it was a move. It will see a file deleted and a new file created because the new file doesn't look like the old one. To get around this people often git mv files, commit the move, and then make changes. In your situation you can do

git reset # Move changes from index to working copy
git checkout package/old/myfile.java # Undo delete
git mv package/old/myfile.java package/new/myfile.java # Move file
like image 153
asm Avatar answered Sep 22 '22 13:09

asm


Move the file back, then commit it, and put the actual move into a separate commit. Git does not record moves (or renames) as such, but can recognize them afterward based on content. If the content changes, it can't detect the move properly. Therefore it's common practice to split moves and changes into two commits.

like image 42
Nevik Rehnel Avatar answered Sep 20 '22 13:09

Nevik Rehnel