简体   繁体   English

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

[英]Git - Switching branches (windows) & uncommitted changes

I'm having a hard time understanding some git/DCVS concepts. 我很难理解一些git / DCVS概念。 Here's what happened: 这是发生的事情:

  1. I created a git project, and imported it from an SVN repo 我创建了一个git项目,并从SVN仓库导入它
  2. I made some commits 我做了一些提交
  3. I wanted to experiment something, so I created a branch called constants-update 我想尝试一些东西,所以我创建了一个名为constants-update的分支
  4. I switched to constants-update branch, 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 现在我正在尝试使用git checkout master切换到我的主分支
  7. I got this error: error: You have local changes to 'src/groovy/Constants.groovy'; 我收到此错误: 错误:您对'src / groovy / Constants.groovy'进行了本地更改; cannot switch branches. 不能切换分支。

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. 我对DCVS的理解是,我可以随意切换分支,即使某个分支的文件比其他分支更多或更少,只要我提交我的文件。 I've tried committing with git commit -a and switching to master branch, but I have the same error. 我尝试使用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; 作为旁注,当我提交git时警告我,LF将被CRLF取代,并警告我一些尾随的空格也是如此; after I commit I do a git status and a bunch of files always appear as #modified ... . 在我提交后,我执行git status ,一堆文件总是显示为#modified ...

Is this related to git/ windows , or I do not understand correctly what it is supposed to happen? 这与git / windows有关 ,或者我不正确地理解应该发生什么? I just want to switch to my master branch without losing my changes in the other branch. 我只想切换到我的主分支而不会丢失我在其他分支中的更改

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

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. 但是,听起来git在行结尾时遇到问题,并且它认为所有文件都被修改,即使它们不是。 I don't use git on Windows, but I was going to suggest the "core.autocrlf" option to make the crlf handling work. 我不在Windows上使用git,但我建议使用“core.autocrlf”选项来使crlf处理工作。 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 但是,以下博客条目表明这可能不是一个好主意: http//weierophinney.net/matthew/archives/191-git-svn-Tip-dont-use-core.autocrlf.html

I solved the problem hacking my pre-commit hook (commenting these lines in .git/hooks/pre-commit with a # ): 我解决了破解我的预提交钩子的问题(在.git/hooks/pre-commit使用#注释这些行):

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

Just use the following option in .gitconfig file which resides in your users directory. 只需在.gitconfig文件中使用以下选项,该文件位于users目录中。

[core] autocrlf = true [core] autocrlf = true

And it will solve the issue. 它将解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM