2

I have created a file sample1 on master branch and edited it with a line "changes made to file in master". It was still untracked file as I did not add to git yet.

Then I created a branch using

git checkout -b feature_branch

First thing I observed was the file sample1 was visible in the new feature_branch. I think this is ok.

Then I made changes to this file in the feature_branch.

I then went back to master branch using

git checkout master

I opened the file and to my surprise, I see the changes made in feature branch in the master without me committing the changes or merging the files.

And then I made changes to the master again and switched to feature_branch and still found the changes.

Can anyone explain if this is intended behaviour?

The Roy
  • 1,999
  • 1
  • 12
  • 27
  • git only does version control in files that were added to git via `add` and `commit`, otherwise git won't "take responsibility" over that file. – ignacio Dec 11 '19 at 04:49
  • Does this answer your question? [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 Dec 11 '19 at 12:56

1 Answers1

1

It was still untracked file as I did not add to git yet.

As long as the file is untracked (not in the index), you can switch branches (using nowodays the new git switch command rather than the old git checkout), the untracked file will remain there, untouched.

Any modification done on that file will not depend on your current branch.
See "git checkout without deleting untracked files" for more.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283