我读到在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
#

看起来我们没事了。那么,为什么Git在我第一次使用Finder时没有识别出这个重命名呢?


当前回答

对于Xcode用户:如果你在Xcode中重命名你的文件,你会看到徽章图标更改为追加。如果你用Xcode提交,你会创建一个新文件,丢失历史记录。

解决方法很简单,但你必须在使用Xcode提交之前完成:

对文件夹进行Git状态检查。你应该看到分阶段的变化是正确的: 重命名:Project/OldName.h -> Project/NewName.h 更名为:项目/ OldName。m -> Project/NewName.m Do commit -m 'name change' 然后回到Xcode,你会看到徽章从A变成M,它被保存以提交将来使用Xcode的更改。

其他回答

对于Git 1.7。下面的命令对我有用:

git mv css/iphone.css css/mobile.css
git commit -m 'Rename folder.'

不需要添加git,因为原始文件(即css/mobile.css)已经在之前提交的文件中。

Git将从内容中识别文件,而不是将其视为一个新的未跟踪文件

这就是你出错的地方。

只有在添加文件之后,Git才能从内容中识别它。

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

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

mv old new然后git add -A也可以工作。

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

参见“git add -A”和“git add”的区别。

你必须添加css/mobile.css这个新文件和rm css/iphone.css,这样git就知道了。然后它将在git status中显示相同的输出。

你可以在状态输出(文件的新名称)中清楚地看到:

# Untracked files:
#   (use "git add <file>..." to include in what will be committed)

还有(旧名):

# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)

我认为在幕后git mv只不过是一个包装器脚本,它确实做到了:从索引中删除文件,并以不同的名称添加它

在windows 10的git 2.33上测试

重命名文件夹和文件你需要在资源管理器。 Git添加。 Git commit -m "提交信息" git推

Git将检测重命名。你可以通过运行git状态来检查(提交后)