Git: How to merge feature branch into master using VS Code source control?

13,155

STARTING VS Code 1.48, many of the Git commands are now available as menu options from the Source Control panel, including merging of branches. See the "New Git View submenus" section of the July 2020 (1.48) release notes:

Thanks to the new submenu proposed API, the Git View and More Actions (...) menu has been refactored for better organization of several commands:

enter image description here

To merge branches (ex. merge featureA into master):

  1. Select the destination branch (ex. master)
    • Select ... > Checkout to... enter image description here
    • Select the branch enter image description here
  2. Select the branch to be merged (ex. featureA)
    • Select ... > Branch > Merge Branch... enter image description here
    • Select the branch enter image description here
  3. Push the updated branch (ex. master)
    • Select ... > Pull,Push > Push enter image description here

BEFORE VS Code 1.48, VS Code Source Control has no UI for merging branches. But you can do the merge with commands from the Command Palette.

To merge branches (ex. merge my-feature-branch into master):

  1. Checkout the destination branch (master)

    • Command Palette > Git: Checkout to...

    • Select the branch

      enter image description here

  2. Make sure master is synchronized with the remote

    enter image description here

  3. Merge the feature branch

    • Command Palette > Git: Merge Branch..

    • Select the branch

      enter image description here

  4. Confirm the merge

    • If the merge completed without conflicts, you should now see from the Source Control UI or from the status bar that you now have new commits that need to be pushed to the remote. (The merge operation was only executed in your local copy of the repo).

      enter image description here


While using the VS Code Source Control UI can work, I highly recommend learning how to use Git from the command line, as those can be simpler to use, yet they give you more control over Git operations. Plus, they work even outside VS Code, as long as you have access to a terminal.

As an example, the same branch merging operation can be performed from a terminal.

$ git checkout master
$ git pull
$ git merge my-feature-branch
$ git log
commit 54971a1cc845459742392061e71ef4fcb2444357 (HEAD -> master)
Merge: e8fad11 b1d9050
Author: XXX
Date:   Wed May 13 20:14:15 2020 +0900

    Merge branch 'my-feature-branch'
...

The best place to learn about Git is here: https://git-scm.com/book/en/v2.

For specific to git merge:

Share:
13,155
Deepika vijay
Author by

Deepika vijay

Updated on July 25, 2022

Comments

  • Deepika vijay
    Deepika vijay almost 2 years

    I have created a feature branch in Git using Visual Studio source control.

    Now I want to merge the feature branch into master using Visual Studio Code.

    What is the correct process to do this?

  • cryanbhu
    cryanbhu about 3 years
    merging seems like one thing where the command line is actually simpler.. commits and pushing, I like to do it in VS Code
  • nhe
    nhe about 2 years
    This has now been made easier (I'm not sure as of which version). To merge featureA into main: 1. Switch to main (in Source Control, under Branches, right click main -> Switch To Branch) 2. Right click on featureA, and choose "Merge Branch into Current Branch..." 3. Choose Merge (or Merge and Squash, or whatever option you'd like) 2.