Update code with fetch, merge, and pull

Azure DevOps Services | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018

Visual Studio 2022 | Visual Studio 2019 | Visual Studio 2017 | Visual Studio 2015

When there are several contributors to a project, keep your local Git repo updated past downloading and integrating work that others uploaded to the project's remote repo. These Git commands update your local repo:

  • Git fetch downloads any new commits that others uploaded to the remote repo. The remote-tracking branches in local repo cache are updated—local branches remain unchanged.
  • Git merge integrates commits from i or more than source branches into a target branch.
  • Git rebase integrates commits from a source branch into a target co-operative, merely uses a different strategy than Git merge.
  • Git pull performs a fetch and then a merge or rebase to integrate fetched commits into your current local co-operative.

Visual Studio uses a subset of those Git commands when you synchronize your local repo with a remote repo.

For an overview of the Git workflow, see Azure Repos Git tutorial.

This article provides procedures for the following tasks:

  • Download changes with fetch
  • Update branches with merge or rebase
  • Download changes and update branches with pull

Download changes with fetch

Git fetch downloads remote branch commits and referenced file objects that don't exist in your local repo and updates the remote-tracking branches in local repo cache. Remote-tracking branches are locally cached read-only copies of remote branches and aren't your local branches. Git fetch doesn't update your local branches. For instance, if a remote repo designated by origin has a bugfix3 co-operative, Git fetch volition update the remote-tracking co-operative named origin/bugfix3 and not your local bugfix3 co-operative. Yous tin can use remote-tracking branches to:

  • Compare a remote-tracking branch with a local branch to review fetched changes.
  • Merge a remote-tracking branch into a local branch.
  • Create a new local branch from a remote-tracking branch.
  • Visual Studio 2019
  • Visual Studio 2017
  • Git Control Line

Visual Studio 2019 version 16.eight and later versions provides a Git version command feel while maintaining the Team Explorer Git user interface. To utilize Team Explorer, uncheck Tools > Options > Preview Features > New Git user feel from the carte bar. You can use Git features from either interface interchangeably. Below, we provide a side-by-side comparison of how to perform a Git fetch.

  1. In the Git Changes window, choose Fetch. Then select approachable/incoming to open the Git Repository window.

Screenshot of the Fetch buttons and Incoming link in the Git Changes window of Visual Studio 2019.

You can also choose Fetch from the Git menu.

Screenshot of the Fetch option in the Git menu in Visual Studio 2019.

  1. In the Git Repository window, fetched commits appear in the Incoming department. Select a fetched commit to run across the list of changed files in that commit. Select a changed file to see a diff view of inverse content.

Screenshot of the Git Repository menu in Visual Studio 2019.

Visual Studio Team Explorer
  1. In Team Explorer, select Home and then choose Sync to open the Synchronization view.

Screenshot of the Sync option within Team Explorer in Visual Studio 2019.

  1. In the Synchronization view, choose Fetch. Both Fetch buttons do the same affair. When downloaded, fetched commits volition announced in the Incoming Commits section.

Screenshot of the Fetch button in the Synchronization view of Team Explorer in Visual Studio 2019.

  1. In the Incoming Commits section, correct-click a commit and and then choose View Commit Details to encounter the changed files.

Screenshot of the context menu for incoming commits in the Synchronization view of Team Explorer in Visual Studio 2019.

  1. In the Commit Details window, the changed files are listed in the Changes section.

Screenshot of the Commit Details window within Visual Studio 2019.

Double-click a changed file to open a unequal view of changed content.

Screenshot of a file diff window in Visual Studio 2019.

Tip

Fetch won't delete remote-tracking branches in your local repo enshroud that no longer have a remote counterpart. To configure Visual Studio to clip dried remote-tracking branches during a Fetch:

  • Select Tools > Options > Source Control > Git Global Settings.
  • Set the Prune remote branches during fetch choice to True.

After a Git fetch, you can compare a local co-operative with its corresponding remote-tracking branch to see what changed on the remote co-operative. If y'all decide to update your electric current local co-operative with fetched changes, you lot can perform a Git merge or rebase. Or, you lot tin can run Git pull, which combines a Git fetch with a Git merge or rebase. Both Git merge and Git rebase update a target branch past applying commits from a source branch onto information technology. However, Git merge and Git rebase use unlike strategies. For more than information, come across Update branches with merge or rebase and When to rebase vs. merge.

Update branches with merge or rebase

Git merge and Git rebase integrate commits from a source branch into your current local co-operative (target branch). Git merge performs either a fast-forward or a no-fast-forward merge. The no-fast-forward merge is also known as a three-way merge or true merge. Git rebase is some other type of merge. These merge types are shown in the following diagram.

Diagram showing the before and after commits when using merge and rebase.

Git merge and Git rebase are extensively used in the Git workflow. When working on a local feature or bugfix branch, it's common do to:

  1. Proceed your local main branch current with its remote counterpart by periodically pulling to fetch and merge remote commits.
  2. Integrate local main co-operative updates into your local characteristic branch using a rebase or merge.
  3. Back upwards your work on the local feature co-operative by pushing it to the corresponding remote branch.
  4. On feature completion, create a pull asking to merge your remote feature co-operative into the remote main branch.

