Git:手动重命名文件,Git 困惑
我正在使用 Git 并手动重命名已添加到存储库中的文件。现在,我已将重命名的“新”文件添加到存储库中,但 Git 抱怨“旧”文件已被删除。那么如何让 Git 忘记旧文件呢?更好的是,我如何告诉 Git “新”文件确实是“新”文件,以便我可以保持更改历史记录完整?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有问题。只需
git rm old
甚至git add -A
,它就会意识到这是一个重命名。 Git 将看到删除和添加,其内容与重命名相同。您不需要撤消、取消暂存、使用 git mv 等。 git mv old new 只是 mv old new 的简写; git rm 旧的; git 添加新的。
There is no problem. Simply
git rm old
or evengit add -A
and it will realize that it is a rename. Git will see the delete plus the add with same content as a rename.You don't need to undo, unstage, use
git mv
etc.git mv old new
is only a short hand formv old new; git rm old; git add new
.首先,取消手动移动文件的分阶段添加:
然后,使用 Git 移动文件:
当然,如果您已经提交了手动移动,您可能需要重置为移动之前的修订版,然后只需
git mv
从那里。First, cancel your staged add for the manually moved file:
Then, use Git to move the file:
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 mv
from there.试试这个:
Try this:
这必须是自动化的。在我提交之前我从来不记得 git,而且我也不想这样做。
我不想“记住 git mv”,因为 git mv 在 git diff 后面添加了隐藏状态,该状态隐式添加到下一次提交中,即使您明确犯了一些完全不相关的事情。我知道,这就是所谓的“上演”状态,但我不喜欢它。我喜欢毫无意外地做出承诺。
This has to be automated. I never remember git until I commit, and I don't want to.
I don't want to "remember git mv", because
git mv
adds hidden state behindgit diff
that is implicitly added to the next commit, even if you explicitly commit something totally unrelated. I know, it's the so called "staged" state, but I don't like it. I like to commit without surprises.如果发生此错误,可能意味着您尝试移动的文件最初并未被 git 跟踪,或者您尝试将文件移动到的目录不是 git 跟踪的目录。
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.