简体   繁体   English

是否可以在执行常规 mv 后重命名 git 文件?

[英]Is it possible to rename a git file after doing a regular mv?

I've renamed a few files in my git repo, but I forgot to use git mv to rename those files.我在 git 存储库中重命名了一些文件,但我忘记使用git mv重命名这些文件。 I'd like to make my commit reflect the renamed files, instead of showing a deletion and an addition to the repo.我想让我的提交反映重命名的文件,而不是显示对 repo 的删除和添加。 Is it possible to do something like that?有可能做这样的事情吗?

Or would I have to create a kind of macro that uses mv to move the file back to the original name, and then git mv to rename it once again, like so:或者我是否必须创建一种使用mv将文件移回原始名称的宏,然后git mv再次重命名它,如下所示:

function fixmv() { mv $2 $1 && git mv $1 $2 }

If there is a built-in way to do this using git, I'd love to hear.如果有使用 git 的内置方法,我很想听听。

About three years ago, I wrotea script to do thisgit-mv-after , to be invoked as git mv-after a la hg mv --after —but all you really have to do is run git add old-name new-name .大约三年前,我编写了一个脚本来执行此操作- git-mv-after ,被调用为git mv-after a la hg mv --after - 但你真正需要做的就是运行git add old-name new-name

(My script is extra-fancy as it does a bunch of checks to make sure that the rename is safe.) (我的脚本非常漂亮,因为它会进行大量检查以确保重命名是安全的。)

I do think git mv should include a --after flag, so that Git isn't deliberately dumber than Mercurial here.我确实认为git mv应该包含一个--after标志,这样 Git 就不会故意比 Mercurial 笨。 :-) But it is pretty minor. :-) 但它很小。

Yes;是的; the easy way is to run git add new-name and git rm --cached old-name .简单的方法是运行git add new-namegit rm --cached old-name git doesn't track renames specifically; git 不专门跟踪重命名; they're just a deletion from one name and an add of the same content at a new name.它们只是从一个名称中删除并以新名称添加相同内容。
git rm --cached tells it not to complain about the fact that the file you're asking it to remove doesn't exist. git rm --cached告诉它不要抱怨您要求它删除的文件不存在这一事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM