-1

Let us say, you are working with Visual Studio Code on a file f1.md in git branch-1 and then you switch to branch-2 that does not contain the file.

You get distracted because someone pinged you to ask you a question or you had to attend that mind-numbing meeting; two hours later you get back to Visual Studio Code.

Because you switched contexts, you forgot you switched branches and now you think you are on branch-1 and edit f1.md without looking at the top or which branch you are on because the "open editor" still shows it.

Let us say you have another interruption and this time you really make changes on a file f2.md in branch-2.

Sometime later, you do a git add -A and commit and push to branch-2. Now branch-2 has file f1.md which should never have been there.

Why does "Open Editors" remember the files being edited when I was on branch-1? This is somewhat dangerous. The "Open Editors" probably should remember the files open for a specific branch and not universally across all branches.

One can argue that we should stay away from git add -A as it adds both tracked and untracked files. But still, it seems reasonable to expect "Open Editors" to remember files specific to a branch and not display files edited in other branches purely from a UX point of view.

starball
  • 20,030
  • 7
  • 43
  • 238
Rao Pathangi
  • 504
  • 3
  • 11
  • Was the file already _tracked_ in `branch-1`? If it was _not_ tracked yet, then git does not care for it and it won't stop you from switching (and the editor can keep it open, of course). In general, I think you should issue `git status` more often. Just in case, if the file is committed, it should _not_ be that hard to move those changes into another branch (and remove them from the wrong branch), as long as it's not stuff that you have already pushed and other people are already using. – eftshift0 May 27 '23 at 10:12

4 Answers4

0

When you switch branches in Git, Visual Studio Code (VSC) retains the open editors (and the branch) because it assumes that you continue working on the files when you switch back to that branch.

"Open Editors" are not aware of the branch context and remember the open files of your last session. You need to check which branch are you on when you return to your computer/editor. In VSC, You can see the branch by checking the Git status in the bottom-left corner. Or by using git branch or git status in the integrated terminal.

Maybe also a question for the VS Code team at https://github.com/microsoft/vscode or https://github.com/microsoft/vscode-docs/issues?

Christian
  • 4,902
  • 4
  • 24
  • 42
0

I'm pretty sure this is a feature and/or by-design.

For one thing, git will not automatically stage unstaged changes before you switch to another branch. It will just keep the unstaged changes as-is in the working tree, or issue a warning and abort the operation.

On top of that, VS Code's edit buffers are a level "higher" than the working tree. Changes in the editor do not get persisted to the filesystem until you actually invoke the "save" action.

Given that, I think it would be more generally disruptive and unexpected if VS Code were to automatically close all open editors upon a branch-switch / commit-checkout. And it would be an even bigger pain if one of the editors had unsaved changes- then the user would have to be prompted for whether or not to save those changes, and there could be multiple files with unsaved changes.

If you want to have what you're looking for, you can:

starball
  • 20,030
  • 7
  • 43
  • 238
0

My 2 cents regarding the way to use git:

I came to appreciate the separate stage and commit phases because it gives a rather natural step to review my changes.

IMHO expecting a git add -A && git commit to guess through all the hoops my mind went through is doomed to bring in bad/unwanted changes.

I would suggest you always take time to review what you are about to commit.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

sorry if I didn't get the real issue with your question. But is it because the Source Control in your VS Code is not refreshing automatically? I found an answer on another page here.

In your User Settings (JSON), you can set git.autorefresh to true by adding to your User Settings(JSON) "git.autorefresh": true,

VS Code will automatically refresh source control after you check out a branch - this includes refreshing all the files opened in the Open Editors to their corresponding version based on the branch you checked out.