0

I was expecting a error while checking out to master branch from branch b1, as b1 have uncommitted changes. But I don't get any error and rather changes made in b1 are carried to master branch. Can you explain why is that, I know we can use git stash to keep uncommitted changes and switch to branch. But I want to know why git allowed to switch to master branch in the below case, I have already read Git - checkout another branch when there are uncommitted changes on the current branch but can't wrap my head around it

echo "Line 1" > a.txt
git add a.txt
git commit -m "Initial Commit"

git checkout -b b1
echo "Line 2" > a.txt

git checkout master
Community
  • 1
  • 1
Mahtab Alam
  • 1,810
  • 3
  • 23
  • 40
  • Because of the reasons outlined in this answer: https://stackoverflow.com/a/22055552/1615903 – 1615903 May 05 '17 at 06:15
  • As the author of the long answer there, all I can say is that I cannot explain it any more simply than "Git lets you do it if Git can do it without wiping out your unsaved work." The precise rules are outlined in [the `git read-tree` documentation](https://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html), under the two-tree merge section, but I think you will find that even worse for understandability. – torek May 05 '17 at 06:50
  • Just to be careful, I think I will be using git stash before switching to other branches while having uncommitted changes. That definitely makes sense and keeps it clean. – Mahtab Alam May 05 '17 at 07:21
  • in this case you are stayng at same commit, and only current branch name recorded in metadata changes. So probably it does not even bother checking state of working copy – max630 May 07 '17 at 17:25
  • Does this answer your question? [Checkout another branch when there are uncommitted changes on the current branch](https://stackoverflow.com/questions/22053757/checkout-another-branch-when-there-are-uncommitted-changes-on-the-current-branch) – Inigo Mar 05 '22 at 03:12

0 Answers0