This approach helps you:

  • Stay aware of recent work by others that might affect your work.
  • Promptly resolve any conflicts between your work and that of others.
  • Employ your new feature on top of up-to-date projection content.
  • Get a pull request review of your work.

Merge

For Git merge, if the tip of the target branch exists inside the source branch, the default merge type will be a fast-forward merge. Otherwise, the default merge blazon will be a no-fast-forward merge.

A fast-forrad Git merge tin can never accept a merge conflict because Git won't employ a fast-forward merge if the tip of the target branch has diverged from the source branch. By default, Git uses a fast-forward merge whenever possible. For example, Git volition apply a fast-forwards merge on a local co-operative that you but update past pulling from its remote counterpart branch.

A no-fast-forrad Git merge generates a new target branch "merge commit" that integrates source co-operative changes with target branch changes. The applicable changes are those made later the final commit that's common to both branches. In the preceding diagram, commit C is the final mutual commit in both branches. If any source branch change conflicts with whatsoever target branch alter, then Git will prompt you to resolve the merge disharmonize. The merge commit (L) contains the integrated source branch and target branch changes. The source and target branch tips (G and E) are the parents of the merge commit. In your branch'southward commit history, a merge commit is a useful marker for a merge operation, and clearly shows which branches were merged.

Git merge only modifies the target branch—the source branch remains unchanged. When you lot run across one or more merge conflicts, you must resolve them to complete the merge. Or, you tin abolish the merge operation and return the target branch to its prior state.

For more information on merge options and strategies, see the Git reference transmission and Git merge strategies.

Tip

If the source branch is a remote-tracking branch, ensure that branch is upward-to-date by running a Git fetch before the merge.

  • Visual Studio 2019
  • Visual Studio 2017
  • Git Command Line

Visual Studio 2019 version 16.8 and afterward versions provides a Git version command experience while maintaining the Team Explorer Git user interface. To use Team Explorer, uncheck Tools > Options > Preview Features > New Git user feel from the menu bar. You lot can use Git features from either interface interchangeably. Beneath, we provide a side-by-side comparison of how to perform a Git merge.

  1. Choose Git > Manage Branches from the carte bar to open the Git Repository window.

Screenshot of the Manage Branches option in the Git menu of Visual Studio 2019.

  1. In the Git Repository window, correct-click the target co-operative and select Checkout.

Screenshot of the Checkout option in the Git Repository window of Visual Studio 2019.

  1. Right-click the source branch, and select Merge <source-branch> into <target-branch>.

Screenshot of the Merge option in the Git Repository window of Visual Studio 2019.

  1. Visual Studio will display a confirmation message after a successful merge.

Screenshot of the merge confirmation message in the Git Repository window of Visual Studio 2019.

If the merge is halted due to merge conflicts, Visual Studio will notify you. You can either resolve the conflicts, or cancel the merge and render to the pre-merge state.

Screenshot of the merge conflict message in the Git Repository window of Visual Studio 2019.

Visual Studio Team Explorer
  1. In Team Explorer, select Home and cull Branches.

Screenshot of the Branches option in Team Explorer in Visual Studio 2019.

  1. In the Branches view, right-click the target branch and select Checkout.

Screenshot of the Checkout option in the Branches view of Team Explorer in Visual Studio 2019.

  1. Right-click the source branch, and select Merge From.

Screenshot of the Merge From option in the Branches view of Team Explorer in Visual Studio 2019.

  1. Verify the merge options and so click Merge.

Screenshot of the merge details in the Branches view of Team Explorer in Visual Studio 2019.

  1. Visual Studio will brandish a confirmation message subsequently a successful merge.

Screenshot of the merge confirmation message in the Branches view of Team Explorer in Visual Studio 2019.

If there are conflicts during the merge, Visual Studio volition notify yous. You can either resolve the conflicts, or abolish the merge and return to the pre-merge land.

Screenshot of the merge conflict message in the Branches view of Team Explorer in Visual Studio 2019.

Rebase

Git rebase resequences the commit history of the target branch and then that it contains all source branch commits, followed by all target branch commits since the last common commit. Some other mode to view it is that a Git rebase replays the changes in your target branch on top of the source branch history. If any source branch change conflicts with any target branch change, then Git volition prompt you to resolve the merge conflict. Git rebase doesn't create a merge commit. Notably, Git rebase changes the sequence of the existing target co-operative commits, which isn't the example for the other merge strategies. In the preceding diagram, commit K' contains the same changes equally 1000, but has a new commit ID because it links back to commit E instead of C.

Git rebase but modifies the target branch—the source branch remains unchanged. When yous encounter ane or more merge conflicts, you must resolve them to complete the rebase. Or, yous can abolish the rebase operation and render the target branch to its prior state.

If you're the just person working on your feature or bugfix branch, consider using Git rebase to integrate new master co-operative commits into it. Otherwise, use Git merge. For more information on Git rebase and when to use it, run across Apply changes with rebase and Rebase vs merge.

