Git - 切换分支(窗口)和未提交的更改

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

Git - Switching branches (windows) & uncommitted changes

windowsgitdvcs

提问by Miguel Ping

I'm having a hard time understanding some git/DCVS concepts. Here's what happened:

我很难理解一些 git/DCVS 概念。这是发生的事情:

  1. I created a git project, and imported it from an SVN repo
  2. I made some commits
  3. I wanted to experiment something, so I created a branch called constants-update
  4. I switched to constants-updatebranch, moved some files, deleted others and added many more
  5. I committed to this branch
  6. Now I'm trying to switch to my master branch using git checkout master
  7. I got this error: error: You have local changes to 'src/groovy/Constants.groovy'; cannot switch branches.
  1. 我创建了一个 git 项目,并从 SVN 存储库中导入它
  2. 我做了一些提交
  3. 我想尝试一些东西,所以我创建了一个名为constants-update的分支
  4. 我切换到常量更新分支,移动了一些文件,删除了其他文件并添加了更多文件
  5. 我致力于这个分支
  6. 现在我正在尝试使用以下方法切换到我的主分支 git checkout master
  7. 我收到此错误:错误:您对“src/groovy/Constants.groovy”进行了本地更改;不能切换分支。

My understanding of DCVS is that I can switch branches at will, even if some branch has more or less files than the others, as long as I commit my files. I've tried committing with git commit -aand switching to master branch, but I have the same error.

我对 DCVS 的理解是,我可以随意切换分支,即使某个分支的文件比其他分支多或少,只要我提交我的文件。我试过提交git commit -a并切换到 master 分支,但我有同样的错误。

As a side note, when I commit git warns me that LF will be replaced by CRLF and warns me about some trailing whitespaces also; after I commit I do a git statusand a bunch of files always appear as #modified ....

作为旁注,当我提交时 git 警告我 LF 将被 CRLF 替换并警告我一些尾随空格;在我提交之后,我做了git status一个,一堆文件总是显示为 #modified ....

Is this related to git/windows, or I do not understand correctly what it is supposed to happen? I just want to switch to my master branch without losing my changesin the other branch.

这是否与 git/ windows相关,或者我没有正确理解它应该发生什么?我只想切换到我的主分支,而不会丢失其他分支中的更改

采纳答案by Miguel Ping

I solved the problem hacking my pre-commit hook(commenting these lines in .git/hooks/pre-commitwith a #):

我解决了破解我的预提交钩子的问题(.git/hooks/pre-commit用 a注释这些行#):

#       if (/\s$/) {
#       bad_line("trailing whitespace", $_);
#       }

回答by Grant Limberg

Lookup git-stash for changing branches while there are unsaved changes in the current branch.

在当前分支中有未保存的更改时查找 git-stash 以更改分支。

回答by Greg Hewgill

You are correct in your thinking about how this should work.

您关于这应该如何工作的想法是正确的。

However, it sounds like git is having issues with the line endings, and it thinks all your files are modified even when they aren't. I don't use git on Windows, but I was going to suggest the "core.autocrlf" option to make the crlf handling work. However, the following blog entry indicates that this might not be a good idea: http://weierophinney.net/matthew/archives/191-git-svn-Tip-dont-use-core.autocrlf.html

然而,听起来 git 的行尾有问题,它认为你的所有文件都被修改了,即使它们没有被修改。我不在 Windows 上使用 git,但我打算建议使用“core.autocrlf”选项来使 crlf 处理工作。但是,以下博客条目表明这可能不是一个好主意:http: //weierophinney.net/matthew/archives/191-git-svn-Tip-dont-use-core.autocrlf.html

回答by Autodidact

Just use the following option in .gitconfig file which resides in your users directory.

只需在位于用户目录中的 .gitconfig 文件中使用以下选项。

[core] autocrlf = true

[核心] autocrlf = true

And it will solve the issue.

它会解决这个问题。