Thursday, September 2, 2010

New Upstream Branch into My Fork

‹prev | My Chain | next›

Up today, I think I will have a look-see at what's new in fab.js land. I am working in my own fork of fabjs. There are a couple of tweaks in my fork that I need, but that Jed did not pull in, so my preference is to keep working in my fork, but tracking his changes.

So first up I'll try to remember how to sync up my fork with Jed's so that I can pull the new branch.


OK new plan. I'll look it up on Dunno if this will ever be something that I will just know or if it will always require a lookup. Anyhow...

Following along, I need to add the upstream repos:
cstrom@whitefall:~/repos/fab$ git remote add upstream git://
cstrom@whitefall:~/repos/fab$ git fetch upstream
remote: Counting objects: 66, done.
remote: Compressing objects: 100% (57/57), done.
remote: Total 58 (delta 30), reused 0 (delta 0)
Unpacking objects: 100% (58/58), done.
From git://
* [new branch] master -> upstream/master
* [new branch] v0.5 -> upstream/v0.5
cstrom@whitefall:~/repos/fab$ gba
* master
remotes/origin/HEAD -> origin/master
Next, I need to merge the upstream changes that Jed has made into my local repository:
cstrom@whitefall:~/repos/fab$ git merge upstream/master
Auto-merging apps/fab.nodejs.js
Merge made by recursive. | 2 +-
apps/fab.nodejs.js | 2 +-
package.json | 42 ++++++++++++++----------------------------
3 files changed, 16 insertions(+), 30 deletions(-)
Finally I push those changes to my github fork:
cstrom@whitefall:~/repos/fab$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 7 commits.
nothing added to commit but untracked files present (use "git add" to track)
cstrom@whitefall:~/repos/fab$ git push
Counting objects: 34, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (24/24), done.
Writing objects: 100% (24/24), 2.82 KiB, done.
Total 24 (delta 16), reused 0 (delta 0)
72ec43b..31e4d6b master -> master
Easy enough, except... that only pushes the changes to master to my fork. How do I get the new branch pushed to my fork?

After looking around github a while, I fail to locate the copy-branch-from-a-fork button. My google-fu fails me as well. I suppose I could try to push my way through. It is a bit low-level, but I would guess that my first step ought to be creating a new, local branch. I can then merge in the upstream changes and then push that to my forked copy on github.

Sounds like a plan. Maybe.

Creating the new, local branch:
cstrom@whitefall:~/repos/fab$ git co -b v0.5
Switched to a new branch 'v0.5'
cstrom@whitefall:~/repos/fab$ gba
* v0.5
remotes/origin/HEAD -> origin/master
Now I merge in Jed's upstream changes:
cstrom@whitefall:~/repos/fab$ git merge upstream/v0.5
Removing LICENSE.txt
CONFLICT (content): Merge conflict in
Removing apps/fab.body.js
Removing apps/
Automatic merge failed; fix conflicts and then commit the result.

Indeed, git-status confirms, I have conflicts:
cstrom@whitefall:~/repos/fab$ gst
# On branch v0.5
# Changes to be committed:
# deleted: LICENSE.txt
# deleted: apps/fab.body.js
# deleted: apps/
# Unmerged paths:
# (use "git add/rm ..." as appropriate to mark resolution)
# both modified:
# deleted by them: apps/fab.nodejs.js
Bah! That is not right. This is one of the very few cases that I know I want to git reset --hard. I have myself a merge conflict and I just want to restart. So:
cstrom@whitefall:~/repos/fab$ git reset --hard
HEAD is now at 31e4d6b Merge remote branch 'upstream/master'
Solid. I recovered from that little gaff. But what caused the gaff?

It takes me a bit, but I eventually realize that Jed has done work on master since he branched v0.5. But I made my local branch from HEAD in master. I should have branched at the same point that he did. Fortunately, that is not that difficult. He branched after a618ff9b73832e879161. To do the same, I simply need to supply that as the reference point for my local v0.5 branch:
cstrom@whitefall:~/repos/fab$ git co -b v0.5 a618ff9b73832e879161
Switched to a new branch 'v0.5'
Now, I can safely merge from upstream:
cstrom@whitefall:~/repos/fab$ git merge upstream/v0.5
Updating a618ff9..ebc9ef1
LICENSE.txt | 20 --- | 145 ++++++++++++++++--
apps/fab.body.js | 39 -----
apps/ | 16 --
apps/fab.capture.js | 12 --
delete mode 100644 examples/date.js
delete mode 100644 examples/focus.js
delete mode 100644 examples/hello.js
delete mode 100644 examples/index.js
delete mode 100644 package.json
create mode 100644 server.js
delete mode 100644 utils/build.js
delete mode 100644 utils/test.js
And, finally push my changes to my fork on github:
cstrom@whitefall:~/repos/fab$ gp origin v0.5
Counting objects: 39, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (35/35), done.
Writing objects: 100% (36/36), 9.24 KiB, done.
Total 36 (delta 15), reused 0 (delta 0)
* [new branch] v0.5 -> v0.5
Now I have my own nice, shiny fabjs v0.5 on my fork.

Nice. It was really cool to get through all that. I still feel as though I have a long way to go with git before I really understand it, but I was pleased to be able to work through that without getting hopelessly lost.

The basic flow:
  1. git remote add upstream git:// # insert your git repos here
  2. git fetch upstream
  3. git checkout -b v0.5 a618ff9b73832e879161 # insert branch name and SHA1 of forked-from branch point here
  4. git merge upstream/v0.5 # insert forked-from branch here
  5. gp origin v0.5 # use your branch here
Easy enough.

Still, there has to be a better way of pulling in a new branch from upstream. Right?

Day #214


  1. I still have a long way to go too, but this might help: `git pull` is the same as `git fetch` and then a `git merge`.

  2. Not positive, but I don't think that would work in this case. From

    git pull is a more direct way, but the merge it performs can be confusing if the user doesn’t expect it and a merge conflict results. git fetch will also grab all branches, where git pull grabs only the one specified.

    Personally, I need to keep potential confusion to a minimum. Even if that were not a potential problem, I am specifically trying to get the new upstream branches. If help.github is accurate, then `git pull` won't grab those branches -- just the current branch.

  3. Regarding your the last paragraph of the previous comment Chris, I'm pretty sure if you've checked out other branches and they are set to track their upstream counterparts, a lone `git pull` WILL grab those branches.