Sometimes, when using Git repositories and the Eclipse IDE, we want to access other remote repositories within the one we are currently using.
One example, which I will use as the scenario for the steps below: a team converted existing code and its history to Git from another source-control management tool, with separate repositories for closely related but distinct release points. When they need to make a fix to a past release, on the past release's "hotfix" branch, they want to do as little work as possible to bring that fix into the current cutting-edge "dev" branch of their main repository.
Other writers have offered How-To's for the git command-line steps to do so. What I will do is to show how the setup and configuration can be done within the Eclipse IDE (I used Mars for the screen-shots).
You can use Eclipse to set up one repository with branches from another remote repository. This opens possibilities such as using Git's Cherry-pick feature between the dev
branch and another repository’s hotfix branch.
First, add the Past-Release repository as a Remote for your local copy of the main repository:
- in Git Repositories view, expand the main repository and right-click on Remotes
- select Create Remote
- give it a name that is meaningful to you, it does not have to match the name of the Remote repository
- select "Configure fetch" - we will handle the Push later
- click OK
- on the Configure Fetch dialog, enter the Remote URI
- click Save and Fetch
The result should be new entries under Remotes and under Branches - Remote Tracking for your local Repository:
Next, create a new Local branch for the hotfix branch of the Remote:
- right-click on the repository or the Branches and select Switch to.. New Branch
- in the dialog, click the Select button and choose the "hotfix" branch of your new Remote Tracking repository as the Source for this new branch. Click OK
- give the new branch a meaningful name, it does not have to be "hotfix" if you prefer something like "1602_hotfix" or another name
- check "Configure upstream for push and pull" and select "Rebase commits of local branch onto upstream"
- to work on this branch right away, check "Checkout new branch"
Now you can make changes directly on this new local
branch, or cherry-pick commits between branches, since you now have the
hotfix branch in the same local repository as the dev branch.
Once you have made and saved changes on your local
hotfix branch, be sure to select “Commit” in the Git staging area, not "Commit and Push" – we want
to do a quick double-check of our settings before we Push.
When a hotfix Commit is ready to Push:
- right-click and select "Push Branch..."
- verify that Source is your local branch and correct commit, and that Destination is the Remote repository with Branch = "hotfix"
- click "Next"
- on the Push Confirmation dialog, select "Cancel push if ... changes on remote" (if you hate pop-ups, also check "Show dialog...")
- click "Finish"
The result should be that the upstream
release repository receives the hotfix changes, without
needing to make the changes multiple times or doing any copy-pasting.
Remember:
Be careful with your own scenarios that mix the dev branch and hotfix branches
- they should not be Merged or Rebased with each other. While the repositories in this scenario have a common history to a point, they have diverged since, and
attempting to merge them will produce many conflicts or other questionable results.