Andrey Bushman
Andrey Bushman

Reputation: 12486

Why Git allows switch branches and (!!!) doesn't update the working tree after this switching?

Git for Windows 2.5.2. I expect that if the index has the uncommited changes, then Git will disable the checking to other branches. But I see other result. Script of the sample:

#!/bin/bash
# script.sh
# Run this script in the Git Bash.
git --version
host="./sandbox"
if [ -d $host ]; then
    rm -r $host
    mkdir $host
    echo \"$host\" directory recreated...   
else
    mkdir $host
    echo $host directory \"$host\" created.
fi
cd $host
git init -q
touch 1.txt 2.txt 3.txt
git add "*"
git commit -qm "Initializing"
git branch
ls
git checkout -qb branch#1
rm 3.txt
echo ABCD >> 1.txt
# git commit -aqm "branch#1_commit#1"
git branch
ls
git checkout -q master
git branch
ls
git checkout -qb branch#2
rm 2.txt
echo 1234 >> 1.txt
# git commit -aqm "branch#2_commit#1"
git branch
ls
git checkout -q master
git branch
ls

I have such output:

bushm@DESKTOP-ISD2NUH MINGW64 /d/src
$ /d/src/script.sh
git version 2.5.0.windows.1
./sandbox directory "./sandbox" created.
* master
1.txt  2.txt  3.txt
* branch#1
  master
1.txt  2.txt
  branch#1
* master
1.txt  2.txt
  branch#1
* branch#2
  master
1.txt
  branch#1
  branch#2
* master
1.txt

bushm@DESKTOP-ISD2NUH MINGW64 /d/src
$

I see the swithing done but the working tree is the same (one file instead of three) after the switching (is not updated). I expected that after the switching of the index its contents will be nullified and branch's last commit will be extracted into the working tree. Why working tree was not updated according the content of the last commit of the master branch?

Upvotes: 0

Views: 588

Answers (1)

user743382
user743382

Reputation:

I expect that if the index has the uncommited changes, then Git will disable the checking to other branches.

That expectation is wrong. What you can expect is that if switching to a different branch might cause you to lose data, then Git will prevent you from switching to that different branch (unless you force it). But in your case, switching to a different branch doesn't cause you any harm. You have local changes to some files, but those files are the same in all of your branches, so the local changes can simply be kept.

Upvotes: 5

Related Questions