问题
Git is allowing me to change branches when I have changes not staged for commit (modified files).
Is there a configuration for this somewhere?
Edit: At first I thought this was a configuration that I needed to set to disallow changing between branches if there are modified unstaged files. But by Emily's comment, it appears that you're prompted if the files differ between branches, and not prompted otherwise.
回答1:
How it decides
A quick experiment shows the following.
Suppose you're on branch dev and you've modified foo.txt. Without committing, you try to check out master. One of two things will happen.
If
foo.txtwas modified inmasterin a commit thatdevdoesn't have, you won't be allowed to switch without committing, becausemasterhas a "new" version of the file that conflicts with the unstaged changes.To "check out"
master, therefore, would require Git to updatefoo.txtto the newer version thatmasterhas, destroying your unstaged changes. To prevent your losing work, it won't change branches.- Otherwise, the modification has been done "since" the version
masterknows about, and you'll be able to change branches. Git doesn't have to update the file becausemasterhas no new information about the file.
For the "whoops" changes
Because of the above, if you have unstaged changes in files on one branch and realize you actually want to commit the changes on another, you may or may not be able to check out the other branch.
You can, however, do the following:
git stash save "here's a summary of my changes"(summary will show up ingit stash list)git checkout otherbranchgit stash pop(which is a combination ofgit stash applyandgit stash drop)
回答2:
As Emily pointed out, this is a feature. Git will only disallow the branch change if performing the change would require modifying any of the files that have unstaged changes. If all of the modified files will be untouched by the branch checkout, git will not complain at all. This means that regardless of what branch you check out, you can always check out the previous branch and your working tree will be identical to how you left it.
来源:https://stackoverflow.com/questions/8526279/git-allows-for-branch-change-with-unstaged-changes