简体   繁体   中英

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

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.

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.

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

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

TFS and Git work very differently as version control systems. As far as this question is concerned,

In TFSVC , path-based branches are used. Team members typically set up an additional workspace for each branch they work on.

Whereas in Git, branches are just pointers to the commit. Changing the branch in Git essentially means pointing to a different commit. If there are no structural changes made in commits of the branch you are switching to, then your directory structure remains the same even after switching to that branch.

If there are uncommitted changes on your feature branch and if you switch to master (provided git lets you switch the branches with uncommitted changes), the same uncommitted changes will be visible on your master branch as well. If uncommitted changes have conflicting changes with master, then git will tell you to commit your changes first before switching to master.

However, if you commit those changes in feature branch and then switch to master, those changes will not be visible on master branch as master branch is now pointing to a different commit and it lacks the latest commit made by you on feature branch.

The typical workflow for your scenario is -

  1. Take latest from master
  2. Checkout new branch for new feature
  3. Work on your feature
  4. Stage and commit those changes
  5. Checkout master branch, take latest [git pull]
  6. Do hotfix and commit to master
  7. Checkout feature branch
  8. Merge/Rebase feature branch with master
  9. Continue working on your feature

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

Enjoy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM