147

Suppose you have a repository at github.com/someone/repo and you fork it to github.com/you/repo. You want to use your fork instead of the main repo, so you do a

go get github.com/you/repo

Now all the import paths in this repo will be "broken", meaning, if there are multiple packages in the repository that reference each other via absolute URLs, they will reference the source, not the fork.

Is there a better way as cloning it manually into the right path?

git clone [email protected]:you/repo.git $GOPATH/src/github.com/someone/repo
9
  • 1
    No import path in the new fork will be broken which were not broken already before the forking.
    – zzzz
    Commented Jan 14, 2013 at 19:23
  • 15
    Sorry to disappoint you, but that's not true. If a sub-package is referenced in the imports via it's absolute url, this import will be broken in the fork (or at least reference the wrong package).
    – user187676
    Commented Jan 14, 2013 at 20:49
  • 3
    E.g. goamz. It has internal references all over the place.
    – user187676
    Commented Jan 14, 2013 at 21:00
  • 2
    Look at the ec2 package - it has an launchpad.net/goamz/aws import. Both, the aws and the ec2 packages reside in the SAME repository, so when forked, will not reference the correct package (the one in the fork).
    – user187676
    Commented Jan 14, 2013 at 21:33
  • 2
    Go dependency management is straight-up BS. Use go modules then some dependency becomes incompatible, don't use go modules you're forced to work in $GOPATH/src/github.com/blah/blahblah instead of where you want the project to be. Commented May 8, 2020 at 8:04

13 Answers 13

182

If you are using go modules. You could use replace directive

The replace directive allows you to supply another import path that might be another module located in VCS (GitHub or elsewhere), or on your local filesystem with a relative or absolute file path. The new import path from the replace directive is used without needing to update the import paths in the actual source code.

So you could do below in your go.mod file

module some-project

go 1.12

require (
    github.com/someone/repo v1.20.0
)

replace github.com/someone/repo => github.com/you/repo v3.2.1

where v3.2.1 is tag on your repo. Also can be done through CLI

go mod edit -replace="github.com/someone/[email protected]=github.com/you/[email protected]"
8
  • 10
    worked great. i think the only reason this doesn't have more upvotes is because folks are not using go modules yet. I also used this trick to point to a file location to another directory on my workstation where i had local edits i was working on. I would just remove my "replace" line once i push my local edits in github.
    – lazieburd
    Commented Aug 30, 2019 at 13:45
  • 4
    oh, but "master" did not work for me. I had to write v0.0.1 or some specific version there. Commented Oct 27, 2019 at 6:31
  • 2
    You can also go mod edit -replace directly on the command line: go mod edit -replace="github.com/someone/[email protected]=github.com/you/[email protected]". Both @v... are optional.
    – Joel Purra
    Commented Nov 23, 2019 at 12:17
  • 2
    Wouldn't it be cool to have a go.mod.local or go.mod.dev whose role is to actually replace the import path for local development? I mean, you would never forget to remove the ugly "replace" because you wouldn't have to.
    – Manuel
    Commented Jul 8, 2020 at 17:59
  • 1
    There is open issue on golang since 2018 github.com/golang/go/issues/26904 problem seems to be from transitive dependencies
    – Yogesh
    Commented Nov 16, 2022 at 9:17
85

To handle pull requests

  • fork a repository github.com/someone/repo to github.com/you/repo
  • download original code: go get github.com/someone/repo
  • be there: cd "$(go env GOPATH)/src"/github.com/someone/repo
  • enable uploading to your fork: git remote add myfork https://github.com/you/repo.git
  • upload your changes to your repo: git push myfork

http://blog.campoy.cat/2014/03/github-and-go-forking-pull-requests-and.html

To use a package in your project

https://github.com/golang/go/wiki/PackageManagementTools

3
  • from which folder I should do git remote add? clone from fork? clone from original? from within go?
    – lapots
    Commented Mar 15, 2018 at 17:38
  • 1
    @lapots run the command in the original repo (i.e. $GOPATH/src/github.com/somone/repo)
    – will7200
    Commented Apr 21, 2018 at 22:29
  • What if I want to add changes to a repo which was forked long ago?
    – N A
    Commented Sep 3, 2018 at 5:05
