I am confused about a behavior of git checkout. The documentation of git checkout says:
--merge
When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.
But, I have done a small test which is not behaving as said in the bold part. That is:
- I create a git repo
- create a directory and a file with some conent and commit it in master branch.
- Create another branch "testbranch"
- change the content of the file in master. But did not commit.
- switched to "testbranch".
- Now the changed and uncommitted changes from master branch come to testbranch!
Wasn't it supposed to fail, if I have some local changes and wanted to switch to a branch?
Below is the list of commands to reproduce this behavior:
sabya@SABYA-PC e:/test/merge_test
$ git init
Initialized empty Git repository in E:/test/merge_test/.git/
sabya@SABYA-PC e:/test/merge_test (master)
$ mkdir src
sabya@SABYA-PC e:/test/merge_test (master)
$ echo "Hello World" > src/a.txt
sabya@SABYA-PC e:/test/merge_test (master)
$ cat src/a.txt
Hello World
sabya@SABYA-PC e:/test/merge_test (master)
$ git add src
sabya@SABYA-PC e:/test/merge_test (master)
$ git commit -m "say hello"
[master (root-commit) 939f6e0] say hello
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 src/a.txt
sabya@SABYA-PC e:/test/merge_test (master)
$ git branch testbranch
sabya@SABYA-PC e:/test/merge_test (master)
$ echo "Changed content" > src/a.txt
sabya@SABYA-PC e:/test/merge_test (master)
$ git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: src/a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
sabya@SABYA-PC e:/test/merge_test (master)
$ git checkout testbranch
M src/a.txt
Switched to branch 'testbranch'
sabya@SABYA-PC e:/test/merge_test (testbranch)
$ cat src/a.txt
Changed content
Can anyone explain this?
Below is my git version output:
sabya@SABYA-PC e:/test/merge_test (testbranch)
$ git --version
git version 1.7.0.2.msysgit.0