I am confused about the behavior of git checkout. The git checkout documentation documentation says:
- merge
When switching branches, if you have local modifications for one or more files that differ from each other the current branch and the branch that you switch, the command refuses to switch branches to save their changes to the context. However, with this option, a three-way merge between the current branch, the contents of the working tree, and the new branch is completed, and you will be on the new branch.
But, I did a little test that does not behave as it says in the bold part. I.e:
- I am creating a git repository
- create a directory and a file with some connection and transfer it to the main branch.
- Create another branch "testbranch"
- change the contents of the file in the main. But he didn’t.
- switched to "testbranch".
- Now modified and uncommitted changes from the main branch come to testbranch!
Shouldn't he have failed if I had local changes and wanted to switch to a branch?
The following is a 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
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 output:
sabya@SABYA-PC e:/test/merge_test (testbranch)
$ git --version
git version 1.7.0.2.msysgit.0
source
share