Thursday, April 4, 2019

Working with forks and go sub modules


Just this week I needed to create a fork for an open source go package that uses go modules: https://github.com/alicebob/miniredis  

No problem I thought.  I created my fork and cloned my forked repo locally.    I created a directory in the source tree for my new submodule and then I initialized my new go module:

go mod init

My new go.mod for the submodule contained the following:


module github.com/Jim-Lambert-Bose/miniredis/sentinel

Well, that's not going to work now is it?  How will I merge things upstream to the parent repo when my PR includes a module with a FDQN that references my github account?

What to do next?  I have my fork and the parent repo... how do I get them to appear to be the same repo to the go module tooling?  Well, it's actually not that difficult.  I just need to clone the parent repo and add a new remote to this clone which points to my forked repo.

git clone git@github.com:alicebob/miniredis.git
cd miniredis
git remote add forked git@github.com:Jim-Lambert-Bose/miniredis
git branch -u forked/master

Now, I can just create a new branch for my changes and start making commits to my forked repo.

git branch -b amazing-pr-branch

# make some local edits/commits, then push to my forked repo
git push -u forked amazing-pr-branch

When I want to revert back to tracking the origin, because my PR has been merged (yes!), I can just enter:

git branch -u origin/master
git reset --hard origin/master

Using this process, I can easily fork and change any Go open source package.  Please feel free to comment below, if you find mistakes or issues.

No comments:

Post a Comment