Git:手动重命名文件,Git 困惑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4708655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:52:30  来源:igfitidea点击:

Git: renamed file manually, Git confused

gitversion-control

提问by mmersz

I am using Git and manually renamed a file that I had added to the repository. Now, I have added the "new" file which I renamed to the repository but Git complains that the "old" file has been deleted. So how can I make Git forget about the old file? Better yet, how do I tell Git that the "new" file really is the "new" file so that I can keep the change history intact?

我正在使用 Git 并手动重命名我添加到存储库中的文件。现在,我添加了我重命名到存储库的“新”文件,但 Git 抱怨“旧”文件已被删除。那么如何让 Git 忘记旧文件呢?更好的是,我如何告诉 Git “新”文件确实是“新”文件,以便我可以保持更改历史完整?

回答by Jakob Borg

There is no problem. Simply git rm oldor even git add -Aand it will realize that it is a rename. Git will see the delete plus the add with same content as a rename.

没有问题。简单git rm old或什git add -A至,它会意识到这是一个重命名。Git 将看到删除加上与重命名内容相同的添加。

You don't need to undo, unstage, use git mvetc. git mv old newis only a short hand for mv old new; git rm old; git add new.

您不需要撤消、取消暂存、使用git mv等。git mv old new只是mv old new; git rm old; git add new.

回答by Amber

First, cancel your staged add for the manually moved file:

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

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

Then, use Git to move the file:

然后,使用 Git 移动文件:

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

Of course, if you already committed the manual move, you may want to reset to the revision before the move instead, and then simply git mvfrom there.

当然,如果您已经提交了手动移动,您可能希望重置为移动前的修订版,然后简单地git mv从那里开始。

回答by Chris Hasiński

Try this:

尝试这个:

mv new old
git rm new
git mv old new

回答by Candida Ustine

If this error occurs, it probably means that the file you are trying to move is not originally tracked by git or the directory you are trying to move the file into is not a git tracked directory.

如果发生此错误,则可能意味着您尝试移动的文件最初并未被 git 跟踪,或者您尝试将文件移动到的目录不是 git 跟踪的目录。