A JBoss Project
Red Hat

How To:

Using Git with the IDE

The IDE includes the Git Perspective to allow developers to create, add, and manage their Git repositories quickly and easily with a graphical interface. This article introduces the basic workflow of a Git project and how to accomplish the most common Git-related tasks via the Git perspective. Common Git-based tasks include:

Set Up the Git Perspective

Use the following instructions to locate the Git Perspective in the IDE:

  1. In the menu bar at the top of the page, click Window  Perspective  Open Perspective  Other.

  2. From the list of available perspectives, select Git and click OK.

Result: The Git Repositories view appears on the upper left side of the window.

1. Set up a Repository in the Git Perspective

The first step to using the Git Perspective in the IDE is to set up a Git repository. There are three ways to set up a Git repository in the Git Perspective:

The most common use case for developers is to clone an existing repository, or import an existing local clone of a repository into the IDE. However, the first option is an easy way to create a full repository (bare or normal) from scratch.

Create a New Git Repository

If a repository is not already created and available, use the following steps to create a new repository:

  1. Click the Create a new Git Repository and add it to this view button.

    Click the Create a New Git Repository Button
    Figure 1. Click the Create a New Git Repository Button
  2. In the dialog box:

    1. Ensure that the automatically populated default value for the Repository Directory field is correct.

    2. Optionally, if required, check the Create a bare repository to create a new bare repository. For details about bare repositories and how they differ from a normal repository, see the Did You Know? section.

      Create a New Git Repository
      Figure 2. Create a New Git Repository

      Result: A new git repository is created on your local machine and is listed in the Git Repositories view.

Clone an Existing Git Repository

If your repository already exists online (for example, in GitHub), use the following steps to create a local clone:

  1. Ensure that you have forked the repository online. This option is available in the repository host’s website.

  2. Click the Clone a Git Repository and add the clone to this view button.

    Click the Clone a Git Repository Button
    Figure 3. Click the Clone a Git Repository Button
  3. Enter the details of the source repository as follows:

    1. Add the URI for the repository’s online source. This automatically populates the Host and Repository Path fields are automatically populated.

    2. Add your username and password for the source repository under the Authentication details.

    3. Click Next to continue.

      Enter the Source Repository Details
      Figure 4. Enter the Source Repository Details
  4. Customize the local version of your Git repository as follows:

    1. Confirm that the automatically populated information for the destination Directory and Initial Branch are correctly populated.

    2. Optionally, set a non-default name for the Remote name field.

    3. Optionally, select the Add project to working sets option and use the drop down menu and the Select button to select the appropriate working sets for this repository.

      Customize the Local Version of the Git Repository
      Figure 5. Customize the Local Version of the Git Repository
    4. Click Finish to conclude cloning an existing Git repository.

Result: The new cloned repository is listed in the Git Repositories view.

Add an Existing Local Git Repository

If you have already cloned a Git repository locally, the following instructions are necessary to add your Git repository to the IDE. If you have not yet cloned your repository, follow the instructions in the prerequisites section below:

Prerequisites

  1. Ensure that you have forked the repository online.

  2. In the command line on your local system, navigate to the location where you want to store the local copy of the repository and enter the following command to clone the repository:

    $ git clone ${repo_URL}

    ==== Add an Existing Git Repository Use the following instructions to add your existing local Git Repository to JBoss Developer Studio’s Git Perspective:

  3. Click the Add an existing local Git Repository to this view button.

    Click the Add an Existing Local Git Repository Button
    Figure 6. Click the Add an Existing Local Git Repository Button
  4. Select the local Git Repository as follows:

    1. Click Browse to navigate to the local directory that contains the Git repository.

    2. Optionally, select the Look for nested repositories checkbox to search for nested repositories.

    3. In the Search results box, ensure that the appropriate .git file is selected.

      Find and Add Local Repository
      Figure 7. Find and Add Local Repository
    4. Click Finish.

Result: The local repository now appears in the Git Repositories view.

Add a Remote for the Repository

After setting up your repository for the first time, set up a remote for repository. This is a one-time set up step for newly created or added repository.

  1. In the Git Repositories view, expand the target repository.

  2. From the expanded options, right-click Remotes and then Create Remotes.

  3. In the New Remote dialog box:

    1. Add a name in the Remote name field.

    2. Ensure that the Configure Push radio button is selected.

    3. Click OK to continue.

  4. In the Configure Push dialog box:

    1. Click Change to view the Select a URI dialog box.

    2. Add the URI to your repository in the URI field. This automatically populates the Host and Repository path fields.

    3. Add your repository username and password in the Authentication section of the dialog box and click Finish to continue.

  5. Click Save to save your push configuration settings.

Result: Expand the Remotes folder in the repository view to see the newly added remote.

2. Create and Work With a New Branch

This section provides instructions for creating a new branch and common tasks with the new branch, such as:

Creating a New Branch

If your repository is already set up in the IDE, create a new branch to make changes to the files.

  1. In the Git Repositories view:

    1. Expand the name of your Git Repository.

    2. Click Branches to expand the branch view.

    3. Click Remote Tracking to view all remote branches for the repository.

    4. A branch displays with a name that begins with origin/master. Right-click this branch and select Create Branch from the displayed options.

      Create a Branch from Origin/Master
      Figure 8. Create a Branch from Origin/Master
  2. Add the required details about the new branch:

    1. Add the desired new branch name in the Branch name field.

    2. Ensure that the Configure upstream for push and pull checkbox is selected.

    3. In the When doing a pull options, select the option that suits your requirement. The Merge upstream commits into local branch option creates commits when a merge occurs. These commits are included in the subsequent pull request. The Rebase commits of a local branch onto upstream option performs a rebase before adding your changes, which prevents additional commits in your pull request.

    4. Ensure that the Checkout new branch checkbox is selected.

      Add Details for a New Branch
      Figure 9. Add Details for a New Branch
    5. Click Finish to create the new branch.

