为什么我不能结帐另一个 git 分支?

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

why i cannot checkout another git branch?

gitversion-controlgit-branchgit-diffgit-checkout

提问by hugemeow

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/lab_master
  remotes/origin/master

$ git checkout lab_master
error: Your local changes to the following files would be overwritten by checkou                                                                                                                           t:
        **project.properties**
Please, commit your changes or stash them before you can switch branches.
Aborting

why i just failed to checkout lab_master branch?

为什么我只是无法结帐 lab_master 分支?

another question: why i cannot compare current file with the file in another branch?

另一个问题:为什么我不能将当前文件与另一个分支中的文件进行比较?

$ git diff project.properties -b lab_master
fatal: bad flag '-b' used after filename

回答by SQiShER

Git protects you from switching to another branch, because that would override some changes you applied to the file project.properties. You can either throw the changes away by using git checkout -f lab_masteror stash them first via git stash(and git stash popafter you checked out the other branch.) If you are sure, you want to keep the changes, you can also simply commit them.

Git 保护您免于切换到另一个分支,因为这会覆盖您应用到文件的一些更改project.properties。您可以通过使用git checkout -f lab_master或首先通过git stash(并git stash pop在您签出另一个分支之后)隐藏更改来丢弃更改。如果您确定要保留更改,您也可以简单地提交它们。

回答by dutt

You need to either commit your changes or stash them, http://git-scm.com/book/en/Git-Tools-Stashing

您需要提交更改或隐藏它们,http://git-scm.com/book/en/Git-Tools-Stashing

回答by jarodeells

git diff already uses the -b flag to ignore whitespace. That conflicts with your desire to refer to another branch. For that you need to use 'git diff master..anotherbranch'

git diff 已经使用 -b 标志来忽略空格。这与您希望引用另一个分支的愿望相冲突。为此,您需要使用“git diff master..anotherbranch”