【问题标题】:GIT - Fail to checkout to other branch when nothing to commit - when file is in gitignoreGIT - 当文件在 gitignore 中时,无法提交到其他分支
【发布时间】:2014-04-07 21:38:45
【问题描述】:

试图结帐到另一个分支

我有一个带有

的 .gitignore 文件
.someFile

即使我没有任何更改,尝试结帐到另一个分支也会失败

dev@cool:~/proj/source$ git status
# On branch master
nothing to commit, working directory clean
dev@cool:~/proj/source$ git checkout someBranchName 
error: Your local changes to the following files would be overwritten by checkout:
    .someFile
Please, commit your changes or stash them before you can switch branches.
Aborting
dev@cool:~/proj/source$ 

从 .gitignore 中删除文件并再次添加没有帮助

运行命令:

      git update-index --assume-unchanged .someFile

 git rm --cached .someFile

没有帮助

有什么想法吗?

【问题讨论】:

  • 你确定你有.gitignroe而不是.gitignore吗?

标签: git git-branch gitignore git-checkout


【解决方案1】:

分支之间的切换应该被视为应用了代表它们之间差异的补丁。

在您的情况下,git 尝试应用的区别是“添加新文件 .somefile,但仅在它不存在的情况下。”。

然后就发生冲突了。

如果你想用目标分支的版本覆盖文件,那么你可以在切换之前简单地在工作树中删除它。

如果您想保留被忽略的文件,那么您可以将其重命名以搁置(这不会导致任何冲突,直到其新的临时名称与其他文件名不匹配)。然后做一个开关。之后,只需将“.somefile”替换为您临时重命名的文件。
如果内容不匹配,这将导致工作树更改为文件。

【讨论】:

    【解决方案2】:

    如果您暂时删除 .gitignore 文件会怎样?听起来 Git 正在跟踪该文件,即使您告诉它忽略它。

    你可以试试:

    git rm --cached .someFile
    

    从 git 中删除文件(不从工作区中删除)。然后提交。

    【讨论】:

      猜你喜欢
      • 2015-04-17
      • 1970-01-01
      • 2021-10-08
      • 2015-02-13
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      相关资源
      最近更新 更多