Read Anthropic’s case study about Graphite Reviewer

How to merge pull requests in VS Code

Sara Verdi
Sara Verdi
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Merging pull requests (PRs) is a key step in the development process to make sure that changes made in feature branches are integrated into the main codebase. Visual Studio Code (VS Code) provides a robust Git integration that makes it easier for developers to manage and merge pull requests directly from the editor. This guide explains how to merge pull requests in VS Code with examples to walk you through the process.

Before merging pull requests, make sure that Git is properly integrated with your VS Code setup.

  1. Install Git: If you don’t already have Git installed, download it from the official Git website and follow the installation instructions.

  2. Configure Git in VS Code: After installing Git, configure it in VS Code by following these steps:

    • Open VS Code.
    • Press Ctrl + Shift + P (or Cmd + Shift + P on macOS) to bring up the Command Palette.
    • Type "Git: Clone" and choose the option to clone a repository, or open an existing Git project.
    • Make sure you are signed into GitHub (or the platform hosting your pull request).
  3. Verify Git integration: Check the source control tab in VS Code to verify that the Git integration is working. This tab displays changes, staged files, and branches.

To merge a pull request in VS Code, you’ll likely start by creating one first:

  1. Open the GitHub or GitLab repository: Navigate to the repository for your project by cloning it or opening the folder containing the repository.

  2. Make changes to the code: Modify your code in a new branch. You can create a branch directly from VS Code by:

    • Clicking the branch icon in the bottom-left corner.
    • Selecting Create New Branch.

    Create new branch in VS Code

  3. Stage and commit your changes: Stage the modified files using the Source Control tab and commit them with a message.

    Source control tab icon

  4. Push the branch to origin: Once committed, push your branch to the remote repository using:

    • The push icon in the Source Control tab.
    • Or run git push origin <branch-name> in the integrated terminal.
  5. Create a pull request: VS Code has built-in support for GitHub pull requests. You can:

    • Click the GitHub pull request icon (if you have the GitHub Pull Requests and Issues extension installed).
    • Or, visit your repository in the browser and manually create a pull request on GitHub or GitLab.

Once a pull request is reviewed and approved, you can easily merge it using VS Code. Here’s a step-by-step guide:

For working with pull requests in GitHub directly from VS Code, you need to install the GitHub Pull Requests and Issues extension:

  • Navigate to the Extensions tab (click on the square icon on the left sidebar).
  • Search for "GitHub Pull Requests and Issues".
  • Install the extension.

This extension will allow you to review and merge pull requests in VS Code without leaving the editor.

Before you start the merge process, it’s important to ensure that your local copy of the repository is up to date:

  • Open the Command Palette with Ctrl + Shift + P (or Cmd + Shift + P on macOS).
  • Type Git: Fetch and select it. This will retrieve the latest changes from the remote repository.

To view and manage pull requests:

  • Click the GitHub icon in the activity bar (left sidebar).
  • Select the Pull Requests view to see all the open PRs for your repository.

If you have a specific PR you want to merge, click on it to see more details. You can view the status of checks, approvals, and any merge conflicts.

It’s critical to ensure that there are no merge conflicts before proceeding:

  • VS Code will show if there are merge conflicts when you select the pull request.
  • If conflicts exist, they will appear in the Source Control tab, and you can resolve them by opening the conflicting files. VS Code highlights conflicts and gives you options to choose between the conflicting lines.

Example:

Terminal
<<<<<<< HEAD
// Your current code
=======
// Incoming changes from pull request
>>>>>>> feature-branch

After resolving the conflicts, stage the changes and commit them to proceed with the merge.

Once the pull request is conflict-free and approved, you can merge it directly in VS Code:

  • In the Pull Requests view, click the pull request you wish to merge.
  • Select the Merge Pull Request option in the detail view.
  • You will be prompted to confirm the merge, and the pull request will be merged into the target branch (usually main).

Alternatively, you can merge the pull request via the command line in the VS Code terminal using Git commands:

Terminal
git checkout main
git pull origin main
git merge feature-branch
git push origin main

After merging, you can delete the feature branch that is no longer needed:

  • In the Pull Request view, VS Code may prompt you to delete the branch.
  • Alternatively, you can manually delete the branch using Git:
Terminal
git branch -d feature-branch
git push origin --delete feature-branch
  • Merge conflicts: If you frequently encounter merge conflicts, make sure you regularly pull changes from the main branch to your working branch before opening the pull request.

  • Authentication issues: If you're unable to push or merge, ensure you're logged into GitHub or GitLab through the GitHub Pull Requests extension, or use SSH keys for authentication.

  • Permission errors: Ensure you have the correct permissions on the repository. You may need admin or write access to merge certain pull requests.

Not comfortable with command-line interfaces (CLI)? Graphite's VS Code extension is perfect for developers who prefer a graphical user interface. It allows you to create and manage stacked pull requests directly from your IDE, offering a visual way to break up large tasks into smaller, incremental code changes.

To install the Graphite VS Code extension, make sure you have the Graphite CLI set up. Once that's done, follow these steps:

  1. Install from the Visual Studio Marketplace: Head over to the Visual Studio Marketplace and search for "Graphite" or search for it directly in the Extensions tab in VS Code.

  2. Search in VS Code: Open your IDE and use the Extensions tab (click the square icon on the sidebar). Search for "Graphite" and install the extension.

After installing, you can launch the Graphite VS Code extension in a few ways:

  • Click the Graphite icon in your sidebar. If you prefer, you can drag it to your sidebar for easier access.
  • Run the command: Use the command palette (Ctrl + Shift + P or Cmd + Shift + P) and type Graphite: Open Graphite interactive.
  • Set up a custom keyboard shortcut: Define a custom shortcut to run the graphite.open-gti command and launch the extension quickly.

Tip: You can customize where the Graphite icon appears in VS Code. Modify the "Show in Sidebar" setting to display it in an editor tab instead of a sidebar tab.

By following these steps, you’ll be able to manage and merge pull requests seamlessly in VS Code. With the tight Git integration, it’s easy to collaborate with your team, review code, and keep your workflow within a single environment.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2