2

I just did git rm file1.c after which git status shows

On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    file1.c

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    dir/file2.c
    dir/file3.c

All well and good so far. But then if I git add dir/file2.c I get

On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    renamed:    file1.c -> dir/file2.c

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    dir/file3.c

Is this a bug or am I doing it wrong? I would expect the new file to be listed as new file. file1.c and file2.c does in no way have similar content [edit] besides for a short copyright notice [\edit].

evading
  • 3,032
  • 6
  • 37
  • 57
  • 2
    Are the files short? If so, Git might be detecting the rename incorrectly. But don't worry … it's not recorded in the history. Git tries to detect renames every time it needs them. If you want to avoid it, remove the file, commit, add the file, make second commit. – knittl Nov 19 '14 at 14:14
  • Yes, they are short. Maybe that together with the fact that they actually have a small copyright notice makes git think it's a rename. – evading Nov 19 '14 at 14:40
  • Check [this](http://stackoverflow.com/q/13805750/1860929) and [this](http://stackoverflow.com/q/7938582/1860929) – Anshul Goyal Nov 21 '14 at 08:53

1 Answers1

7

This is not a bug, this is a feature.

When you remove a file, and then add another file where most of the code is same, it is an operation which is equivalent to renaming the file.

Hence, git displays it as

renamed:    file1.c -> dir/file2.c

Check this link on git-scm for more details. Also, you can check git fails to detect renaming and How does git detect similar files, for its rename detection.

Community
  • 1
  • 1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186