Tip

If the source branch is a remote-tracking branch, ensure that branch is up-to-date past running a Git fetch before the rebase.

  • Visual Studio 2019
  • Visual Studio 2017
  • Git Command Line

Visual Studio 2019 version sixteen.viii and later versions provides a Git version command experience while maintaining the Team Explorer Git user interface. To employ Squad Explorer, uncheck Tools > Options > Preview Features > New Git user feel from the menu bar. You tin can utilise Git features from either interface interchangeably. Below, we provide a side-past-side comparison of how to perform a Git rebase.

  1. Cull Git > Manage Branches to open up the Git Repository window.

Screenshot of the Manage Branches option in the Git menu of Visual Studio 2019.

  1. In the Git Repository window, right-click the target branch and select Checkout.

Screenshot of the Checkout option in the branch context menu in the Git Repository window of Visual Studio 2019.

  1. Right-click the source branch, and select Rebase <target-branch> onto <source-branch>.

Screenshot of the Rebase option in the branch context menu in the Git Repository window of Visual Studio 2019.

  1. Visual Studio will brandish a confirmation message afterwards a successful rebase.

Screenshot of the rebase confirmation message in the Git Repository window of Visual Studio 2019.

If the rebase is halted due to merge conflicts, Visual Studio will notify you. Yous can either resolve the conflicts, or cancel the rebase and return to the pre-rebase country.

Screenshot of the rebase conflict message in the Git Repository window of Visual Studio 2019.

Visual Studio Squad Explorer
  1. In Team Explorer, select Habitation and choose Branches.

Screenshot of the Branches option in Team Explorer in Visual Studio 2019.

  1. In the Branches view, correct-click the target branch and select Checkout.

Screenshot of the Checkout option in the Branches view of Team Explorer in Visual Studio 2019.

  1. Right-click the source branch, and select Rebase Onto.

Screenshot of the Rebase Onto option in the Branches view of Team Explorer in Visual Studio 2019.

  1. Verify the rebase options then click Rebase.

Screenshot of the rebase details in the Branches view of Team Explorer in Visual Studio 2019.

  1. Visual Studio will display a confirmation message later on a successful rebase.

Screenshot of the rebase confirmation message in the Branches view of Team Explorer in Visual Studio 2019.

If there are conflicts during the rebase, Visual Studio will notify you. You can either resolve the conflicts, or cancel the rebase and return to the pre-rebase state.

Screenshot of the rebase conflict message in the Branches view of Team Explorer in Visual Studio 2019.

Download changes and update branches with pull

Past default, Git pull combines a Git fetch and a Git merge to update your current local co-operative from its remote counterpart. Optionally, Git pull tin perform a Git rebase instead of a Git merge.

Unlike Git fetch, Git pull will update your electric current local branch immediately afterwards downloading new commits from the remote repo. Use Git pull when yous know yous want to update your current local branch correct after a Git fetch.

  • Visual Studio 2019
  • Visual Studio 2017
  • Git Control Line

Visual Studio 2019 version xvi.8 and after versions provides a Git version control feel while maintaining the Squad Explorer Git user interface. To utilize Squad Explorer, uncheck Tools > Options > Preview Features > New Git user feel from the menu bar. Yous can utilise Git features from either interface interchangeably. Below, we provide a side-by-side comparison of how to perform a Git pull.

Tip

To configure Visual Studio to rebase instead of merge when you Pull:

  1. From the Git menu, go to Tools > Options > Source Control > Git Global Settings.
  2. Ready the Rebase local branch when pulling pick to True.
  1. In the Git Changes window, cull Pull.

Screenshot of the Pull button in the Git Changes window of Visual Studio 2019.

Y'all can also cull Pull from the Git carte.

Screenshot of the Pull option in the Git menu in Visual Studio 2019.

  1. A confirmation message displays when the pull operation completes.

Screenshot of the pull confirmation message in the Git Changes window in Visual Studio 2019.

If there are conflicts during the merge portion of the pull performance, Visual Studio will notify you lot. You tin can either resolve the conflicts, or cancel the merge and render to the pre-merge land.

Screenshot of the pull conflict message in the Git Changes window in Visual Studio 2019.

Visual Studio Team Explorer
  1. In Team Explorer, select Home and cull Sync to open the Synchronization view.

Screenshot of the Sync option in Team Explorer in Visual Studio 2019.

  1. In the Synchronization view, choose Pull. Both Pull buttons exercise the aforementioned thing.

Screenshot of the Pull options in the Synchronization view of Team Explorer in Visual Studio 2019.

  1. Visual Studio will display a confirmation message when the pull operation completes.

Screenshot of the pull confirmation message in the Synchronization view of Team Explorer in Visual Studio 2019.

If there are conflicts during the merge portion of the pull operation, Visual Studio will notify yous. You tin either resolve the conflicts, or cancel the merge and render to the pre-merge state.

Screenshot of the pull conflict message in the Synchronization view of Team Explorer in Visual Studio 2019.

Adjacent steps

  • New to Git repos? Learn more