0

I have been studying from the book ProGit, and stumbled upon this paragraph under the topic "Reset Demystified":

Switching branches or cloning goes through a similar process. When you checkout a branch, it changes HEAD to point to the new branch ref, populates your Index with the snapshot of that commit, then copies the contents of the Index into your Working Directory.

However, as you can see in the below terminal output, I am unable to replicate the behavior.

GaurangTandon@Gaurang MINGW64 /j/test (master)
$ git status
On branch master
nothing to commit, working tree clean

GaurangTandon@Gaurang MINGW64 /j/test (master)
$ touch a.txt

GaurangTandon@Gaurang MINGW64 /j/test (master)
$ git checkout -b "dev"
Switched to a new branch 'dev'

GaurangTandon@Gaurang MINGW64 /j/test (dev)
$ git status
On branch dev
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        a.txt

nothing added to commit but untracked files present (use "git add" to track)

According to me, based on my interpretation of that paragraph: after I checked out to the new branch dev, Git should have populated the Index with the snapshot of the commit master was at (since both dev and master point to the same commit, as I verified on Visualizing Git). That commit should not have had the file a.txt, as it was created after the commit. Also, my working directory should have had the same contents as the Index, i.e., without the file a.txt.

Yet, as you can see in the output above, my working directory happens to have the file a.txt, and it is detected by git status.

I wish to understand where I have made a mistake in interpreting the paragraph.

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
  • Possible duplicate of [Modified files in a git branch are spilling over into another branch](https://stackoverflow.com/questions/246275/modified-files-in-a-git-branch-are-spilling-over-into-another-branch) – phd Nov 08 '18 at 18:28
  • @phd While I agree that the situation depicted in that question is the same, I disagree that these are duplicates. In my question, I am asking for the correctness of the paragraph that I have mentioned earlier from the ProGit book. The other question however is not. – Gaurang Tandon Nov 08 '18 at 18:56

2 Answers2

1

As far as I know, Git checkout will carry over modified files in your working directory when switching branches. The documentation supports this claim:

git checkout <branch>
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>.

As to why Git might have this behavior, this prevents accidentally wiping out changes in the working directory by changing branches.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Please also elaborate on the context of the question i.e. the given paragraph and why my interpretation of it is wrong. Thanks. – Gaurang Tandon Nov 08 '18 at 08:42
  • @GaurangTandon Whence is that paragraph, and how old is it? I think that non modified files will in fact be clobbered with the checked out branch, but not modified files. In the case of a completely clean working directory, the paragraph is correct. – Tim Biegeleisen Nov 08 '18 at 09:46
1

This is the normal behaviour of git checkout: as stated in the documentation :

Local modifications to the files in the working tree are kept, so that they can be committed to the branch.

Romain Warnan
  • 1,022
  • 1
  • 7
  • 13
  • Please also elaborate on the context of the question i.e. the given paragraph and why my interpretation of it is wrong. Thanks. – Gaurang Tandon Nov 08 '18 at 08:42
  • @GaurangTandon "copying the index" does not mean that it will erase untracked files. They are not in the index by definition so they remain as-is. But git would not allow you to switch branch if you had a file "a.txt" inside the index of the branch you are checking out. – n00dl3 Nov 08 '18 at 08:55
  • I would say that you quote a paragraph of [this article](https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified) which is about the `reset` command. You just find a more detailled explanation of how `checkout` works in the checkout section. – Romain Warnan Nov 08 '18 at 08:58
  • @n00dl3 Hi, I understand when you say "they are not in the index by definition". However, please note that the paragraph also says that this operation "copies the contents of the Index into your Working Directory." Hence, my working directory should have also been overwritten. However, that does not seem to be the case. – Gaurang Tandon Nov 08 '18 at 09:06
  • @DogEata Do I take your comment to mean that the given paragraph is wrong? Or does it lacks details? – Gaurang Tandon Nov 08 '18 at 09:06
  • I would say it just lacks details because it is not it's main subject of the article. – Romain Warnan Nov 08 '18 at 09:15