在git中处理文件重命名

我读过在git中重命名文件时 ,应提交全部更改,执行重命名,而后暂存重命名的文件。 Git将从内容中识别文件,而不是将其视为新的未跟踪文件,并保留更改历史记录。 css

可是,今晚仅此一次,我最终恢复为git mvhtml

> $ 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

> $ 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完成工做。 xcode

> $ 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 mviphone

> $ 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第一次没有识别出重命名? spa


#1楼

若是确实须要手动重命名文件,例如。 使用脚本批量重命名一堆文件,而后使用git add -A . 为我工做。 code


#2楼

对于Xcode用户:若是在Xcode中重命名文件,则会看到徽章图标更改成追加。 若是使用XCode进行提交,则实际上将建立一个新文件并丢失历史记录。 htm

解决方法很简单,可是您必须在使用Xcode进行提交以前执行此操做: 索引

  1. 在您的文件夹上执行git Status。 您应该看到分阶段的更改是正确的:

重命名:Project / OldName.h-> Project / NewName.h重命名:Project / OldName.m-> Project / NewName.m

  1. 提交-m'名称更改'

而后返回到XCode,您将看到徽章从A更改成M,并保存以当即使用xcode进行进一步更改。


#3楼

最好的办法是本身尝试一下。

mkdir test
cd test
git init
touch aaa.txt
git add .
git commit -a -m "New file"
mv aaa.txt bbb.txt
git add .
git status
git commit --dry-run -a

如今git status和git commit --dry-run -a显示两个不一样的结果,其中git status显示bbb.txt做为新文件/ aaa.txt被删除,而--dry-run命令显示实际的重命名。

~/test$ git status

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   bbb.txt
#
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    aaa.txt
#


/test$ git commit --dry-run -a

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   renamed:    aaa.txt -> bbb.txt
#

如今,继续办理登机手续。

git commit -a -m "Rename"

如今您能够看到该文件实际上已被重命名,而且git status中显示的内容是错误的。

故事的寓意:若是不肯定文件是否已重命名,请发出“ git commit --dry-run -a”。 若是它显示文件已重命名,那就很好了。


#4楼

步骤1:将文件从旧文件重命名为新文件

git mv #oldfile #newfile

步骤2:git commit并添加注释

git commit -m "rename oldfile to newfile"

第三步:将此更改推送到远程服务器

git push origin #localbranch:#remotebranch

#5楼

您必须git add css/mobile.css新文件和git 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在幕后只不过是一个包装脚本,它确实作到了这一点:从索引中删除文件并以不一样的名称添加它

相关文章
相关标签/搜索