在git中处理文件重命名

我读过在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第一次没有识别出重命名?


湖上湖
浏览 649回答 3
3回答

梵蒂冈之花

您必须将两个修改后的文件添加到索引,然后git才能将其识别为移动。mv old new和之间的唯一区别git mv old new是git mv还将文件添加到索引中。mv old new那git add -A本来也可以请注意,您不能只使用它,git add .因为那不会为索引添加删除内容。
打开App,查看更多内容
随时随地看视频慕课网APP