21

One way to solve it is that suggested by Ivan Rave and http://blog.campoy.cat/2014/03/github-and-go-forking-pull-requests-and.html -- the way of forking.

Another one is to workaround the golang behavior. When you go get, golang lays out your directories under same name as in the repository URI, and this is where the trouble begins.

If, instead, you issue your own git clone, you can clone your repository onto your filesystem on a path named after the original repository.

Assuming original repository is in github.com/awsome-org/tool and you fork it onto github.com/awesome-you/tool, you can:

cd $GOPATH
mkdir -p {src,bin,pkg}
mkdir -p src/github.com/awesome-org/
cd src/github.com/awesome-org/
git clone [email protected]:awesome-you/tool.git # OR: git clone https://github.com/awesome-you/tool.git
cd tool/
go get ./...

golang is perfectly happy to continue with this repository and doesn't actually care some upper directory has the name awesome-org while the git remote is awesome-you. All import for awesome-org are resovled via the directory you have just created, which is your local working set.

In more length, please see my blog post: Forking Golang repositories on GitHub and managing the import path

edit: fixed directory path

2
  • 5
    I agree this is the "best" solution for this. But it would be really nice to see how people manage this workflow when running the Go app in a Docker container. I am learning golang and wanted to add a tiny feature to a library I am using when I ran into this headache with testing it before creating a Pull Request.
    – Joakim
    Commented Jul 18, 2016 at 16:20
  • 7 years later, still no good solution for Docker container that I can find online. Commented Aug 4, 2023 at 10:40
4

If your fork is only temporary (ie you intend that it be merged) then just do your development in situ, eg in $GOPATH/src/launchpad.net/goamz.

You then use the features of the version control system (eg git remote) to make the upstream repository your repository rather than the original one.

It makes it harder for other people to use your repository with go get but much easier for it to be integrated upstream.

In fact I have a repository for goamz at lp:~nick-craig-wood/goamz/goamz which I develop for in exactly that way. Maybe the author will merge it one day!

2
  • 1
    Just so I understand the implications of doing this, if I went this route, when someone does a go get from my repo, all of my import statements and such will still reflect github.com/original_author and thus be broken... correct? Commented Oct 4, 2014 at 20:30
  • @parker.sikand yes that is correct. This technique is best for stuff you intend to get merged upstream, not for go get use. If you intend to fork the package permanently then use the other answer's technique. Commented Oct 5, 2014 at 14:30
3

Here's a way to that works for everyone:

Use github to fork to "my/repo" (just an example):

go get github.com/my/repo
cd ~/go/src/github.com/my/repo
git branch enhancement
rm -rf .
go get github.com/golang/tools/cmd/gomvpkg/…
gomvpkg <<oldrepo>> ~/go/src/github.com/my/repo
git commit

Repeat each time when you make the code better:

git commit
git checkout enhancement
git cherry-pick <<commit_id>>
git checkout master

Why? This lets you have your repo that any go get works with. It also lets you maintain & enhance a branch that's good for a pull request. It doesn't bloat git with "vendor", it preserves history, and build tools can make sense of it.

2
3

Instead of cloning to a specific location, you can clone wherever you want. Then, you can run a command like this, to have Go refer to the local version:

go mod edit -replace github.com/owner/repo=../repo

https://golang.org/cmd/go#hdr-Module_maintenance

1

The answer to this is that if you fork a repo with multiple packages you will need to rename all the relevant import paths. This is largely a good thing since you've forked all of those packages and the import paths should reflect this.

