Consider the following “story”:
$ mkdir my_project
$ cd my_project
$ git init
Initialized empty Git repository in /home/misha/misha/my_project/.git/
$ echo "first line" > hello.txt
$ git add hello.txt
$ git commit -m "first commit"
[master (root-commit) 9c913a1] first commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 hello.txt
$ git branch new_feature
$ git checkout new_feature
Switched to branch 'new_feature'
$ echo "second line" >> hello.txt
$ cat hello.txt
first line
second line
$ git checkout master
M hello.txt
Switched to branch 'master'
$ cat hello.txt
first line
second line
Why hello.txt has two lines on branch master? (I thought that git checkout will revert the working directory to the previous state, i.e. hello.txt will have only one line.)
What actually happens behind the scenes to the working directory on git checkout? How it is updated?
Your git checkout to master prevents you from losing uncommitted changes. Thats why you still have your second line in your
hello.txtfile. If you really want to lose your uncommitted changes you have to use the-fparameter.Finally your checkout would look like this: