Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you switch between branches in Visual Studio Code with Git?

Tags:

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.

Visual Studio Code Branching Icon

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.

like image 959
Paul Avatar asked Jun 03 '19 14:06

Paul


People also ask

How do I switch between branches in git Visual Studio?

You can choose an existing local or remote branch as the base. The Checkout branch checkbox automatically switches you to the newly created branch. The equivalent command for this action is git checkout -b <new-branch><existing-branch> .

How do I git branch in Visual Studio?

Create a Git branch in Visual Studio To start, make sure you've got a previously created or cloned repo open. From the Git menu, select New Branch. In the Create a new branch dialog box, enter a branch name. For branch naming details, see Special characters in branch and tag names.

How can I see all branches in VS code?

Branches and Tags# You can create and checkout branches directly within VS code through the Git: Create Branch and Git: Checkout to commands in the Command Palette (Ctrl+Shift+P). If you run Git: Checkout to, you will see a dropdown list containing all of the branches or tags in the current repository.

Which command is used to switch between branches in a repository?

Assuming the repo you're working in contains pre-existing branches, you can switch between these branches using git checkout .


2 Answers

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.
like image 54
Bruno Vermeulen Avatar answered Sep 29 '22 05:09

Bruno Vermeulen


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

like image 41
Julien Jm Avatar answered Sep 29 '22 05:09

Julien Jm