在git中处理文件重命名 [英] Handling file renames in git
问题描述
当在git中重命名文件,您应该提交任何更改,执行重命名,然后重命名您的重命名文件。 Git会从内容中识别文件,而不是将其视为新的未跟踪文件,并保留更改历史记录。
然而,今晚我只做了这件事,至 git mv
。
> $ git status
#在分支主机上
#要提交的更改:
#(使用git reset HEAD< file> ...停用)
#
$ modified:index.html
#
将Finder中的样式表从 iphone.css
至 mobile.css
> $ git status
#在分支主机上
#要提交的更改:
#(使用git reset HEAD< file> ...停用)
#
#modified:index.html
#
#已更改但未更新:
#(使用git add / rm< file> ...更新将提交的内容)
#(使用git checkout - < file> ...放弃工作目录中的更改)
#
#deleted:css / iphone.css
#
#未追踪的文件:
#(使用git add< file> ...来包含将要提交的内容)
#
#css / mobile.css
所以git现在认为我已经删除了一个CSS文件,并添加了一个新文件。不是我想要的,让我们撤销重命名并让git完成这项工作。
> $ git reset HEAD。
重置后的未暂存变更:
M css / iphone.css
M index.html
返回我开始的地方。
> $ git status
#在分支主机上
#要提交的更改:
#(使用git reset HEAD< file> ...停用)
#
#modified:index.html
#
让我们使用 git mv
代替。
> $ git mv css / iphone.css css / mobile.css
> $ git status
#在分支主机上
#要提交的更改:
#(使用git reset HEAD< file> ...来暂停)
#
#改名为:css / iphone.css - > css / mobile.css
#
#已更改但未更新:
#(使用git add< file> ...更新将提交的内容)
# (使用git checkout - < file> ...放弃工作目录中的更改)
#
#修改:index.html
#
看起来我们很棒。那么为什么git在第一次使用Finder时就不认识这个重命名?
对于 git mv
手册页面
表示
成功完成后索引更新
[....]
所以,首先你必须更新你自己
的索引(通过使用 git添加mobile.css
)。但是
git status
仍然会显示两个不同的文件
$ git status
#分支主
警告:LF将被index.html中的CRLF替换
#要提交的更改:
#(使用git reset HEAD< file> ...来取消)
#
#modified:index.html
#new file:mobile.css
#
#已更改但未更新:
#(使用git add / rm< file> ...来更新将要提交的内容)
#(使用git结账 - < file> ...放弃工作目录中的更改)
#
#已删除:iphone.css
#
通过运行
git commit --dry-run -a
这会导致你的
期望值
Tanascius @ H181 / d / temp / blo(master)
$ git commit --dry-run -a
#在分支大师
警告:LF将被重新分配由CRLF放置在index.html中
#要提交的更改:
#(使用git reset HEAD< file> ...停用)
#
#修改:index.html
#改名为:iphone.css - > mobile.css
#
我无法确切地告诉您为什么我们会看到这些差异
之间 git status
和
git commit --dry-run -a
,但
这里是
的提示 Linus b
$ b
git甚至不关心整个
重命名检测的内部情况,并且任何提交
的提交都完全独立于
启发式,然后我们用它来显示这些重命名内容。
$ b $ a 干运行
使用真正的重命名机制,而
git status
可能不会。
I'd read that when renaming files in git, you should commit any changes, perform your rename and then stage your renamed file. Git will recognise the file from the contents, rather than seeing it as a new untracked file, and keep the change history.
However, doing just this tonight I ended up reverting to git mv
.
> $ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
Rename my stylesheet in Finder from iphone.css
to 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
So git now thinks I've deleted one CSS file, and added a new one. Not what I want, lets undo the rename and let git do the work.
> $ git reset HEAD .
Unstaged changes after reset:
M css/iphone.css
M index.html
Back to where I began.
> $ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
Lets use git mv
instead.
> $ 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
#
Looks like we're good. So why didn't git recognise the rename the first time around when I used Finder?
For git mv
the
manual page
says
The index is updated after successful completion, [....]
So, at first you have to update the index on your own
(by using git add mobile.css
). However
git status
will still show two different files
$ 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
#
You can get a different output by running
git commit --dry-run -a
which results in what you
expect
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
#
I can't tell you exactly why we see these differences
between git status
and
git commit --dry-run -a
, but
here is a hint from
Linus
git really doesn't even care about the whole "rename detection" internally, and any commits you have done with renames are totally independent of the heuristics we then use to show the renames.
A dry-run
uses the real renaming mechanisms, while a
git status
probably doesn't.
这篇关于在git中处理文件重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!