Friday, November 27, 2009

Git Part 2 - Share Code with (a few) friends

In my current project we shared code among a few team members without impacting the others using Git. Since it happened twice and I already forgot it once, I put it here as a personal reminder. Hopefully it will help you too.

Imagine you're working on a feature and a teammate wants to use your code. But you're not finished, your code might break the build or some other functionality is broken. You have a few choices. You can send a patch. You can accept that the build is going to be broken. Or you can use Git and create a temporary branch for the sub-team to work on.

Setting up

Let's say you need to work on a new feature. First you create your branch:
git checkout -b myfeautre


The feature takes longer than expected. And you need help. But your changes will break the application and you don't want to impact every one. So you push your branch back to origin:
git push origin myfeature

Get work done (with some help)

Those who want to help you can create their own local branch.
git fetch
git branch myfeature origin/myfeature


I suggest you delete your own local branch and create another one as above. Your new branch will be linked to origin/myfeature so push/pull will work as usual. If you know another way to do this without deleting your branch, please tell me.
To sent your changes back to origin/myfeature, simply do
git push origin myfeature


Now everyone involved in the feature can commit as they wish.

Cleaning up

When you're ready to merge back to master:
git checkout master
git merge myfeature


Then delete your old branches once everything is working:
git branch -d myfeature # deletes your local branch
git push origin :myfeature # deletes the remote branch


Gotcha:
If you push using git push, it will push every thing from every branches, including master. Make sure all your branches are in a stable state when pushing. Otherwise you can push a single branch:
git push origin myfeature

I you feel I missed anything, don't hesitate to tell me.

No comments:

Post a Comment