我读到在 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 没有识别出重命名呢?