Manfredo Manfredo - 1 year ago 72
Git Question

git double checkout, folder content gone

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

Say my branch is called


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

git checkout master
git checkout branch1

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

I should note that
is included in the
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download