Use Git branches to switch contexts, suspend work, and isolate risk

You can use branches to switch contexts, suspend work, and isolate risk. Some people create a “topic” branch for each task they perform. When they are satisfied with the work, they merge it back into the master branch. You have the option to publish the branch into a remote repository (such as a Git team project) to collaborate with others.

For example, you are working on some tasks in the interactive branch. An urgent bug on the master branch is assigned to you. You commit your changes in the interactive branch. You then create a new branch called hotfix1, which is based on master. After you’ve finished in hotfix1, you publish that branch to your team project so others can test it. You then and then switch back to the work you were doing on interactive. When you are done, you merge those changes into master and push them to the team project.

  • Create a branch

  • Switch among branches

  • Merge a branch

  • Publish a branch

  • Conduct a Git pull request (for an on-premises team project you must first install Team Foundation Server Update 4)

  • Get (create) a published branch

  • Remove a branch

    • Unpublish a remote branch

    • Delete a local branch

  • View content of a branch from your dev machine

  • View content of a published branch from your web browser

    • Explore the files in a published branch

    • Get the history of a published branch

    • Compare published branches

  • Q & A

Create a branch

Create a branch from the branches page (Keyboard: Ctrl + 0, N).

Creating a new branch on the Branches page

Tip

If you need to set aside some changes, create a new branch based on the one you are working in and then commit the changes in the new branch.

Switch among branches

When you switch branches, you are switching your workspace from one version of your files to another.

Branches page with multiple branches

You can switch among your branches from the branches page (Keyboard: Ctrl + 0, N), home (Keyboard: Ctrl + 0, H), the changes page (Keyboard Ctrl + 0, G) and the commits page (Keyboard: Ctrl + 0, O).

Merge a branch

When you’re ready, merge the work you’ve done in one branch into another branch. For example, Raisa has completed some work in her local interactive topic branch, and she wants to integrate this work into the team’s master branch. She merges interactive into master.

Merge command highlighted on Branches page

Publish a branch

When you want to collaborate or preserve the work you’ve done in a branch, you can publish it. For example, Raisa publishes her hotfix1 branch so that Jamal can take a look at the work she’s done.

Publishing a branch from the Branches page

Get (create) a published branch

After one of your team members has published a branch, you can get a copy of it on your dev machine and make changes from there. For example, Jamal wants to test the hotfix1 branch that Raisa has published to their team project.

  1. Go to the commits page (Keyboard: Ctrl + 0, O) and then fetch the latest changes from your team project.

    Fetch link on Commits page

  2. From the branches page, create a copy of the branch on your dev machine.

    Creating a branch from a remote branch

Remove a branch

After you have merged your changes into another branch or decided to abandon the work, you can remove the branch. Before you remove the branch, you must switch your workspace to another branch, as explained above.

Unpublish a remote branch

Before you can unpublish a remote branch, you must have a copy on your dev machine. If you don’t already have it, see Get (create) a published branch. You must also have sufficient permissions. Otherwise, an error appears when you try.

Branches page with Unpublish Branch highlighted

After you unpublish the branch, you can delete your local copy if you no longer need the data.

Delete a local branch

Important

Before you delete a local branch, make sure that your team has a copy of it somewhere else, or that it does not contain any commits that you might need. See the sections below for information about how to view the contents of a branch.

You can delete a local branch from the branches page.

Branches page with Delete highlighted

View content of a branch from your dev machine

You can view the history of both published and unpublished branches, as well as details about each commit. To make sure you are getting the latest history of a published branch, go to the commits page (Keyboard: Ctrl + 0, O) and fetch the latest commits from the team project. Then view the history.

Branches page and History window

See View and manage past versions in Git.

View content of a published branch from your web browser

You can get more information about your published branches in your web browser (Keyboard: Ctrl + 0, A).

On the Home page, choose Web Access

Explore the files in a published branch

You can explore and view the content of files in a published branch.

Web Portal File Explorer in Git team project

Get the history of a published branch

You can view the history of a published branch.

Branch history on web portal

See View and manage past versions in Git.

Compare published branches

You can compare one published branch with other published branches.

Git branches on the web portal

For example, the hotfix1 branch has one commit that the interactive branch lacks, but it is missing two commits that the interactive branch has. You can choose the links to drill down further to get details about these differences. Or, you can compare from the context menu of the branch.

Comparing Git branches on the web portal

A page appears to display the two commits that hotfix1 lacks and that interactive has.

Comparing Git branches on the web portal

When you choose Swap branches to swap the branches, a page appears to show you the commit that hotfix1 has and interactive lacks.

Comparing Git branches on the web portal

Q & A

  • Q: I was blocked by the system because I don’t have permission. How do I get it?

    A:

  • Q: When I tried to merge I was blocked by conflicts. How do I resolve them?

    A:Resolve Git conflicts.

  • Q: When I create a branch, does it matter whether I choose branch-name (a local branch) or origin/branch-name (a remote branch)?

    A: Yes. For example:

    Branch source options

    If you create a branch from interactive, then the branch is unpublished. This means it exists only on your dev machine until you decide to publish it. If you create a branch from origin/interactive, then the branch is automatically published.

  • Q: Why did I get the following message when I tried to switch to another branch?

    A: If your current branch references a different commit than the branch to which you want to switch (and this is often the case), then you must first commit or undo your uncommitted changes.

    Switching branches blocked by uncommitted changes

  • Q: What can I do if I’m not ready to commit my changes but I want to switch to another branch?

    A: You have a couple of options:

    • If you don’t need the changes can undo them from changes page (Keyboard: Ctrl + 0, G).

    • You can create a new local branch and commit the changes there. For example, you make some changes in the master branch. You decide that you want to put these changes away and try a different approach. You can create a possible_fix branch based on master, switch to possible_fix, and commit the changes in that branch.

    • You can stash the changes from the command prompt. See Work from the Git command prompt.

  • Q: I want to merge my topic branch into our master branch. The topic branch contains a lot of commits that I don’t want to include in the history. How can I combine them into a single commit before I merge?

    A: You can squash commits from the command prompt. See Work from the Git command prompt.

  • Q: I see a branch in my published branches section, but on the web portal, I don’t see the branch. What happened? Is the branch published or not?

    A: When a branch appears in the published branches section, this indicates that the branch was published at one point by someone on the team. After that, someone might have unpublished the branch. You can move the branch to your unpublished branches section by selecting the branch, opening its context menu and choosing Unpublish Branch, and then choosing RefreshRefresh.

  • Q: Can I apply different permissions to different branches?

    A: Yes. See Git repository permissions.

  • Q: Can I work from the command prompt?

    A: Yes. Work from the Git command prompt.

  • Q: Can I create a branch from a specific commit or tag?

    A: Yes. You can do this from the command prompt using the <start-point> option. See Git-scm: git-branch(1) Manual Page

  • Q: What are some other uses for branches?

    A: Many teams use long-standing branches to:

    • Manage concurrent work by multiple teams on the same codebase

    • Isolate risks that are introduced by different sets of changes to the codebase

    • Take snapshots and then support subsequent isolated changes (for example, to create a release branch)

    See Git-scm: Git Branching - Branching Workflows and Visual Studio TFS Branching Guide.

  • Q: Where can I get more information about how branches work in Git?

    A:Git-scm: Git Branching