我读过renaming files in git时,你应该提交任何更改,执行重命名,然后重命名你的文件。 Git会从内容中识别文件,而不是将其视为新的未跟踪文件,并保留更改历史记录。在git中处理文件重命名
但是,今晚我只做了这件事,结果我回到了git mv
。
> $ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
从iphone.css
重命名我在Finder中样式表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时第一次认识到重命名?
Git跟踪内容,而不是文件,因此,如何让索引进入适当的状态 - “add + rm”或“mv” - 它会产生相同的结果并不重要。然后Git使用它的重命名/复制检测来让你知道它是一个重命名。你引用的来源也不准确。无论您是否在同一个提交中修改+重命名,都没关系。当您在修改和重命名时进行差异化时,重命名检测会将其视为重命名+修改,或者如果修改是完全重写,则会显示为已添加和删除 - 仍然无关紧要它。 – Cascabel 2010-04-14 21:53:18
如果这是真的,为什么它没有使用Finder重命名检测到它? – 2010-04-14 22:45:04
'git mv old new'自动更新索引。当你在Git之外重命名时,你必须执行'git add new'和'git rm old'来对索引进行更改。一旦你完成了这个'git status'就可以像你期望的那样工作。 – 2010-04-15 01:16:40