Result: The new branch appears under Repository_Name  Branches  Local.

Working in the New Branch

After creating a new branch, you can implement changes in the new branch as follows:

  1. Expand Repository_Name  Branches  Local and find the new branch where changes are to be implemented.

  2. Confirm that the target branch is checked out. The currently checked-out branch displays a small black check mark:

    An Example of a Checked-out Branch
    Figure 10. An Example of a Checked-out Branch
  3. Right-click on the checked-out branch name and mouse-over the Show In option in the menu and then click Terminal in the submenu.

    The Show Branch in Terminal Option
    Figure 11. The Show Branch in Terminal Option
  4. On the right view, a Terminal tab appears. In the same row, click the icon that resembles a computer monitor to view the command line prompt in this view.

    The Open a Terminal Button
    Figure 12. The Open a Terminal Button
  5. In the Launch Terminal dialog box:

    1. Ensure that Local Terminal is selected in the Choose a Terminal drop-down box.

    2. Select Default (ISO-8859-1) in the Encoding box.

    3. Click OK. Note that as a default, the terminal window is at the /home/YourCurrentUser/ directory.

Result: The Terminal tab now displays a command line terminal. Use the terminal view to make the required changes to your checked-out files.

Updating the Branch Before Implementing Changes

When working locally on a branch, it is better to ensure the local branch is up to date before creating a pull request (PR). As an example, if someone else has checked out the same repository and created a new branch, made changes, and merged the changes, use the following procedure to update your repository and branch before committing your own changes.

In the example below, a new branch called TrackingID-1234 is created using the IDE. Assuming that someone else is working on the same repository and has created a new branch called NEWBRANCH, made changes to it, and then merged the changes back into the repository. The local branch (TrackingID-1234) is now out of date because it does not include the changes from NEWBRANCH. Use the following instructions to update the branch:

  1. Right-click the name of the repository to update.

  2. From the menu that displays, click Pull.

  3. A status menu appears that displays the progress of the pull request.

  4. When the pull completes, a Pull Result for Repository_Name menu appears that lists the results of the fetch and update operations.

  5. Click OK to conclude the operation.

Result: The repository now contains the most updated version of the contents.

4. Commiting and Merging Changes

After all required changes are complete, commit the changes and then create a Pull Request. Pull Requests are then evaluated by the repository owner and either merged into the repository or rejected.

  1. Ensure that the black and white tick mark that indicates the current branch appears at the correct working branch in the Local folder of your repository view.

  2. Right-click the name of the repository. In the displayed menu, click the Commit option.

  3. In the Commit Changes dialog box:

    1. Add a commit message describing the changes in the Commit message text box.

    2. Confirm that the automatically populated Author and Committer fields contain the correct name and email address.

    3. In the Files area, all files added using the git add command display in the box. Select the checkbox next to each file to include it in the commit. See the Did You Know? section to learn about only including some of the changed files in a commit.

      Add details to the Commit Changes Dialog Box
      Figure 13. Add details to the Commit Changes Dialog Box
    4. Click Commit to create a new commit (without creating a Pull Request) or click Commit and Push to commit the changes and create a Pull Request at the same time.

Committing Without Pushing the Changes

If you selected Commit in the previous procedure to commit changes but not push them, use the following instructions:

  1. When the operation completes, the repository is now ahead by one commit. This is represented with an arrow and the number one, as seen in the screenshot:

    Git Repository Status
    Figure 14. Git Repository Status
  2. When you are ready to create a Pull Request, right click the current branch name and click Push Branch.

  3. An automatically populated Push Branch Branch_Name dialog box appears. Confirm that the settings are correct. The settings selected when creating this branch are used for this step. Click Next to continue.

  4. A dialog box appears requesting the repository access username and password.

  5. A Push Confirmation dialog box appears. Click Finish to create the Pull Request. If requested, supply the username and password for the repository once again.

  6. When the operation completes, a Push summary dialog box appears. Click OK to dismiss this dialog box.

Result: The included changes are now committed and a Pull Request is generated for the repository owner to review.

Committing and Pushing the Changes

If you selected Commit and Push in the previous procedure, use the following instructions:

  1. A dialog box appears requesting the repository access username and password.

  2. When the operation completes, the repository is now ahead by one commit. This is represented with an arrow and the number one, as seen in the screenshot:

    Git Repository Status
    Figure 15. Git Repository Status
  3. After the Pull Request is evaluated and merged, right-click the repository and click Pull to manually update the repository.

Result: A Pull Request is generating and ready for the repository owner to review.

Did You Know?

  • Bare repositories are recommended for central repositories, but not for development environments. Bare repositories differ from normal repositories because they do not contain a working or checked out copy of any source files. This prevents editing files and committing changes in the repository. Additionally, they store the git revision history for your repository in the repository’s root folder instead of in a .git sub-folder.

  • When selecting the files to commit in the Commit Changes, you can select only the files you want included in the commit using the checkboxes next to each file name. The unchecked files are not included in the commit and no extra actions are required to reconcile the uncommitted files when committing and creating a Pull Request.

  • If you need to add a change ID to each commit message, in the Comming Changes dialog box, click the rightmost icon at the top right corner to add a change ID to the commit message.

back to top