首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Git中处理文件重命名

在Git中处理文件重命名
EN

Stack Overflow用户
提问于 2010-04-15 05:23:10
回答 14查看 411.1K关注 0票数 471

我读过当renaming files in Git时,你应该提交任何更改,执行重命名,然后暂存重命名的文件。Git将从内容中识别该文件,而不是将其视为新的未跟踪文件,并保留更改历史。

然而,今晚我就这样做了,我最终恢复到了git mv

代码语言:javascript
复制
> $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   index.html
#

我将Finder中的样式表从iphone.css重命名为mobile.css

代码语言:javascript
复制
> $ 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来完成这项工作。

代码语言:javascript
复制
> $ git reset HEAD .
Unstaged changes after reset:
M    css/iphone.css
M    index.html

我回到了我开始的地方:

代码语言:javascript
复制
> $ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   index.html
#

让我们改用git mv

代码语言:javascript
复制
> $ 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的时候没有识别出重命名呢?

EN

回答 14

Stack Overflow用户

回答已采纳

发布于 2010-04-15 05:35:40

对于git mvthe manual page

索引在成功完成后更新,…

因此,首先,您必须自己更新索引(通过使用git add mobile.css)。但是,git status仍然会显示两个不同的文件:

代码语言:javascript
复制
$ 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获得不同的输出,这将产生您所期望的结果:

代码语言:javascript
复制
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 statusgit commit --dry-run -a之间的这些差异,但这里是a hint from Linus

git甚至不关心整个内部的“重命名检测”,而且你对重命名所做的任何提交都完全独立于我们用来显示重命名的启发式方法。

dry-run使用真正的重命名机制,而git status可能不使用。

票数 368
EN

Stack Overflow用户

发布于 2011-10-21 15:18:07

您必须将这两个修改过的文件添加到索引中,Git才会将其识别为移动。

mv old newgit mv old new之间的唯一区别是git mv还会将文件添加到索引中。

mv old new,那么git add -A也会起作用。

请注意,您不能只使用git add .,因为这不会向索引中添加删除。

请参阅

票数 87
EN

Stack Overflow用户

发布于 2015-07-09 06:36:17

对于Git 1.7.x,以下命令对我有效:

代码语言:javascript
复制
git mv css/iphone.css css/mobile.css
git commit -m 'Rename folder.'

不需要git add,因为原始文件(即css/mobile.css)已经存在于先前提交的文件中。

票数 29
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2641146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档