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

发布于 2024-10-12 10:56:54 字数 138 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故事还在继续 2024-10-19 10:56:54

没有问题。只需 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 even git 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 for mv old new; git rm old; git add new.

一杯敬自由 2024-10-19 10:56:54

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

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

然后,使用 Git 移动文件:

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

当然,如果您已经提交了手动移动,您可能需要重置为移动之前的修订版,然后只需 git mv 从那里。

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 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 mv from there.

最初的梦 2024-10-19 10:56:54

试试这个:

mv new old
git rm new
git mv old new

Try this:

mv new old
git rm new
git mv old new
知足的幸福 2024-10-19 10:56:54

这必须是自动化的。在我提交之前我从来不记得 git,而且我也不想这样做。

#!/usr/bin/env python3

# This `git rm` wrapper respects the `--cached` option (like `git rm`).

import sys
import subprocess

if "--cached" in sys.argv:
    dest = sys.argv[-1]
    rest = sys.argv[1:-1]
    subprocess.check_call(["git", "add", dest])
    subprocess.check_call(["git", "rm"] + rest)
else:
    subprocess.check_call(["git", "mv"] + sys.argv[1:])

我不想“记住 git mv”,因为 git mv 在 git diff 后面添加了隐藏状态,该状态隐式添加到下一次提交中,即使您明确犯了一些完全不相关的事情。我知道,这就是所谓的“上演”状态,但我不喜欢它。我喜欢毫无意外地做出承诺。

This has to be automated. I never remember git until I commit, and I don't want to.

#!/usr/bin/env python3

# This `git rm` wrapper respects the `--cached` option (like `git rm`).

import sys
import subprocess

if "--cached" in sys.argv:
    dest = sys.argv[-1]
    rest = sys.argv[1:-1]
    subprocess.check_call(["git", "add", dest])
    subprocess.check_call(["git", "rm"] + rest)
else:
    subprocess.check_call(["git", "mv"] + sys.argv[1:])

I don't want to "remember git mv", because git mv adds hidden state behind git 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.

圈圈圆圆圈圈 2024-10-19 10:56:54

如果发生此错误,可能意味着您尝试移动的文件最初并未被 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文