我读过当renaming files in Git时,你应该提交任何更改,执行重命名,然后暂存重命名的文件。Git将从内容中识别该文件,而不是将其视为新的未跟踪文件,并保留更改历史。
然而,今晚我就这样做了,我最终恢复到了git mv。
> $ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#我将Finder中的样式表从iphone.css重命名为mobile.css
> $ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: css/iphone.css
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# css/mobile.css所以Git现在认为我已经删除了一个CSS文件,并添加了一个新的。这不是我想要的。让我们撤销重命名,让Git来完成这项工作。
> $ git reset HEAD .
Unstaged changes after reset:
M css/iphone.css
M index.html我回到了我开始的地方:
> $ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#让我们改用git mv:
> $ git mv css/iphone.css css/mobile.css
> $ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: css/iphone.css -> css/mobile.css
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: index.html
#看起来我们很棒。那么为什么Git在我第一次使用Finder的时候没有识别出重命名呢?
发布于 2010-04-15 05:35:40
对于git mv,the manual page说
索引在成功完成后更新,…
因此,首先,您必须自己更新索引(通过使用git add mobile.css)。但是,git status仍然会显示两个不同的文件:
$ git status
# On branch master
warning: LF will be replaced by CRLF in index.html
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
# new file: mobile.css
#
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: iphone.css
#您可以通过运行git commit --dry-run -a获得不同的输出,这将产生您所期望的结果:
Tanascius@H181 /d/temp/blo (master)
$ git commit --dry-run -a
# On branch master
warning: LF will be replaced by CRLF in index.html
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
# renamed: iphone.css -> mobile.css
#我不能确切地告诉您为什么我们看到git status和git commit --dry-run -a之间的这些差异,但这里是a hint from Linus
git甚至不关心整个内部的“重命名检测”,而且你对重命名所做的任何提交都完全独立于我们用来显示重命名的启发式方法。
dry-run使用真正的重命名机制,而git status可能不使用。
发布于 2011-10-21 15:18:07
您必须将这两个修改过的文件添加到索引中,Git才会将其识别为移动。
mv old new和git mv old new之间的唯一区别是git mv还会将文件添加到索引中。
mv old new,那么git add -A也会起作用。
请注意,您不能只使用git add .,因为这不会向索引中添加删除。
请参阅
发布于 2015-07-09 06:36:17
对于Git 1.7.x,以下命令对我有效:
git mv css/iphone.css css/mobile.css
git commit -m 'Rename folder.'不需要git add,因为原始文件(即css/mobile.css)已经存在于先前提交的文件中。
https://stackoverflow.com/questions/2641146
复制相似问题