Git checkout automatically merges local modifications
I tried the 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 checking in the test branch, all local changes from master are merged.
Why?
I was expecting git give up checking before test due to local changes. I was expecting git to request commit or stash local changes.
Edit: I used a bash script to execute these commands. I get the 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' git tries not to lose perhaps valuable data. In this case, it does not actually merge branches, since these changes were not fixed. Rather, when you do a git checkout , it tries to save recently made but not fixed changes, so it checks the command you request and adds your uncommitted changes. If you really want to undo uncommitted changes, use git checkout -f or git checkout and then git reset --hard HEAD . Sometimes, if the changes you have not made yet cannot be combined into what you are checking, you will receive an error message and the check will fail.