8
  • 3
    I burned more time than I care to admit diagnosing this in my first contribution to a Go project. "All tests pass, including the ones I wrote to exhaustively test new functionality. What's wrong?!" Are you aware of any available tooling to ease this stumbling point for beginners? Commented May 30, 2014 at 16:37
  • 3
    Once I figured it out it was easy to solve using find, xargs, and sed, but it would help to have a pain-free workflow that consistently works for everyone. Commented May 30, 2014 at 16:39
  • @JakeMitchell gomvpkg can do the renames easier/better. go get golang.org/x/tools/cmd/gomvpkg then gomvpkg -help.
    – Dave C
    Commented Mar 20, 2015 at 17:06
  • 3
    This answer strikes me as complety impractical. Sed-ing project files out of a forked project, that's insane? What do you do when you create a pull request? The answer by Ivan Rave looks like a much better solution to me.
    – Ivan P
    Commented Mar 27, 2015 at 5:58
  • 8
    Is this still how Go-lang is working? This is just so insane, that it is not funny... Either be upstream-friendly, or downstream-friendly, but not both. It is a huge design flaw in my not so humble opinion, probably done by people who doesn't collaborate too much cross-projects. #FAIL #GOLANG Commented Jul 15, 2016 at 12:41
1

Use vendoring and submodules together

  1. Fork the lib on github (go-mssqldb in this case)
  2. Add a submodule which clones your fork into your vendor folder but has the path of the upstream repo
  3. Update your import statements in your source code to point to the vendor folder, (not including the vendor/ prefix). E.g. vendor/bob/lib => import "bob/lib"

E.g.

cd ~/go/src/github.com/myproj

mygithubuser=timabell
upstreamgithubuser=denisenkom
librepo=go-mssqldb

git submodule add "[email protected]:$mygithubuser/$librepo" "vendor/$upstreamgithubuser/$librepo"

Why

This solves all the problems I've heard about and come across while trying to figure this out myself.

  • Internal package refs in the lib now work because the path is unchanged from upstream
  • A fresh checkout of your project works because the submodule system gets it from your fork at the right commit but in the upstream folder path
  • You don't have to know to manually hack the paths or mess with the go tooling.

More info

1

Since Go 1.18, you can use Go Workspace for this use case.

Run the command

$ go work init path-to-your-fork path-to-your-app-module

or directly edit

go.work

go 1.18

use (
       ./path-to-your-fork
       ./path-to-your-app-module
)

The paths here refer to the location of the local copy in your computer.

0

The modern answer (go 1.15 and higher, at least).

go mod init github.com/theirs/repo

Make an explicit init arg that is the ORIGINAL package names. If you don't include the repo name, it will assume the one in gopath. But when you use go modules, they no longer care where they are on disk, or where git actually pulls dependencies from.

-1

To automate this process, I wrote a small script. You can find more details on my blog to add a command like "gofork" to your bash.

function gofork() {
  if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
    echo 'Usage: gofork yourFork originalModule'
    echo 'Example: gofork github.com/YourName/go-contrib github.com/heirko/go-contrib'
    return
  fi
   echo "Go get fork $1 and replace $2 in GOPATH: $GOPATH"
   go get $1
   go get $2
   currentDir=$PWD
   cd $GOPATH/src/$1
   remote1=$(git config --get remote.origin.url)
   cd $GOPATH/src/$2
   remote2=$(git config --get remote.origin.url)
   cd $currentDir
   rm -rf $GOPATH/src/$2
   mv $GOPATH/src/$1 $GOPATH/src/$2
   cd $GOPATH/src/$2
   git remote add their $remote2
   echo Now in $GOPATH/src/$2 origin remote is $remote1
   echo And in $GOPATH/src/$2 their remote is $remote2
   cd $currentDir
}

export -f gofork
3
  • Should golang be changed to gofork in line 4? Commented Dec 7, 2017 at 4:12
  • well seen! fix!
    – heralight
    Commented Dec 8, 2017 at 8:58
  • This worked years ago now dude, but not now. Commented Dec 3, 2021 at 23:10
-2

You can use command go get -f to get you a forked repo

1
-5

in your Gopkg.toml file add these block below

[[constraint]]
  name = "github.com/globalsign/mgo"
  branch = "master"
  source = "github.com/myfork/project2"

So it will use the forked project2 in place of github.com/globalsign/mgo

2
  • 1
    The Gopkg.toml file is only used by dep which this question doesn't mention at all. New Go projects should be using Go modules instead (and IMO existing dep based projects should migrate as well).
    – Dave C
    Commented Aug 21, 2019 at 16:54
  • 1
    I didn't know about this dep feature, and your answer surely helped me out :)
    – Veger
    Commented Oct 25, 2019 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.