7

I'm using VS code + git version 2.20.1 on macOS. Sometimes, when I switch throughout branches, files of other branches are wrongly retained in the current one.

Those files are not present in .gitignore and they are not uncommitted changes.

For example if I have those committed files:

  • branch_a: file1,file2
  • branch_b: file2

if I checkout branch_a in VS Code, I commit all changes and then I switch to branch_b, I still have file1 in my working dir.

Oddly enough, file1 is somehow "invisible" to git. Indeed, this is not considered a potential change to branch_b. This is just ignored even if it is actually on the file system. Let me clarify this from the command line point of view.

$ [git: branch_a] ls
file1   file2
$ [git: branch_a] git status
nothing to commit, working tree clean

$ git checkout branch_b
Switched to branch 'branch_b'
Your branch is up to date with 'origin/branch_b'.

$ [git: branch_b] ls
file1   file2

however, file1 shouldn't be here, but...

$ [git: branch_b] git status
nothing to commit, working tree clean

working tree is considered clean, and even if I rm or I modify the file, git does not detect any changes:

$ [git: branch_b] rm file1
$ [git: branch_b] ls
file2

$ [git: branch_b] git status
nothing to commit, working tree clean

This issue is not systematic but it happens only sometimes. A similar problem has been reported in this SO question and it seems related to a kind of lock managed by VS Code (or by other Code Editors)

I'm wondering if there is a way to avoid this behavior and therefore if I can avoid to clean stale files with git reset --hard + git clean -f -d.

floatingpurr
  • 7,749
  • 9
  • 46
  • 106
  • Are they in the `.gitignore`? – jonrsharpe Jul 10 '19 at 11:25
  • No, they are not. This is an aberrant behavior, I guess. – floatingpurr Jul 10 '19 at 11:26
  • Possible duplicate of [Why git keeps showing my changes when I switch branches (modified,added, deleted files) no matter if I run git add or not?](https://stackoverflow.com/questions/5531362/why-git-keeps-showing-my-changes-when-i-switch-branches-modified-added-deleted) – EncryptedWatermelon Jul 10 '19 at 11:27
  • @EncryptedWatermelon looks like this is a different problem. I'm gonna edit to specify further – floatingpurr Jul 10 '19 at 11:30
  • 1
    `git` has a `check-ignore` command to debug what may cause a file to be ignored. Try running : `git check-ignore --verbose file1` and check if this command returns something. – LeGEC Jul 10 '19 at 11:57
  • I've already cleaned up the repo to continue the job :( I can try this command as soon as it happens again – floatingpurr Jul 10 '19 at 12:00
  • 2
    Also, did you consider if `git update-index` has something for the file? – eftshift0 Jul 10 '19 at 15:17
  • No I didn't. As I mentioned, I cannot replicate this issue systematically to look inside it since it happens randomly (or better: after certain events that I still don't get). I hope that there is a known reason for this problem and a known way to avoid it :) – floatingpurr Jul 10 '19 at 15:20
  • @LeGEC @eftshift0 I tried your solutions but both return anything about extra files (i.e., "`file1`"). I cannot figure out what it is going on... – floatingpurr Jul 11 '19 at 06:50
  • Visual Studio Professional 2019 version 16.10 does the same thing. I don't remember VS doing it before the last update (to 16.9). – Suncat2000 Jun 10 '21 at 11:51
  • Pretty annoying – floatingpurr Jun 10 '21 at 12:24
  • I Have an untracked file and nothing to commit working tree clean, and I have only ever worked on one branch in this repo. Very weird. – Simon Chemnitz-Thomsen Nov 25 '22 at 12:15

1 Answers1

-2

Before shifting to other branches make sure you have committed all files in one branch then files of other branches will not be reflected in current branch.

Vipin
  • 1