Git lets you change a branch with unspecified changes

Git allows me to change branches when I have changes that are not set for commit (modified files).

Is there any configuration for this somewhere?

Edit: at first I thought it was a configuration in which I needed to prevent the change between branches if there were modified uninstalled files. But, according to Emily, it seems that you are prompted whether the files differ between branches, and are not requested otherwise.

+55
git version-control
Dec 15 2018-11-12T00:
source share
2 answers

How does he decide

A quick experiment shows the following.

Suppose you are on the dev branch and you have changed foo.txt . Without commit you are trying to check master . There will be one of two things.

  • If foo.txt been changed in master to a commit that dev does not have, you are not allowed to switch without committing, because master has a "new" version of the file that conflicts with unspecified changes.

    To “check” master , so Git would need to upgrade foo.txt to a newer version that master has, destroying your unspecified changes. To prevent your losing work, it will not change branches.

  • Otherwise, the modification has been made “since the master version knows about it, and you can change the branches. Git does not need to update the file because master does not have new information about the file.

For changes "exclamations"

Due to the foregoing, if you have unidentified changes to files on one branch and you really want to commit the changes to another, you may or may not be able to check another branch.

You can, however, do the following:

  • git stash save "here a summary of my changes" (a summary will appear on the git stash list )
  • git checkout otherbranch
  • git stash pop (which is a combination of git stash apply and git stash drop )
+123
Dec 15 2018-11-21T00:
source share

As Emily noted, this is a feature. Git will reject a branch change only if you need to modify any of the files that have unstated changes to make the change. If all modified files are not affected by branch validation, Git will not complain at all. This means that no matter which branch you check, you can always check the previous branch, and your working tree will be identical to how you left it.

+10
Dec 15 '11 at 20:54
source share



All Articles