Git不能识别重命名和修改的包文件 [英] Git doesn't recognize renamed and modified package file

查看:489
本文介绍了Git不能识别重命名和修改的包文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 package / old / myfile.java 的java文件。我通过git提交了这个文件。然后,我将我的软件包重命名为 new ,因此我的文件位于 package / new / myfile.java 中。

我现在想把这个文件重命名(和内容更改)提交给git。



当我做 git status 我得到

 #未针对提交进行更改:
#(使用git add / rm< file> ...更新将提交的内容)
#(使用git checkout - < file> ...放弃工作目录中的更改)

#deleted:package / old / myfile.java

#Untracked文件:
#(使用git add< file> ...包含在将要提交的内容中)

#package / new / myfile.java

我试过添加新的和 rm 旧的,反之亦然,我不断收到

  $ git status 
#分支开发
#要提交的更改:
#delete:package / old / myfile.java
#new file:package / new / myfile.java
code>

我不能做 mv old new ,因为旧文件doesn' t存在,所以我得到坏源错误。



是还有什么我可以尝试?

我已经尝试了类似问题上的多个答案中的一些,但他们没有奏效。

解决方案

git book 中的#Repository#Moving-Files>相关部分解释了这一点。


与许多其他VCS系统不同,Git没有明确跟踪文件移动。如果你在Git中重命名一个文件,那么没有元数据存储在Git中,它告诉它你重命名了这个文件。然而,在事实之后,Git非常聪明 - 我们将在稍后检测文件移动。


什么这意味着如果你移动一个文件,然后做出重大改变,git将不知道这是一个举动。它会看到一个文件被删除,并创建一个新文件,因为新文件看起来不像旧文件。为了解决这个问题,经常 git mv 文件,提交移动,然后进行更改。在你的情况下,你可以做

$ g $ reset git reset#把索引的变化移动到工作拷贝
git checkout package / old / myfile.java#撤消删除
git mv package / old / myfile.java package / new / myfile.java#移动文件


I had a java file called package/old/myfile.java. I had committed this file through git. I then renamed my package to new so my file was in package/new/myfile.java.

I now want to commit this file rename (and content changes) to git.

When I do git status I get

# 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:    package/old/myfile.java
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       package/new/myfile.java

I've tried adding the new and rming the old and vice versa, I keep getting

$ git status
# On branch develop
# Changes to be committed: 
#        delete:    package/old/myfile.java
#        new file:  package/new/myfile.java

I can't do mv old new because the old file doesn't exist and so I get bad source error.

Is there anything else I can try?

I've tried some of the multiple answers on SO for similar problem, but they haven't worked.

解决方案

The relevant section from the git book explains this.

Unlike many other VCS systems, Git doesn’t explicitly track file movement. If you rename a file in Git, no metadata is stored in Git that tells it you renamed the file. However, Git is pretty smart about figuring that out after the fact — we’ll deal with detecting file movement a bit later.

What this means is that if you move a file and then make significant changes git will not know that it was a move. It will see a file deleted and a new file created because the new file doesn't look like the old one. To get around this people often git mv files, commit the move, and then make changes. In your situation you can do

git reset # Move changes from index to working copy
git checkout package/old/myfile.java # Undo delete
git mv package/old/myfile.java package/new/myfile.java # Move file

这篇关于Git不能识别重命名和修改的包文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