52

I am trying to workout how branching works in Visual Studio Code and Git. Previously I've worked with TFS which was pretty simple - you create a branch and this is all stored in a separate folder on your disk so you can easily work on one branch or another.

So in Visual Studio Code I have created a new branch "test" from master. In Visual Studio Code I am looking at the branch icon bottom left to see which branch I'm working in.

enter image description here

It says I'm working in test, so I make changes to file1, commit and synchronise the changes. I now want to work on the master branch, so I go bottom left, switch to master, but my changes from the test branch are still displayed in the editor.

If I look on the hard disk, my changes from the test branch are there, and there is no separate test & master folder. So how do I get to work on the master branch again? I've tried checkout, synchronising on the master branch, but I always have the test changes displayed.

A typical scenario is, I'm part way through working on a new feature on a branch, and I need to switch to master to do a hotfix, before returning to the feature branch. Switching between those branches is escaping me.

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
Paul
  • 621
  • 1
  • 5
  • 6

5 Answers5

60

When in the Visual Studio Code IDE, do the following:

  • on the bottom left, press on the Git branch which will give a list: Select a ref to checkout
  • select one of the branches of the remote branch.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bruno Vermeulen
  • 2,970
  • 2
  • 15
  • 29
  • 1
    I had issues with this at first because I forgot I had already created the new branch in github. It wasn't in VSCode yet which is why I forgot. Once I pulled, I realized my issue and then this worked perfectly. – Technichor Apr 18 '20 at 16:12
  • 4
    For anyone who stumbles upon this while learning VS Code or Github: use the left side menu to go to the git Source Control panel, then click on three dot menu at top right of Source Control panel, then click on Fetch in the dropdown that appears. Fetch will update your branch lists in the current project so that the other tips on this SO question page work for you. – Word Nerd Sep 19 '22 at 17:35
26

gif_1

Steps to reproduce :

  1. Access the "Source Control" tab on the left side of VSCode
  2. Click on the "three small dots" next to the refresh button
  3. Click on the "Checkout to..." option
  4. Choose the branch you want to switch to

(Personally it works better if you create a branch in VSCode directly than on the web application on Github or Gitlab)

Enjoy

Julien J
  • 2,826
  • 2
  • 25
  • 28
7

To switch branches in VScode: git checkout branchname. That all that's needed.

Makyen
  • 31,849
  • 12
  • 86
  • 121
vasif mammadov
  • 105
  • 1
  • 3
3

For me, I recommend you to install vscode gitlens extensions(https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens), and go left bottom. then click on the branch name and select your branch.

  1. git branch branchName if you like command line
1

Refresh the remote branch listing

  1. Open the git Source Control panel by clicking the git symbol in the side menu
  2. Expand the menu options by clicking the three dots in the upper right corner of the Source Control menu
  3. Refresh the remote branch listing by selecting Fetch from the drop-down menu

Switch branches

  1. Open the branch selection menu by clicking the current branch name in the bottom left corner
  2. Select the remote branch from the pop-up menu
JDawg
  • 8,560
  • 4
  • 25
  • 29