Home » Software Development » Git » Git Remote Branch Example

About Mary Zheng

Mary Zheng
Mary has graduated from Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She works as a senior Software Engineer in the telecommunications sector where she acts as a leader and works with others to design, implement, and monitor the software solution.

Git Remote Branch Example

1. Introduction

Git is an open source distributed version control system designed to handle everything from small to large projects with speed and efficiency. Three-tree architecture is the key concept. Here are the three trees:

  • Repository tree – The place to store the committed files. It can be local or remote. The remote repository is used to track the same project but is stored somewhere else.
  • Working tree – The tree of actual files that you are currently working on. It normally contains the check out files and any local changes that you have made but not yet committed.
  • Staging index tree – It acts as an interface between a repository and a working tree. It prepares the committing files.

Figure 1 GIT Repositories

This diagram shows my open source project – Deadlock-demo‘s local and remote repositories.

In this example, I will demonstrate how to manage remote branches via EGit.

2. Technologies and Tools Used

  • Java 1.8.101 (1.8.x will do fine)
  • Maven 3.3.9 (3.3.x will do fine)
  • Eclipse Mars (Any Java IDE would work)
  • Git 2.16.*

3. Use Cases

Deadlock-Demo is my open source project hosted in GitHub at https://github.com/maryzheng01/Deadlock-demo. It has a default master branch. In this example, I created two remote branches from the master:

EGit is the Git integration for the Eclipse IDE. Click here for more details. I will demonstrate below use cases with EGit:

  1. Clone the master branch to a local repository.
  2. Modify the working files and then push the changes to the remote master branch.
  3. Switch the working files to Branch-Bug101-Mary and push the changes to the remote Branch-Bug101-Mary.
  4. Repeat step 3 for Branch-Bug101-Alex.
  5. Merge the changes from Branch-Bug101-Mary to Branch-Bug101-Alex and push the changes to the remote Branch-Bug101-Alex.

4. Clone from a Remote Repository

In this step, I will clone the source codes from a master branch via EGit:

  1. Click Window->Perspective ->Open Perspective ->Other…->Git to switch to Git perspective.
  2. In Git perspective window, click the cloning icon to clone from a remote repository.
  3. Enter the remote repository data as the screenshot below (replace the login with your credentials) and then click Next button.

    Figure 2 Git Clone

  4. Check all three remote branches, and then click the Next button.

    Figure 3 Clone – Branch

  5. Set the local repository location; leave all others as the default value. Click Finish.

    Figure 4 Clone – Local Repo

  6. You can expand the Remote Tracking folder to list the remote repositories.

    Figure 5 Cloned

5. Modify and Commit the Changes

In this step, I will show you how to modify, commit, and push the changes to the remote repository.

  1. In Git Perspective, right-click the Deadlock-Demo project and select Import Projects...

    Figure 6 Import

  2. Modify the source files and save the changes.
  3. Select Deadlock-Demo, right-click to select Team->Commit…
  4. Drag and drop the files from Unstaged Changes to Staged Changes.

    Figure 7 Modified

    Figure 8 Staged

  5. Click the Commit and Push button and click OK to confirm.

    Figure 9 Commit

  6. Verify by checking the GitHub repository.

    Figure 10 Verify from GitHub

6. Switch to a Different Branch

EGit provides an easy way to switch to a different branch. In this step, I will show you how to switch to a different branch.

First, you select the Deadlock-Demo project. Then right-click and select Team->Switch to->New Branch…

Select Branch-Bug101-Maryand then click Finish.

Figure 11 Switch to ok

Modify the source codes, save them, and follow step 5 to commit to the remote branch.

Repeat these steps for Branch-Bug101-Alex.

7. Merge between Branches

In this step, I will demonstrate how to merge the changes from Branch-Bug101-Mary to Branch-Bug101-Alex.

First, follow step 5 to switch to Branch-Bug101-Alex. Select it and right-click Team->Merge…

Select the source Branch-Bug101-Mary, leave others as the default value, and then click Merge.

It will pop a new window for merge results.

Figure 12 Merge 1

It pops out a new window for the merge result.

Figure 13 Merged OK

After you merge, you will see that there is an up arrow with a number 2 right next to the project. It indicates that there are two commits that can be pushed to upstream.

Figure 14 Merged

Select Branch-Bug101-Alex, right-click Team->Push to Upstream.

Figure 15 Pushed

8. Summary

In this example, I showed how to clone a project from a remote repository, then update them and commit the changes back to the remote repository. I also demonstrated how to switch to a different branch and how to merge the changes between two branches via EGit. These steps are part of a Git branching workflow.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of