1

I have two branches, let's call them project/branch-A and project/branch-B. In branch A I have a directory, let's say parent/dirI/dirII/dirIII .

In branch B I have parent/dirI/dirIII where I have moved dirIII up one level and remove dirII. Now when I am on branch A and go

git checkout branch-B

I still can see dirII and dirIII with all the correct contents in dirIII. This does not change, again when I am currently on branch-B and go

git checkout branch-A

I get the same stuff again: both dirII and dirIII presents in the lower level of dirI.

What is happening?
Have I done something wrong? Or is this how git supposed to work?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Git does not track changes in dirs, only files. Keep at least `.gitkeep` (common name) in every empty directory to have the existence of that directory kept in git history. – Tomasz Kowalczyk Dec 28 '15 at 20:26
  • Is there any way that can be done? consider I am proposing some pull request to restructure the directories of the project. Also `.gitkeep` doesn't seem to work for me... –  Dec 28 '15 at 23:04
  • The branch-names are **project/branch-A** and **project/branch-B** or it is **branch-A** and **branch-B**? **The prefix matters**, so maybe you need to perform **git checkout project/branch-B** – yorammi Dec 29 '15 at 14:25

1 Answers1

0

When you checkout branch-A again, dirI/dirIII becomes a "private" untracked folder (from branch-A perspective): a checkout by default does not remove untracked content.

git checkout restores the working tree and index from branch-A HEAD commit, and in branch-A index, dirI/dirIII is not present: its content (files) is considered untracked and left as-is.
As explained in "Git checkout has deleted untracked files unintentionally", an untracked file is one not present in the index.

With Git 2.23 (August 2019), you would use git switch instead

git switch -f branch-A

with -f or --force an alias for --discard-changes.

--discard-changes

Proceed even if the index or the working tree differs from HEAD.
Both the index and working tree are restored to match the switching target.
This is used to throw away local changes.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250