Branching with Git
Now that jQuery UI is on Github, I need to learn how to branch and merge with Git – and not just local branches, but also pushing branches to Git so that others can work on them.
The chapter on branching from Pro Git is a good start for learning branching in Git. And the next step, remote branches, has its own chapter as well.
In order to be able to repeat the necessary steps, I’ll describe here how to create a local branch, commit some changes, and then push that branch to your Github repository.
The following command creates a new branch and switches to that:
git checkout -b "newbranch"
To see the local branches and which one is the active one, use:
git branch
That should now display a list like
master
* newbranch
Where the asterix marks the new active branch. With that, you can modify stuff in the branch. Then, to add all modified files to a commit, along with a message:
git commit -a -m "Message along with modifications"
Now comes the interesting part, pushing the branch back to the remote repository, Github in my case:
git push origin tooltip
Git knows that “origin” refers to [email protected]:jquery/jquery-ui.git, so it’s enough to add the name of the branch.
This works so far, but isn’t optimal yet: To push from the tooltip branch, I need to use “git push origin tooltip” again. I’ve found two ways to fix this, both way to complicated for such a simple requirement.
Either manually edit your .git/config file and add the branch, you should then end up with something like this:
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "tooltip"]
remote = origin
merge = refs/heads/tooltip
For my “tooltip” branch, I added the entry below the entry for “master”, and just replaced “master” with “tooltip”.
To avoid editing that config file, I found this to work:
git branch -D tooltip
git checkout --track origin/tooltip
With branch -D I forced a deletion of the local branch, then used checkout with the –track option to check it out again and have Git set up the config entries for “tracking”. There should be an easier way to do that.
Update: Apparently you can configure Git to automatically setup the tracking via:
git config --global branch.autosetupmerge true
You can leave out “–global” to apply it only to the current project. Seems to be more useful as a global setting…