Here is my situation:
- create a file
2.md
in branchdev
, andgit add
this file, then I can see this file is added into index throughgit ls-files
- switch to branch
master
bygit checkout master
, butls
andgit ls-files
still shows this file exists in working tree and index. Whygit checkout
doesn't update working tree and index according to master's last commit?
Here is the description ofgit checkout
in git manual:
To prepare for working on
<branch>
, switch to it by updating the index and the files in the working tree, and by pointing HEAD at the branch. Local modifications to the files in the working tree are kept, so that they can be committed to the<branch>
.
git commit
this file in branchmaster
, thengit checkout dev
, I find this file disappears from both working tree and index of branchdev
, which also confuse me
Below is my operation and output:
(base) [root@zjmhost hello-github]# git checkout dev
Switched to branch 'dev'
(base) [root@zjmhost hello-github]# touch 2.md
(base) [root@zjmhost hello-github]# ls
2.md copy.md README.md test.md
(base) [root@zjmhost hello-github]# git ls-files
.ipynb_checkpoints/README-checkpoint.md
README.md
copy.md
test.md
(base) [root@zjmhost hello-github]# git add 2.md
(base) [root@zjmhost hello-github]# git ls-files
.ipynb_checkpoints/README-checkpoint.md
2.md
README.md
copy.md
test.md
(base) [root@zjmhost hello-github]# git checkout master
A 2.md
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 12 commits.
(use "git push" to publish your local commits)
(base) [root@zjmhost hello-github]# ls
1.md 2.md copy.md README.md test.md
(base) [root@zjmhost hello-github]# git ls-files
.ipynb_checkpoints/README-checkpoint.md
1.md
2.md
README.md
copy.md
test.md
(base) [root@zjmhost hello-github]# git commit -m "commit 2.md in master branch"
[master 005f0c2] 2.md do the same with 1.md
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 2.md
(base) [root@zjmhost hello-github]# git ls-files
.ipynb_checkpoints/README-checkpoint.md
1.md
2.md
README.md
copy.md
test.md
(base) [root@zjmhost hello-github]# git checkout dev
Switched to branch 'dev'
(base) [root@zjmhost hello-github]# ls
copy.md README.md test.md
(base) [root@zjmhost hello-github]# git ls-files
.ipynb_checkpoints/README-checkpoint.md
README.md
copy.md
test.md