我手动移动了一个文件,然后修改了它。根据Git,它是一个新文件和一个已删除的文件。有什么方法可以强制Git将其视为文件移动吗?


当前回答

If you're using TortoiseGit it's important to note that Git's automatic rename detection happens during commit but the fact that this is going to happen isn't always displayed by the software beforehand. I had moved two files to a different directory and performed some slight edits. I use TortoiseGit as my commit tool and the Changes made list showed the files being deleted and added, not moved. Running git status from the command line showed a similar situation. However after committing the files, they showed up as being renamed in the log. So the answer to your question is, as long as you haven't done anything too drastic, Git should pick up the rename automatically.

编辑:显然,如果你添加新文件,然后从命令行执行git状态,重命名应该在提交前显示。

编辑2:另外,在TortoiseGit中,在提交对话框中添加新文件,但不要提交它们。然后,如果您进入Show Log命令并查看工作目录,您将看到Git是否在提交前检测到重命名。

同样的问题在这里被提出:https://tortoisegit.org/issue/1389,并已被记录为一个bug来修复这里:https://tortoisegit.org/issue/1440。原来这是一个显示问题与TortoiseGit的提交对话框,也存在于git状态,如果你没有添加新文件。

其他回答

If you're using TortoiseGit it's important to note that Git's automatic rename detection happens during commit but the fact that this is going to happen isn't always displayed by the software beforehand. I had moved two files to a different directory and performed some slight edits. I use TortoiseGit as my commit tool and the Changes made list showed the files being deleted and added, not moved. Running git status from the command line showed a similar situation. However after committing the files, they showed up as being renamed in the log. So the answer to your question is, as long as you haven't done anything too drastic, Git should pick up the rename automatically.

编辑:显然,如果你添加新文件,然后从命令行执行git状态,重命名应该在提交前显示。

编辑2:另外,在TortoiseGit中,在提交对话框中添加新文件,但不要提交它们。然后,如果您进入Show Log命令并查看工作目录,您将看到Git是否在提交前检测到重命名。

同样的问题在这里被提出:https://tortoisegit.org/issue/1389,并已被记录为一个bug来修复这里:https://tortoisegit.org/issue/1440。原来这是一个显示问题与TortoiseGit的提交对话框,也存在于git状态,如果你没有添加新文件。

如果你说git状态不显示重命名,试试git commit——dry-run -a

或者你也可以听听Amber的回答! 再引用一下:

首先,取消手动移动文件的阶段性添加:

$ git reset path/to/newfile
$ mv path/to/newfile path/to/oldfile

然后,使用Git移动文件:

$ git mv path/to/oldfile path/to/newfile

当然,如果你已经提交了手动移动,你可能想要重置到移动之前的修订,然后简单地从那里git mv。

在不同的提交中执行移动和修改操作。

对我来说,在提交之前保存所有更改并再次弹出它们是有效的。这使得git重新分析添加/删除的文件,并正确地将它们标记为已移动。