DEV Community

penthaapatel
penthaapatel

Posted on • Updated on

Contributing to Open Source Go projects on GitHub - A recipe to clone forked Go repos.

Phew! It took me a while to figure out the entire process so I decided to write this post and I hope it'll further help everyone else get started. I managed to gather all the ingredients and curate the recipe to start working on an open source Golang project…so follow along.

Say you want to experiment with org's new tool which is written in Go. So, you go to get (pun intended) the repo which you have forked on your GitHub account.

Question : Why your regular go get on the forked repo won't work?
Simple Answer : All your imports will be messed up. You'll have to change all the import statements that say something like github.com/org/tool to github.com/yourGitHubID/tool to make things work and your code will run into conflicts.

go get the original org's repo. go get clones the repo into your GOPATH . -u flag updates the named packages and their dependencies.

$ go get -u github.com/org/tool
Enter fullscreen mode Exit fullscreen mode

Go to the directory where you just cloned the org's tool repo

$ cd $GOPATH/src/github.com/org/tool
Enter fullscreen mode Exit fullscreen mode

Let's check what remote repos we have at this point. List out the remote repositories. -v for a verbose output.

$ git remote -v
origin https://github.com/org/tool (fetch)
origin https://github.com/org/tool (push)
Enter fullscreen mode Exit fullscreen mode

Add a add a new remote Git repository explicitly pointing at your fork. Here the shortname I used for the new remote is fork, you can name it whatever you like.

$ git remote add fork https://github.com/yourGitHubID/tool.git
Enter fullscreen mode Exit fullscreen mode

List out the remote repositories again to check and there you see it, a new remote has been added.

$ git remote -v
fork   https://github.com/yourGitHubID/tool.git (fetch)
fork   https://github.com/yourGitHubID/tool.git (push)
origin https://github.com/org/tool (fetch)
origin https://github.com/org/tool (push)
Enter fullscreen mode Exit fullscreen mode

Now that you have a remote that points to the forked repo on your GitHub account, you can start working. To start working on a new branch:

$ git checkout -b experimentbranch
Enter fullscreen mode Exit fullscreen mode

Make sure you have your branch updated with org's branch. Then you can start making changes to the files on the experimentbranch git add your modified files and git commit your work after testing the new features you've added.

Push changes to your fork.

$ git push -u fork experimentbranch:experimentbranch
Enter fullscreen mode Exit fullscreen mode

Then you can compare the changes you have made and submit a Pull Request to propose changes to be merged with the org's repository.

If you notice any corrections that need to be made in the post feel free to comment below! :)

Top comments (1)

Collapse
 
nanohard profile image
William Antonelli

This assumes go modules aren't used, and I'm not sure that will be possible in Go 1.13 as they want to deprecate GOPATH. I don't see any problem with forking and cloning, but if I'm missing something then please let me know the pitfalls of doing that.

go get -u in module mode will actually update all packages, whether you supply a package URL or not.

There is talk about this tool which may be helpful, although I've never looked into it:
github.com/rogpeppe/gohack