Git允许我切换分支而不做任何更改 [英] Git allowing me to switch branches without committing changes

查看:190
本文介绍了Git允许我切换分支而不做任何更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习Git,阅读教程。我在分支seo_title,我有文件mission.html的未提交更改。我做了 git checkout master 期望得到关于更改没有提交的警告,没有更改添加等的警告,但是,它继续前进并用消息切换分支:

  M mission.html 
转换到分支'master'

然后,当我执行 git diff mission.html 时,它显示我的工作目录仍包含我所做的更改而我有另一个分支签出。
我错过了什么?
对于它的价值,我在Windows上使用Git Bash。



编辑:对mission.html的更改没有被添加到staging index。



编辑2:我认为最高投票答案是正确的,但经过进一步调查后,它与我所看到的行为不符。以下是我正在做的更全面的描述:

  top_directory(master)> git分支new_branch_1 
top_directory(master)> git分支new_branch_2
top_directory(master)> git checkout new_branch_1

(打开notepad ++并修改resources.html,保存)

  top_directory(master)> git status 
#在分支上new_branch_1
#没有为commit提交的更改:
#(使用git add< file> ...更新将提交的内容
# (使用git checkout - < file> ...放弃wo

#修改:resources.html

中的更改未添加任何更改以提交使用git add和/或git commit
top_directory(new_branch_1)> git checkout new_branch_2

这是我希望git反对并告诉我存储或提交的地方,因为new_branch_1和new_branch_2具有不同版本的resources.html,但它只是在没有警告的情况下切换到新分支,并带来未提交的更改:

  M resources.html 
切换到分支'new_branch_2'
top_directory(new_branch_2)> git status
#在分支上new_branch_2
#没有为commit提交更改:
#(使用git add< file> ...更新将提交的内容)
#(使用git chec kout - < file> ...放弃工作目录中的更改)

#修改:resources.html

没有更改添加到提交中(使用 git add和/或git commit -a)

是否有模式或设置会使它表现这种方式而不是警告?或者我仍然误解了这种情况?编辑3:我现在明白了。评分最高的答案是正确的,请参阅我对该答案的最新评论。

解决方案

您从上次看到的不同行为试图切换本地更改的分支,现在是由于不同的文件更改。



因此,假设我们有一个名为'readme'的分支,一个文件,让我们说README.md。

现在,您已切换回主人。你在其他文件(不是README.md)上做了一些工作。现在您有本地更改。如果您尝试切换回'自述'分支而不提交更改,它会让您。为什么?因为切换到'readme'分支不会覆盖任何本地更改。



如果您修改了master分支上的README.md文件,然后当你尝试做一个

  git checkout readme 

您会遇到

 错误:您对本地文件的更改将被结帐覆盖:README.md 
请在您切换分支之前提交您的更改或存储它们。

,因为您对README.md所做的更改需要合并。

I am just learning Git, going through a tutorial. I am in branch seo_title and I have uncommitted changes to file mission.html. I did git checkout master expecting to get the warning about Changes not staged for commit, no changes added, etc, however instead it went ahead and switched branches with the message:

M       mission.html
Switched to branch 'master'

Then when I did git diff mission.html it showed me that the working directory still contains the changes I made while I had the other branch checked out. What am I missing? For what it's worth, I am using Git Bash on Windows.

EDIT: the changes to mission.html have not been added to staging index either.

EDIT 2: I thought the top voted answer was correct, but upon further investigation it doesn't match the behavior I am seeing. Here is a fuller description of what I am doing:

top_directory(master) > git branch new_branch_1
top_directory(master) > git branch new_branch_2
top_directory(master) > git checkout new_branch_1

(open notepad++ and modify resources.html, save)

top_directory(master) > git status
# On branch new_branch_1
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed
#   (use "git checkout -- <file>..." to discard changes in wo
#
#       modified:   resources.html
#
no changes added to commit (use "git add" and/or "git commit
top_directory(new_branch_1) > git checkout new_branch_2

This is where I expect git to object and tell me to stash or commit since new_branch_1 and new_branch_2 have different versions of resources.html, but it just switches to the new branch without a warning and it brings the uncommitted changes along:

M       resources.html
Switched to branch 'new_branch_2'
top_directory(new_branch_2) > git status
# On branch new_branch_2
# 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:   resources.html
#
no changes added to commit (use "git add" and/or "git commit -a")

Is there a mode or a setting that would make it behave this way instead of warning? Or am I still misunderstanding the scenario?

EDIT 3: I get it now. The top rated answer was right, see my last comment on that answer.

解决方案

The different behaviour you saw from the last time you tried to switch branches with local changes and now is due to different file changes.

So, let's say we have a branch called 'readme' where you have committed some changes to a file, let's say README.md.

Now, you have switched back to master. You do some work on other files (not README.md). Now you have local changes. If you try to switch back to the 'readme' branch without committing your changes, it will let you. Why? Because switching to the 'readme' branch won't override any of your local changes.

If, however you make a modification to the README.md file on the master branch, then when you try to do a

git checkout readme 

you will encounter

error: Your local changes to the following files would be overwritten by checkout: README.md
Please, commit your changes or stash them before you can switch branches.

because you have changes to README.md that would require a merge.

这篇关于Git允许我切换分支而不做任何更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