Update code with fetch and pull

Last Update: 3/30/2017
Team Services | TFS 2015 & TFS 2017 | Visual Studio 2015 & 2017

Overview

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.

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 2015 & 2017

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.

Fetching changes in Visual Studio's Team Explorer Synchronization View

> git fetch
remote: Found 3 objects to send. (9 ms)
Unpacking objects: 100% (3/3), done.
   e2ccee6..55b26a5  feature1   -> origin/feature1 

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.

Visual Studio 2015 & 2017

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.

Running merge without any flags or parameters will add the commits downloaded from fetch into the local branch. Git will add a merge commit if you have any conflicts. This merge commit has two parent commits (one for each branch) and contains the changes committed to resolve the conflicts between branches.

> git merge
Updating e2ccee6..55b26a5
 1 file changed, 1 insertion(+)

You can merge without committing using --no-commit to attempt to perform the merge but not commit the final changes, which gives you a chance to inspect the changed files before finalizing the merge with a commit.

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.

Visual Studio 2015 & 2017

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.

Pulling Changes with Visual Studio

git pull without any options will do a fetch of the changes you don't have from origin and will merge the changes for your current branch.

> git pull
Updating 55b26a5..e7926cd
 1 file changed, 2 insertions(+), 1 deletion(-)

Pull a remote branch into a local one by passing remote branch information into pull:

> git pull origin users/frank/bugfix

This is a useful way to directly merge the work from remote branch into your local branch.

What's next

Share code with push and resolve merge conflicts