Git Branches

Using Branches

To create a new git branch or switch between existing branches, go to Git > Branches:

Command line equivalents:
list of local branches

git branch

list of local and remote branches

git branch -a

create a new branch

git branch <branch name>

checkout to a branch

git checkout <branch name>

create and immediately checkout to the newly created branch

git checkout -b <branch name>

remove a branch

git branch -d <branch name>

Master branch is checked out by default.

branches

You can create new branches, switch between multiple branches or delete unnecessary ones.

Merge One Branch Into Another

To perform branch merging, switch to the “target” branch you want to merge data to, and go to Git > Merge.

Choose to create a local or remote branch and press Merge.

merge

If the branches are successfully merged, you will see a notification in the Output panel.

Command line equivalent

git merge <branch name>

If the branches cannot be merged, you will have to resolve conflicts.

Working With Merge Conflicts

You will occasionally face merge conflicts providing the files to be merged until the conflicts have to be resolved.

Below is a simple example of such a conflict and the way to resolve it.

A new project has been created and initialized, and a new test_branch has been created. Java class file was edited, saved, added to index and then committed.

public class GreetingController implements Controller
{

   @Override
   public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
   {
      String userName = request.getParameter("user");
      String result = "";
      if (userName != null)
      {
        result = "Hello, dear" + userName + "!";
      }

      ModelAndView view = new ModelAndView("hello_view");
      view.addObject("greeting", result);
      return view;
   }

After checking out to a master branch the same file has been edited (Mr. instead of dear), and again added to index and committed.

public class GreetingController implements Controller
{

   @Override
   public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
   {
      String userName = request.getParameter("user");
      String result = "";
      if (userName != null)
      {
        result = "Hello, Mr." + userName + "!";
      }

      ModelAndView view = new ModelAndView("hello_view");
      view.addObject("greeting", result);
      return view;
   }

It’s time to merge branches now at Git > Merge. As expected, there are merge conflicts which have to be resolved.

conflict

There are two things you will notice after the merge conflict message. First, the file with merge conflicts is marked with a red asterisk in the Project Explorer.

conflict_icon

Secondly, you will find detailed conflict info in the file itself:

public class GreetingController implements Controller
{

   @Override
   public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
   {
      String userName = request.getParameter("user");
      String result = "";
      if (userName != null)
      {
<<<<<<< HEAD
        result = "Hello, Mr." + userName + "!";
=======
        result = "Hello, dear" + userName + "!";
>>>>>>> refs/heads/test_branch
      }

      ModelAndView view = new ModelAndView("hello_view");
      view.addObject("greeting", result);
      return view;
   }
}

When the file is cleaned up from unnecessary text (info on the merge conflict) and only one of the code line version is left, you can add this file to the index and commit it. The conflict is resolved and the asterisk is gone.
Up ↑