我曾经读到,在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
#
看起来我们没问题了。那么为什么当我使用Finder重命名时,Git第一次没有识别到这个重命名呢?
add+rm
或者mv
——Git都会生成相同的结果。然后,Git利用其重命名/复制检测功能来让您知道这是一次重命名操作。您引用的源的说法也不准确。无论您是否在同一提交中进行修改和重命名,都没有关系。当您跨越修改和重命名执行diff操作时,重命名检测会将其视为重命名+修改操作,如果修改是完全重新编写,则会显示为添加和删除操作——无论您如何执行都没有关系。 - Cascabelgit mv old new
命令会自动更新索引。当你在 Git 之外重命名文件时,你需要执行git add new
和git rm old
命令来将更改添加到索引中。一旦完成这些步骤后,运行git status
命令就可以按照你的预期工作了。 - Chris Johnsenpublic_html
目录。进行了git add .
和git commit
操作后,在git status
中仍然显示了一堆“deleted”文件。我执行了git commit -a
,删除操作被提交了,但现在我找不到那些位于public_html
中的文件的历史记录了。这个工作流程并不如我所愿的那么顺畅。 - Greg Kgit mv
命令可以立即将重命名添加到暂存区,而手动重命名则需要你自己将重命名添加到暂存区,然后才能在git状态中看到它被标记为"重命名"。回答关于“为什么使用Finder重命名时它没有被检测到”的问题,是因为你还没有将它添加到暂存区。一旦你将删除和未跟踪的文件都添加到暂存区,它就会被检测到。(此外,文件内容变化的百分比超过阈值后,它将不再被视为重命名。) - undefined