Home > Software design >  Git removes file when switching branches
Git removes file when switching branches

Time:05-21

If I checkout a git branch, sometimes a file path/to/File is deleted. I can see this as an unstaged change after the operation. It is always the same text file, which has not been edited for a long time. So it should be the same file at each branch. (But one branch calls it path/to/file.)

Usually I use Atlassian Sourcetree for switching branches, but the problem has also appeared on our gitlab ci/cd runner, which may use simple git command line commands.

The problem emerge at various clones of the repository and with various branches.

Is the repository somehow corrupted? Any idea how to resolve the problem?

CodePudding user response:

This is a case sensitivity issue : git expects a file with a certain letterCase, but your system is fine with having a different Lettercase.

There may be two variants of bugs related to that :

  • git may have only one "version" of filename in its storage,
  • git may have both filename and FileName versioned in its storage.

Check the content of git's commit (git ls-tree HEAD:path/to/directory) to see what variant you have.

  • if it's the first one : "fixing" it just requires choosing the correct name in git's storage, and make sure you have the same correct name on disk,

  • if it's the second one : you will have to additionally compare the content of the two versions (one way is git diff HEAD:path/to/filename HEAD:path/to/FileName), and check which version should be kept.

a note : Windows treats mv filename FileName as a noop, the easiest way to change the case of a name on disk is to go through a temporary name mv filename tempfile && mv tempfile FileName

  • Related