Based on your explanation of the situation and your observations, I deduce the following:
- On the
master
branch, the folder myfolder
is part of the repository.
- On the
branch1
branch, the folder is not part of the repository, and it is being excluded from Git using a .gitignore
rule.
So you start on branch1
, the folder exists but is ignored by Git, so Git does not know about its contents. Now, you switch to the master
branch; that branch does track the contents of that folder. When switching to it, Git compares the contents of the (untracked) local files with the ones from the master
branch. Because it does not run into any differences, there is no conflict when checking out the branch, so it works fine.
You end up with the master
branch, and the tracked myfolder
folder. Now, you switch back to branch1
: The myfolder
folder is part of the master
branch you are switching away from, but it does not exist on the branch1
branch. So Git will remove it. That’s how you end up on branch1
without that folder.
Unfortunately, Git does appear to overwrite the contents without doing the check it usually does when it detects the possibility of losing uncommitted content. This is likely due to the ignore rule. So I’m afraid, it won’t be possible to restore the exact contents from your uncommitted state before. You only have whatever is on master
.