Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit » Proper way to create new branches?
Proper way to create new branches? [message #894047] Fri, 06 July 2012 13:39 Go to next message
ROBERT JONES is currently offline ROBERT JONESFriend
Messages: 2
Registered: July 2012
Junior Member
Hello there,

We've just recently started using Git. Previously our shop was using SVN. With that in mind this might just be a newbie question.

When I create a new local branch and then push it up to the github repository, I'm unable to pull changes until I go to the egit properties and manually add the repository settings for the new branch. I would assume that there is a way to create these settings automatically.

In addition anyone who tries to retrieve the new branch has issues. They are able to see the new branch in the remote repositories view but the only way we've been able to pull that branch down locally seems kind of convoluted.

The developer who wants to use the new branch first has to create a new branch locally with the same name and then they have to manually add the configuration for the new branch.

I'm really hoping that there's a better way to do this and we can chalk this up to our own ignorance about egit and git.

Thanks in advance for any help!
Re: Proper way to create new branches? [message #894073 is a reply to message #894047] Fri, 06 July 2012 14:30 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias SohnFriend
Messages: 612
Registered: July 2009
Senior Member
In Git a local branch can track another branch. This other branch is most often a so called "remote tracking branch" which is a special type of local branch residing in your local clone and tracking updates you may fetch from a branch with the same name in the corresponding remote repository.

The remote repository is stored in your clone's configuration as a "remote" which is simply a named alias for the URL needed to reach the remote repository. When creating a clone the repository you are cloning from is stored as remote "origin" by default.

In order to get updates from the remote repository you run "fetch origin". The default "fetch configuration" is refs/heads/*:refs/remotes/origin/* and maps branches in the remote repository (left hand side of fetch configuration) to remote tracking branches in your local repository (right hand side of fetch configuration). Hence when you run fetch on remote "origin" e.g. from EGit's Repositories View it fetches updates for all branches existing in the remote repository "origin" points at and maps them to remote tracking branches under "refs/remotes/origin/". If you happen to fetch changes from multiple remote repositories you are going to use multiple remotes e.g. "origin", "joe" and "anne" in order to decide which remote repository you want to trade changes with.

Now back to your question: if you want to use pull, since a local branch can only track one other branch,
you need to have (at least one) local branch which is tracking the remote branch via your local repositories' corresponding remote tracking branch. The tracking relationship isn't tied to the local branch's name but explicitly configured in .git/config.

With EGit you can establish this tracking relationship most easily by creating the local branch in the following way:
- in Repositories View select the remote tracking branch (e.g. "origin/master") your new local branch should track
- click "Create Branch" and select any name you want (e.g. "myNewFeature"), select the "Pull strategy" you want
- click "Ok"
the new local branch is now configured to track the branch "master" in the repository "origin" points at.

This configuration is stored in the following way in .git/config :

[branch "myNewFeature"]
remote = origin
merge = refs/heads/master

If you have chosen pull strategy "Rebase" this entry is a bit different:

[branch "myNewFeature"]
remote = origin
merge = refs/heads/master
rebase = true

If branch "myNewFeature" is checked out and you click "Pull from Upstream" EGit will
- first fetch all updates corresponding to the fetch configuration for the remote "origin" which your local branch is tracking
- and then, depending on the chosen pull strategy, merge or rebase your local branch "myNewFeature" with the updates you received on the remote tracking branch "refs/remotes/origin/master"

Usually you have many local branches tracking origin/master (shortened form of refs/remotes/origin/master) since often you have multiple changes you are working on in parallel, which should end up on the same branch in the remote repository.
Re: Proper way to create new branches? [message #894099 is a reply to message #894073] Fri, 06 July 2012 16:03 Go to previous messageGo to next message
ROBERT JONES is currently offline ROBERT JONESFriend
Messages: 2
Registered: July 2012
Junior Member
Thanks for the reply. I'll give this a shot and pass a link to this post on to my team. Hopefully this will clear up confusion about how to pull down new branches from the remote repository.

If you wouldn't mind enlightening me a little more, what is the best way to create a branch that will live remotely from within egit? I'll give you an example of what we were doing and the problems we ran into.

We decided (right or wrong) that we wanted two branches that lived on github. The 'master' branch and a 'dev' branch. The 'dev' branch is what we are using as 'trunk' this is where unreleased code would be checked in and nightly builds would be created from. The 'master' branch would then be used for releases. The idea being that when we were ready to build release candidates we would merge (rebase?) from 'dev' to 'master' and do any bug fixes for the released version on the 'master' branch.

When I created the dev branch I right clicked on the project and clicked Team > Switch To > New Branch. Named the branch 'dev' I then pushed this to the remote repository which created the remote branch. The problem then was that the configuration for the dev branch was not correct and I had to manually edit the repository configuration to set the remote references correctly.

Is there a better way to do this?
Re: Proper way to create new branches? [message #894109 is a reply to message #894073] Fri, 06 July 2012 16:46 Go to previous messageGo to next message
R Shapiro is currently offline R ShapiroFriend
Messages: 386
Registered: June 2011
Senior Member
The original question might have a simpler answer: egit doesn't behave quite the same as Git when it comes to creating a new branch in the origin repository. Or at least it didn't last time I did this.

In Git if I create and push new branch, my local branch will automatically track the branch I just created in the origin.

In egit this doesn't happen: I have to configure the tracking manually. Possibly this is what tripped up the original poster.

Re: Proper way to create new branches? [message #894346 is a reply to message #894073] Sun, 08 July 2012 14:09 Go to previous message
R Shapiro is currently offline R ShapiroFriend
Messages: 386
Registered: June 2011
Senior Member
Quote:
Usually you have many local branches tracking origin/master (shortened form of refs/remotes/origin/master) since often you have multiple changes you are working on in parallel, which should end up on the same branch in the remote repository.


Certainly Git supports this mode. But in my experience a slightly different approach is much more common. Instead of multiple local branches tracking the same remote reference, one local branch tracks the remote ref, via 'merge', and all the others track that first one, via 'rebase'.

That first local branch, the one the tracks the remote ref, is used purely as a merge point - no edits are ever done here. The real work is always done in the other branches, which are kept in sync via 'rebase'. Think of it as a kind of staging area that sits between the per-task work branches and the remote.

So the branch 'master' is configured to track 'origin/master' in the usual way. To push to or pull from the origin, you always go through 'master'. The 'pull' (or 'fetch') will merge changes into 'master'

You never work directly in 'master'. Instead the work for each task is done in a new work branch that tracks 'master', in this case via rebase rather than merge. A 'pull' in one of these work branches will rebase it onto master. To get a the changes from a work branch back into master you would merge them.


There are more steps in this approach but I think it also lessens the likelihood of messy conflict resolution, and that's always a good goal.

The main downside with local tracking in egit is that status decorations are broken. This is a long-standing bug which I wish would be fixed.




Previous Topic:Add to version control
Next Topic:How can i get the list of commited Files
Goto Forum:
  


Current Time: Mon Nov 24 07:02:55 GMT 2014

Powered by FUDForum. Page generated in 0.11561 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software