0

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?

Manfredo
  • 1,760
  • 4
  • 25
  • 53
  • Not cleared what you did, you had a folder with content and when switching branches the content gone? – CodeWizard Nov 11 '16 at 18:42
  • Yes exactly, so say at time 0 I'm on branch1. I checkout first to master and then to branch1 so I should return where I was. However in the process the content of the folder is erased. – Manfredo Nov 11 '16 at 18:54

1 Answers1

0

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.

poke
  • 369,085
  • 72
  • 557
  • 602
  • Thanks for the explanation, it definitely makes sense. I will now go to cry against git for the nth time. – Manfredo Nov 11 '16 at 19:21