【Debug-git】error:. local changes to the following files would be overwritten by merge:.idea/misc.xml

本文探讨了在使用IntelliJ IDEA与Git进行项目管理时遇到的常见问题,特别是关于.misc.xml文件的冲突处理,提供了如何正确配置.gitignore以避免此类问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

公司、个人编译环境可能是有某些不为人知的差异,每次用git命令pull的时候,总会遇到一些奇葩问题,比如:

...done.
Resolving deltas: 100% (55/55), completed with 29 local objects.
From https://git.xxxx.xxx/xxxxxxxx/xxxxxx
 * branch            master     -> FETCH_HEAD
   9******..******d  master     -> origin/master
error: Your local changes to the following files would be overwritten by merge:
        .idea/misc.xml
Please commit your changes or stash them before you merge.
Aborting
Updating 9******..******d

前两次因为时间的原因,要么把这个被覆盖的“.idea/misc.xml”本地文件删掉,要么直接生气了删掉本地项目重新clone。

这终究不是一个办法,今天解决一下吧。

首先,这个文件是什么?

【.idea/misc.xml】

知乎上的一个回答是酱紫的:

.idea  #ide 相关
*.apk  #optional

是在系统默认的.gitignore文件基础上额外添加的,如果不加 .idea的话会存在不同开发人员开发机上.idea/下文件不同,导致需要提交的问题。而且经过我和团队小伙伴之间的实践.idea目录下的东西AS都会自动生成,并不需要提交到仓库中

打开看一眼这个文件里都有啥:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="NullableNotNullManager">
    <option name="myDefaultNullable" value="android.support.annotation.Nullable" />
    <option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
    <option name="myNullables">
      <value>
        <list size="5">
          <item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
          <item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
          <item index="2" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
          <item index="3" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
          <item index="4" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
        </list>
      </value>
    </option>
    <option name="myNotNulls">
      <value>
        <list size="4">
          <item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
          <item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
          <item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
          <item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
        </list>
      </value>
    </option>
  </component>
  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8 (9)" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/build/classes" />
  </component>
  <component name="ProjectType">
    <option name="id" value="Android" />
  </component>
</project>

 

【.idea/modules.xml】

这个文件告诉编译器你要有哪几个子项目,不分主次。在Android Studio中打开看一眼:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectModuleManager">
    <modules>
      <!--module指定需要编译lib或者主程序绝对路径、相对路径-->
      <module fileurl="file://$PROJECT_DIR$/ExportJar.iml" filepath="$PROJECT_DIR$/ExportJar.iml" />
      <module fileurl="file://$PROJECT_DIR$/child/child.iml" filepath="$PROJECT_DIR$/child/child.iml" />
    </modules>
  </component>
</project>

 

【根目录.iml】

iml文件是一个工程配置文件。这个文件的作用见如下注释部分:

<?xml version="1.0" encoding="UTF-8"?>
<module type="PLUGIN_MODULE" version="4">
  <component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/resources/META-INF/plugin.xml" /><!--intellij插件配置文件-->
  <component name="NewModuleRootManager" inherit-compiler-output="true"><!--使用默认编译输出目录,编译器会将编译的class文件放到此目录中-->
    <exclude-output />
    <content url="file://$MODULE_DIR$"><!--指定项目根目录-->
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /><!--指定Source源码目录-->
      <sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" /><!--指定资源文件目录-->
    </content>
    <orderEntry type="inheritedJdk" /><!---->
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module" module-name="child" /><!--指定lib模块-->
  </component>
</module>

这几个文件中,【根目录.iml】文件没什么争议,一般需要放进【.gitignore】中让git自动忽略,但【根目录.idea】文件夹下的文件我的git只忽略了两个文件,我的“.gitignore”文件默认忽略的是:

*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild

查了一下,2015年之前早期的回答中,大家配置的忽略文件都比较多,【.idea/misc.xml】【.idea/modules.xml】都在“.gitignore”之列,更有甚者整个【.idae】文件夹都在忽略之内。而近期的回答中,基本与本人的“.gitignore”无异。鉴于上文知乎科学家的建议,本人试一下把这两个文件手动添加到“.gitignore”之列试试。

/.idea/misc.xml
/.idea/modules.xml

先这么着吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值