Git 要求我在 checkout master 上提交或存储更改,即使所有更改都已提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8709001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:37:24  来源:igfitidea点击:

Git asks me to commit or stash changes on checkout master, even though all changes were committed?

git

提问by Berislav Lopac

I have two branches locally, masterand Berislav. The latter is currently active, and I have committed all the changes. When I try to checkout to master, I get the following message:

我在本地有两个分支,master并且Berislav. 后者目前处于活动状态,我已经提交了所有更改。当我尝试结帐到 时master,我收到以下消息:

error: Your local changes to the following files would be overwritten by checkout: [list of files changed in the active branch] Please, commit your changes or stash them before you can switch branches. Aborting

错误:您对以下文件的本地更改将被检出覆盖:[活动分支中更改的文件列表] 请在切换分支之前提交您的更改或隐藏它们。中止

However, everything else I tried -- commit, status, merge-- tells me that there's nothing to commit (working directory clean). What do I need to do to get to my masterbranch?

但是,我尝试过的其他所有内容 -- commit, status, merge-- 告诉我没有什么可提交的(工作目录干净)。我需要做什么才能到达我的master分行?

EDIT: When I try git stash, I'm getting:

编辑:当我尝试时git stash,我得到:

error: feeding unmodified [file path] to diffcore

错误:将未修改的 [文件路径] 提供给 diffcore

for all the files listed in the error above.

对于上面错误中列出的所有文件。

回答by Munawwar

I encountered a similar problem today. git statuswasn't listing the files which checkout was complaining about. I did a:

我今天遇到了类似的问题。git status没有列出结帐抱怨的文件。我做了一个:

git checkout -- path/to/file

And that undoes any changes to the file.

这会撤消对文件的任何更改。

An even easier way to undo all unstaged changes on current working directory [1]:

撤消当前工作目录[1]上所有未暂存更改的更简单方法:

git checkout -- .

[1]- Be warned - you will lose any other unstaged changes you were working on (if any). If you don't know what you are doing, then keep a backup of the files you were working on :)

[1]- 请注意 - 您将丢失您正在处理的任何其他未暂存的更改(如果有)。如果您不知道自己在做什么,请备份您正在处理的文件:)

回答by user697576

As simple as this:

就这么简单:

git stash
git stash pop

The "errors" you see when running git stasharen't anything to be concerned about. It's just git recognizing that the file doesn't have any uncommitted changes.

您在运行时看到的“错误”git stash无需担心。只是 git 认识到该文件没有任何未提交的更改。

回答by Adam Bowen

I had a similar problem on a fresh clone. I just forced the checkout with the --force (-f) flag

我在新克隆上遇到了类似的问题。我只是用 --force (-f) 标志强制结帐

git checkout --force some_branch 

Probably not the best way of resolving the issue on a repo that you are making changes to, but in my case I was sure I hadn't made any changes, and just wanted to switch branch.

可能不是解决您正在更改的存储库上的问题的最佳方法,但就我而言,我确信我没有进行任何更改,只是想切换分支。

回答by John Hammink

Move the offending file to some other location. Then delete the offending file from your tree. Pull again. then diff the changes from your offending file over the file you just pulled. hack, but it works.

将有问题的文件移动到其他位置。然后从树中删除有问题的文件。再拉。然后将您的违规文件中的更改与您刚刚拉取的文件进行比较。黑客,但它的工作原理。

回答by Rudi

It seems that you have untracked files in your working copy, which are tracked on the other branch. Git refuses to checkout the other branch, since your currently untracked local files would be overwritten by the files on the other branch.

您的工作副本中似乎有未跟踪的文件,这些文件在另一个分支上进行了跟踪。Git 拒绝签出另一个分支,因为您当前未跟踪的本地文件将被另一个分支上的文件覆盖。

You can now

您现在可以

  • Add those files to your current branch, if those files are relevant for this branch
  • remove those files if they are not needed
  • 如果这些文件与此分支相关,则将这些文件添加到您的当前分支
  • 如果不需要,请删除这些文件

回答by YKa

Very similar to @JohnHammink answer but here goes.

与@JohnHammink 的答案非常相似,但在这里。

Solution

解决方案

  1. Remove file to another directory.
  2. Commit your changes (it might show you just removed two files).
  3. Add the file again to the directory.
  4. Commit (you should be able to see only one file being committed).
  1. 删除文件到另一个目录。
  2. 提交您的更改(它可能显示您刚刚删除了两个文件)。
  3. 再次将文件添加到目录中。
  4. 提交(您应该只能看到一个正在提交的文件)。

In my case this was because of a file that was renamed with only a change to the case. e.g. fooViewModel to FooViewModel. Git kept thinking that the same file was actually two separate files.

在我的情况下,这是因为一个文件被重命名,只对案例进行了更改。例如 fooViewModel 到 FooViewModel。Git 一直认为同一个文件实际上是两个单独的文件。

I think this has something to do with Unix being case sensitive while windows not really.

我认为这与 Unix 区分大小写有关,而 Windows 则不然。

Hope this helps

希望这可以帮助

回答by jk_

I had the same problem, and this solved my issues (on Linux):

我遇到了同样的问题,这解决了我的问题(在 Linux 上):

git config --global core.autocrlf input

回答by Saif Hakeem

As the message suggests, rerun the command w/ --force option.

如消息所示,重新运行带有 --force 选项的命令。

flutter upgrade --force