Home > Back-end >  Why does my repository show information from the branch that was last open in Visual Studio Code?
Why does my repository show information from the branch that was last open in Visual Studio Code?

Time:01-27

I've only been playing around with Git for a couple of days now as I'm new to it. I wanted to better understand how branches work. Here is what I did:

I created a repository which contains a file I am working on called Python.py. Here is the path to the repository:

C:\Users\Hamid\Documents\Hamid-GitHub

Here is the file opened in Visual Studio Code:

enter image description here

  1. I create a new branch called 'Dev' and made some changes to the Python file, saved it, staged it and committed it. Here is the code:

enter image description here

2.If I then open the file in another Python IDE like Spyder for example it shows the 'dev' branch code:

enter image description here

But if I go back to Visual Studio Code and switch back to the main branch:

enter image description here

And then go to Spyder, I get the initial code associated with the main branch:

enter image description here

I thought that the codes/files in the main branch are the codes that are accessed in the repository and that files from other branches are only visible when activated. So for example if I navigated to C:\Users\Hamid\Documents\Hamid-GitHub and opened the Python.py file I would see the code from the 'main' branch. From what I can see above, the code in the file depends on the branch that was last open in Visual Studio code. How can I ensure the repository only reflects the main branch unless specified?. Also is this normal?.

I tried to deleted the branch and create it again thinking that I did something wrong along the way but I received the same outcome.

CodePudding user response:

A "normal" git repository (a "non-bare" one, but feel free to ignore that phrase for now) is basically two parts:

  • the .git directory which contains the database/storage of all the previous and current commits, branches, tags and related information.
  • a workspace which represents the current working state of all the files, usually based on a given branch/commit (i.e. the latest commit on the branch you checked out) with some optional local changes.

(There's also the "staging area" or "cache" which in some sense exists in both of those at the same time, but that's not relevant for this question).

When you check out a given commit, git will update the workspace to the version of each file in the latest commit of that branch. I.e. if you check out your main branch, then the workspace will contain those files. Similarly when you check out the Dev branch, the changes made in that branch will be visible.

Note that "which branch is checked out" and "what does the workspace" look like are persistent states that can easily be seen in the file system. In other words: if you use different IDEs or git clients or whatnot to inspect the same git repository, then they will all see the same state.

If you switch to the Dev branch in VS Code then Spyder will of course see the new branch. If you use Spyder to switch back to the main branch, then VS Code will also see that change.

  •  Tags:  
  • git
  • Related