Confusion regarding git checkout

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
# 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 output:

sabya@SABYA-PC e:/test/merge_test (testbranch)
$ git --version
git version 1.7.0.2.msysgit.0
+3
source share
2 answers

The key is "the difference between the current branch and the branch you are switching to "

, , , , , .

git init
vim file
# hack hack hack
git add .
git commit -m "1"
git checkout -b branch
vim file
# hack hack hack
git commit -a -m "2"
git checkout master
vim file
# hack hack hack
git checkout branch

.

+5

, , , .

sabya@SABYA-PC e:/test/merge_test (master)
$ git checkout testbranch
M       src/a.txt
Switched to branch 'testbranch'

, M src/a.txt? M .

, .

+3

Source: https://habr.com/ru/post/1752758/


All Articles