0

I have two branches dev and bugfix

on dev I have created new file TestClass.cs this file is not staged. If I run git status it shows

On branch dev
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        WebApplication1/TestClass.cs

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

Then I run git checkout bugfix Now I can see the newly added class TestClass.cs in this branch as well.

so if I run git status it shows

On branch bugfix
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        WebApplication1/TestClass.cs

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

Can someone explain to me this behavior? why this file is being shared between them? I came to know that I should have staged then stash this file before checking out.

But still I want to understand this behavior. Why it's not kept for dev branch only? sometimes I forget to stage and stash changes before checking out and it creates a huge mess

Jack
  • 107
  • 1
  • 9
  • 1
    The same symptom with modified files: https://stackoverflow.com/a/246298/7976758. In your case it's an untracked file that doesn't belong to Git at all. Not to any branch. – phd Jul 31 '21 at 18:29
  • 1
    Unstaged files means untracked files. i,e git does not know which branch that file belongs to. So unless you explicitly stage it in a particular branch, git will show that file in all branches. It really is irrelevant in which branch you create that file until you explicitly stage that file in a particular branch. – Jdeep Jul 31 '21 at 18:30

1 Answers1

4

Until it is committed into git, (i.e. while the file is untracked), it is not on any branch. So no matter which branch you are on, the file will still be there locally.

David Deutsch
  • 17,443
  • 4
  • 47
  • 54
  • This a surprisingly simple but accurate answer. It made me realise that the file that persisted was a new file, not committed to any branch. Thanks. – Jerrit May 10 '23 at 07:56