Monday, June 14, 2010

Git Branch from Branch/Remote Branch and Making the New Branch Tracking Remote

It is an easy enough concept: You have a remote branch you created and you want to branch off of it into a new branch that is also remote and tracking. However, it was hard to find an example of this anywhere that contained all of the steps involved, so here they are. Thanks to Paul D. for his help and to Richard for support with this.

Let's assume you already have a remote version branch and that your remote repo is "origin". Creating a branch off of master remotely and tracking it is described elsewhere, and I have a gbranch function in my git-scripts project for that purpose. But what if you want to branch off of that remote branch?

Ok, DON'T CHANGE THIS --no-track to -t/--track thinking you are smart. The goal here is to create a local copy of the remote 4.0 branch, not to create a local copy of it and track the remote existing branch. We'll add correct tracking in a minute!

git co --no-track -b new_branch_name origin/existing_remote_branch_to_branch_from
Now push that new branch to the remote repository:
git push origin new_branch_name
Finally, add tracking to the new_branch. If you have git 1.7.0 or later (do a git --version) then you can do this:
git branch --set-upstream new_branch_name origin/new_branch_name
If you don't have git 1.7.0 or later, you can edit .git/config and add something like:
[branch "new_branch_name"]
        remote = origin
        merge = refs/heads/new_branch_name

No comments: