Manfredo Manfredo - 23 days ago 9
Git Question

git double checkout, folder content gone

I have a git repository with a master and developer branch.

Say my branch is called

branch1
.

Inside my repo I have a folder called
myfolder
. I was on
branch1
and I did

git checkout master
git checkout branch1


myfolder
is now empty (sic), how did this happen?

I should note that
myfolder
is included in the
.gitignore
which I suspect is the cause of the problem, but I don't understand why. Also is there a way to restore its content?

Answer

Based on your explanation of the situation and your observations, I deduce the following:

  1. On the master branch, the folder myfolder is part of the repository.
  2. 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.

Comments