【问题标题】:File renamed when git rm and git add done on different filesgit rm 和 git add 在不同文件上完成时重命名文件
【发布时间】:2014-11-21 16:52:30
【问题描述】:

我刚刚做了git rm file1.c 之后git status 显示

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

到目前为止一切顺利。但是如果我git add dir/file2.c 我得到了

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

这是一个错误还是我做错了?我希望新文件被列为新文件。除了简短的版权声明 [\edit] 之外,file1.c 和 file2.c 没有任何相似的内容 [edit]。

【问题讨论】:

  • 文件短吗?如果是这样,Git 可能会错误地检测到重命名。不过不用担心……历史上没有记载的。 Git 会在每次需要重命名时尝试检测它们。如果你想避免它,删除文件,提交,添加文件,进行第二次提交。
  • 是的,它们很短。也许再加上他们实际上有一个小的版权声明这一事实让 git 认为这是一个重命名。
  • 检查this和this

标签: git


【解决方案1】:

这不是错误,这是一项功能。

当你删除一个文件,然后添加另一个大部分代码相同的文件时,这是一个相当于重命名文件的操作。

因此,git 将其显示为

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

查看this link on git-scm 了解更多详情。另外,您可以查看git fails to detect renaming 和How does git detect similar files, for its rename detection。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 2021-05-28
    • 2013-07-17
    • 2012-09-06
    • 2011-04-24
    • 2022-01-07
    • 2015-08-15
    相关资源
    最近更新 更多