git 更改到主分支时出错:我的本地更改将被结帐覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22424142/
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 17:53:19  来源:igfitidea点击:

Error when changing to master branch: my local changes would be overwritten by checkout

gitgit-checkout

提问by Manolo

This question is similar to this one, but more specific.

这个问题类似于这一个,但更具体。

I have a project with two branches (stagingand beta).

我有一个有两个分支(stagingbeta)的项目。

I develop on staging, and use the masterbranch to fix bugs. So if I'm working on staging and I see an error, I change to masterbranch:

我在 上开发staging,并使用master分支来修复错误。因此,如果我在进行暂存并看到错误,我会更改为master分支:

git checkout master

and do the stuff:

并做以下事情:

git add fileToAdd
git commit -m "bug fixed"

and then I merge with both branches:

然后我与两个分支合并:

git checkout staging
git merge master
git checkout beta
git merge beta

And doesn't matter if there are other files on the working tree.

并且工作树上是否还有其他文件都没有关系。

But now, when I try to change to the masterbranch, I'm getting an error:

但是现在,当我尝试更改到master分支时,出现错误

error: Your local changes to the following files would be overwritten by checkout:
src/Pro/ConvocationBundle/Controller/DefaultController.php
Please, commit your changes or stash them before you can switch branches.
Aborting

I thought that I should remove the file from the staging area:

我想我应该从暂存区删除文件:

git reset HEAD src/Pro/ConvocationBundle/Controller/DefaultController.php

but I'm getting the same error. If I do git statusI get No changes to commit

但我遇到了同样的错误。如果我这样做git status,我得到No changes to commit

采纳答案by keltar

Your error appears when you have modified a file and the branch that you are switching to has changes for this file too (from latest merge point).

当您修改了一个文件并且您要切换到的分支也对该文件进行了更改(从最新的合并点开始)时,会出现您的错误。

Your options, as I see it, are - commit, and then amend this commit with extra changes (you can modify commits in git, as long as they're not pushed); or - use stash:

在我看来,你的选择是 - 提交,然后用额外的更改修改这个提交(你可以在 git 中修改提交,只要它们不是pushed);或 - 使用藏匿处:

git stash save your-file-name
git checkout master
# do whatever you had to do with master
git checkout staging
git stash pop

git stash savewill create stash that contains your changes, but it isn't associated with any commit or even branch. git stash popwill apply latest stash entry to your current branch, restoring saved changes and removing it from stash.

git stash save将创建包含您的更改的存储,但它不与任何提交甚至分支相关联。git stash pop将最新的 stash 条目应用到您当前的分支,恢复保存的更改并将其从 stash 中删除。

回答by KikiYu

I encountered the same problem and solved it by

我遇到了同样的问题并通过

git checkout -f branch

and its specification is rather clear.

其规格相当明确。

-f, --force

When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw awaylocal changes.

When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.

-f, --force

切换分支时,即使索引或工作树与 HEAD 不同,也要继续。这用于丢弃本地更改。

从索引中检出路径时,不要因未合并的条目而失败;相反,未合并的条目将被忽略。

回答by Deepika Patel

You can force checkout your branch, if you do not want to commit your local changes.

如果您不想提交本地更改,您可以强制签出您的分支。

git checkout -f branch_name

回答by BeNiza

I encountered the same problem and solved it by

git checkout -f branch

我遇到了同样的问题并通过

git checkout -f 分支

Well, be careful with the -fswitch. You will lose any uncommitted changes if you use the -fswitch. While there may be some use cases where it is helpful to use -f, in most cases, you may want to stashyour changes and then switchbranches. The stashingprocedure is explained above.

嗯,小心-f开关。如果您使用-f开关,您将丢失任何未提交的更改。虽然可能在某些用例中使用 有帮助-f,但在大多数情况下,您可能希望stash更改然后switch分支。stashing上面解释了该过程。

回答by Vitaly Zdanevich

You can commit in the current branch, checkout to another branch, and finally cherry-pick that commit (in lieu of merge).

您可以在当前分支中提交,签出到另一个分支,最后选择该提交(代替合并)。

回答by Tony Fraser

If you get this when trying to check out a different branch:

如果您在尝试查看其他分支时遇到此问题:

my-mac:myGHProject ~$ git checkout other-branch
error: Your local changes to the following files would be overwritten by checkout:
    src/main/resources/reference.conf

This means you've got some changes you need to commit on the branch you have checked out -- or you need to either wipe or stash them as most of the above points to. 19 out of 20 times I am way more likely just to commit my changes.

这意味着您需要在已签出的分支上提交一些更改——或者您需要像上述大多数内容一样擦除或隐藏它们。20 次中有 19 次我更有可能只是提交我的更改。

my-mac:myGHProject ~$ git branch
  * my-local-branch
  * develop    

my-mac:myGHProject ~$ git status
On branch my-local-branch
   Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git checkout -- <file>..." to discard changes in working directory)
 modified:   src/main/resources/reference.conf

my-mac:myGHProject ~$ git add src/main/resources/reference.conf

my-mac:myGHProject ~$ git commit -m "updates on some config"
  [my-local-branch] updates on some config
  1 file changed, 131 insertions(+), 85 deletions(-)

Now that you've done that, you can check out the other branch and switch back and forth pretty easily.

现在你已经完成了,你可以检查另一个分支并很容易地来回切换。

my-mac:myGHProject ~$ git checkout other-branch

my-mac:myGHProject ~$ git status
  On branch other-branch

my-mac:myGHProject ~$ git checkout my-local-branch
  Switched to branch 'my-local-branch'

Just make sure you are both on the right branch and pushing to the right branch when you run your git push origin ${branch} command. Note: if you have your project hooked directly into Intellij, you can see that you changed your branch in the lower right hand corner of the main window.

当你运行你的 git push origin ${branch} 命令时,只要确保你都在正确的分支上并推送到正确的分支。注意:如果您将项目直接挂接到 Intellij,您可以在主窗口的右下角看到您更改了分支。