I tried following commands on the shell
git init
echo "test1" > test1.txt
git add test1.txt
git commit -a -m "test1"
echo "test2" >> test1.txt
git branch test
git checkout test
text.txt now contains:
test1
test2
After checkout to branch test all local modifications from master get merged.
Why?
I expected that git refuses checkout to test because of the local changes. I expected that git asks for a commit or stash the local changes.
Edit:
I used a bash script to execute this commands. I get following output:
r@r:/tmp/test$ ./createrepo
Initialized empty Git repository in /tmp/test/.git/
[master (root-commit) 0407f5b] test1
1 file changed, 1 insertion(+)
create mode 100644 test1.txt
M test1.txt
Switched to branch 'test'
gittries hard to not lose possibly valuable data. In this case, it’s not actually merging branches, as the changes in question have not been committed. Rather, when you do agit checkout, it attempts to preserve newly made but not committed yet modifications, so it checks out the commit you are requesting, and adds your uncommitted changes. If you really want to discard the uncommitted changes, usegit checkout -f, orgit checkoutfollowed bygit reset --hard HEAD. Sometimes, if the changes you have not committed yet can’t be merged into what you’re checking out cleanly, you’ll get an error message and the checkout will fail.