TheFastCat
TheFastCat

Reputation: 3464

In Visual Studio Code How do I merge between two local branches?

In Visual Studio Code it seems that I am only allowed to push, pull and sync. There is documented support for merge conflicts but I can't figure out how to actually merge between two branches. The Git command line within VSC (press F1) only facillitates a subset of commands:

eGit options available in VSCode

Attempting to pull from a an alternate branch or push to an alternate branch yields:

git Command throttling

Here's the documentation on VSCode's Git Visual Studio Code Git Documentation

What am I overlooking?

Upvotes: 157

Views: 301289

Answers (6)

Buzz B
Buzz B

Reputation: 117

Step-by-step guide to Git repository setup, making changes, and merging branches (see Step 4); moving my previous comment (TLDR) into an answer for completeness.

TLDR

  1. Move (checkout) to the branch that you want to bring changes INTO (click the branch name in the bottom-left of the Status Bar (next to the little source control branch icon).
  2. In the Source Control pane, click the 3 dots menu > Branch > Merge...
  3. In the Command Palette menu that appears, select the branch that you want to take changes FROM.

This is detailed in Step 4.

Visual Studio Code (vscode)

Step 1 (Initialise/Clone)

In Visual Studio Code, if a repository (repo) has not already been created you can either Initialise a local repo or Clone from a remote repo:

A. Initialise Local

  1. Go to Source Control pane.
  2. For a new local repo, click Initialise Repository.
    1. To publish to a remote directly instead (which will also initialise the local), select Publish to GitHub:
    2. In the Command Palette dropdown menu that appears, select to publish to private or public remote. Initialise, publish local repo
  3. Enter a commit message.
  4. Stage changes (remember to Save files also):
    1. For individual files: Hover mouse over each file and select +.
    2. For all files: Hover mouse over Changes heading and select +.
  5. Click Commit button.
    1. Repeat steps 3, 4, 5 for subsequent commits. Stage commits
  6. To push to a remote, click Publish Branch (if not already associated with a remote):
    1. In the Command Palette dropdown menu that appears, select to publish to private or public remote. Publish to remote
  7. If associated with a remote, select the dropdown on the Commit button to Commit & Sync to perform this all in one action. Commit and Sync

B. Clone Remote

  1. Copy remote repo URL from internet browser.
  2. In vscode (not in a project folder with a Git repo, i.e. a new session window), go to Source Control pane.
  3. Select Clone Repoository button.
  4. In the Command Palette dropdown menu that appears, paste the remote URL. Clone remote repo

Step 2 (Make Changes)

Make changes and commit according to Step 1, A. 3, 4, 5, 7.

Step 3 (Branch Actions)

  1. In vscode, in the Status Bar at the bottom of the window, next to the Source Control icon there is the name of the current selected branch, click it. Status Bar branch
  2. In the Command Palette dropdown menu that appears, select Create new branch... Command Palette branch actions
  3. To checkout (move) to a branch:
    1. Click the Source Control branch in the Status Bar (Step 3, 1).
    2. Select the branch to checkout to.
    3. Typically, moving is between local branches which are then synchronised with the remote; or moving to a remote branch in which commits are then fetched to the local.

Alternatively, in the the Source Control pane, click the 3 dots (...) menu of the Source Control heading and navigate to the option.

Step 4 (Merging Branches)

  1. Checkout (move) to the branch that you want to bring changes INTO (Step 3, 3). Checkout to branch to bring changes into
  2. On the Source Control pane:
    1. Click the 3 dots (...) menu.
    2. Branch > Merge... Source Control pane to 3 dots menu to Branch to Merge
  3. In the Command Palette dropdown menu that appears, select the branch that you want to take changes FROM. Command Palette select branch to take changes from
  4. To synchronise changes with the remote click the Sync Changes button on the Source Control pane. Sync with remote after merge

For more info check out: VS Code overview

Upvotes: 2

freedeveloper
freedeveloper

Reputation: 4082

Actually you can do with VS Code the following:

Merge Local Branch with VS Code

Upvotes: 71

VonC
VonC

Reputation: 1323573

Update June 2017 (from VSCode 1.14)

The ability to merge local branches has been added through PR 25731 and commit 89cd05f: accessible through the "Git: merge branch" command.
And PR 27405 added handling the diff3-style merge correctly.

Vahid's answer mention 1.17, but that September release actually added nothing regarding merge.
Only the 1.18 October one added Git conflict markers

https://code.visualstudio.com/assets/updates/1_18/merge.png

From 1.18, with the combination of merge command (1.14) and merge markers (1.18), you truly can do local merges between branches.


Original answer 2016:

The Version Control doc does not mention merge commands, only merge status and conflict support.

Even the latest 1.3 June release does not bring anything new to the VCS front.

This is supported by issue 5770 which confirms you cannot use VS Code as a git mergetool, because:

Is this feature being included in the next iteration, by any chance?

Probably not, this is a big endeavour, since a merge UI needs to be implemented.

That leaves the actual merge to be initiated from command line only.

Upvotes: 57

Vahid
Vahid

Reputation: 7551

You can do it without using plugins.

In the latest version of vscode that I'm using (1.17.0) you can simply open the branch that you want (from the bottom left menu) then press ctrl+shift+p and type Git: Merge branch and then choose the other branch that you want to merge from (to the current one)

Upvotes: 207

Shahar Kazaz
Shahar Kazaz

Reputation: 1206

I had the same question, so I created Git Merger.
hope this helps :)

Upvotes: 32

Jay Culpepper
Jay Culpepper

Reputation: 567

I found this extension for VS code called Git Merger. It adds Git: Merge from to the commands.

Upvotes: 8

Related Questions