Update code with fetch and pull

VSTS | TFS 2018 | TFS 2017 | TFS 2015 | VS 2017 | VS 2015

Update the code in your local repo with the changes from other members of your team using the following commands:

  • fetch , which downloads the changes from your remote repo but does not apply them to your code.
  • merge , which applies changes taken from fetch to a branch on your local repo.
  • pull , which is a combined command that does a fetch and then a merge.

In this tutorial you learn how to:

  • Download changes with fetch
  • Update branches with merge
  • Fetch and merge with pull
  • Update your local branch with the latest changes from master

Video Overview

If there is a merge conflict between a commit you haven't pushed yet and a commit you are merging or pulling, you'll need to resolve those conflicts before you finish updating your code.

Download changes with fetch

You download changes to your local branch from the remote through fetch. Fetch asks the remote repo for all commits and new branches that others have pushed but you don't have and downloads them into your repo, creating local branches as needed.

Fetch does not merge any changes into your local branches, it only downloads the new commits for your review.

Visual Studio uses the Sync view in Team Explorer to fetch changes. Changes downloaded by fetch are not applied until you Pull or Sync the changes.

  1. Open up the Synchronization view in Team Explorer by selecting the Home icon and choosing Sync.

    Synchronization view

  2. Choose Fetch to update the incoming commits list. (There are two Fetch links, one near the top and one in the Incoming Commits section. You can use either one as they both do the same thing.)

    Fetch

  3. You can review the results of the fetch operation in the incomming commits section.

    Incoming commits

Update branches with merge

Apply changes downloaded through fetch using the merge command. Merge takes the commits retrieved from fetch and tries to add them to your local branch. The merge will keep the commit history of your local changes so that when you share your branch with push Git will know how others should merge your changes.

The challenge with merge is when a commit taken from fetch conflicts with an existing unpushed commit on your branch. Git is generally very smart about resolving merge conflicts automatically, but sometimes you must resolve merge conflicts manually and complete the merge with a new merge commit.

Team Explorer merges when you do a Pull or a Sync from the Changes view. Sync is a combined operation of pulling remote changes and then pushing local ones, synchronizing the commits on the local and remote branch.

  1. Open up the Synchronization view in Team Explorer by selecting the Home icon and choosing Sync.

    Synchronization view

  2. Choose Sync.

    Synchronization view

  3. A confirmation message is displayed when the sync operation completes.

    Synch operation complete

Fetch and merge with pull

Pull does a fetch and then a merge to download the commits and update your local branch in one command instead of two. Use pull to quickly bring your branch up to date with the remote when you aren't worried about reviewing the changes before merging them into your own branch.

Open the Team Explorer and open the Sync view. Then click the Pull link under Incoming Commits to pull remote changes and merge them into your local branch. Pulling updates files in your open project, so make sure to commit your changes before pulling.

  1. Open up the Synchronization view in Team Explorer by selecting the Home icon and choosing Sync.

    Synchronization view

  2. Choose Pull to fetch remote changes and merge them into your local branch. (There are two Pull links, one near the top and one in the Incoming Commits section. You can use either one as they both do the same thing.)

    Pull

  3. A confirmation message is displayed when the pull operation completes.

    Pull operation complete

Update your branch with the latest changes from master

When working in a branch, you may want to incorporate the latest changes from the master branch into your branch. There are two different approaches you can use to perform this: rebase or merge.

  • Rebase takes the changes made in the commits in your current branch and replays them on the history of another branch. The commit history of your current branch will be rewritten so that it starts from the most recent commit in the target branch of the rebase.
  • Merge merges the changes from the source branch to the target branch using a merge commit, which becomes part of the commit history.

Note

This article demonstrates the merge approach. For more information on rebase and guidance on which method is right for your scenario, see Apply changes with Rebase - When to rebase vs. merge and Rebase vs merge from the Pro Git book.

To merge the latest changes from the master branch to your branch:

  1. Open the Branches view in Team Explorer. Ensure your desired target branch is checked out, right-click the target branch, and choose Merge From.

    Merge from

  2. Specify the desired Merge from branch, which is master in this example, and choose Merge.)

    Merge from master

    If there are any merge conflicts you'll be notified at this stage. Resolve the merge commits before proceeding to the next step.

  3. Enter a commit message and select Commit Staged.

    Commit merge from master

  4. When you are ready to push your local commits, including your new merge commit, to the remote server, choose Push from the Synchronization view.

    Push

Next steps