Saturday, January 21, 2012

Egit and Github: Pulling from a Master Repository and Pushing into Your Own Forked Repository

So I'm new to the world of git and Github, so I'm trying to figure out how all this stuff works. Making things more complicated is the fact that I'm using Eclipse and its egit plugin, but most of the documentation on working with Github assumes that you're working with the command-line version of git. Although there is some documentation on how to use egit with Github, it mostly assumes that you created your own repository in git for your own projects. Github encourages people to take an existing project, fork it, and then work with your own copy of the repository though. This then makes things difficult when you need to pull in changes from the original master repository and stick them in your own repository.

Again, there is documentation on how to do get your forked repository in-sync with the master repository, but I'm still trying to feel my way about how to do this in Eclipse with egit. So far, here are some sets of instructions that seem to work with Eclipse Indigo 3.7 (but I'm still feeling my way around though, so I could be wrong).

So I'm assuming that you've forked a repository on Github, and you've successfully used the egit defaults to make a local repository based on your fork. So in the Git Repository Exploring perspective of Eclipse, you go into your repository.

  1. In the Remotes section, right-click and choose "Create Remote"
    1. Set the name to "upstream" and configure it for fetch
    2. the URI should be set to the git URI of the original master repository on Github that you forked
    3. "Add" a RefSpec
    4. the source should be master, and the destination should then be automatically filled in to refs/remotes/upstream/master
    5. Save your changes
  2. In the Remotes section, expand the upstream configuration, right-click the green incoming arrow, and choose Fetch
  3. After fetching, I think the latest changes are now available in your local repository, so now you want to apply those changes to your local branch, and then push them out to your forked repository
  4. So make sure you have your local master checked out or whatever
  5. Right-click your repository and choose Merge...
  6. Merge in from upstream/master
  7. And then you can push everything back into your forked repository on Github with a simple Right-click on your repository and Push.

It looks like egit might also support some sort of special Upstream configuration or something, so that egit will automatically track the master version of a fork, but it looks like the forked repository needs to be configured a special way, and I don't know how to get Github to do this. I imagine if you dig into egit's property files, there'll be some way to enable it, but I can't figure it out yet.

5 comments:

  1. Thank You.

    This post made me happy.

    ReplyDelete
  2. for step 3, how do you apply the changes to your local branch? thanks

    ReplyDelete
  3. Step 3 simply describes what comes next. Steps 4-6 are what you need to do to actually apply the changes to your local branch.

    ReplyDelete
  4. Thanks for the clear explanation.

    I just did this on Juno. Step 2 is now incorporated in step 1.5.

    ReplyDelete