Home > Mobile >  How to import a specific branch of forked repo into Go code
How to import a specific branch of forked repo into Go code

Time:03-31

I faced with the following problem:

  • I forked a repo, and made some modifications to it under new branch
  • I create PR to upstream repo and it is not yet merged
  • I want to adjust my Go codebase to import the specific branch of my forked repo

The problem:

  • if the original repo is github.com/user/pkg/v3 then the forked repo appears as github.com/myusername/pkg
  • moreover, I made a new branch, e.g. mybranch where I made my fixes
  • in the code where I used original repo I have an entry in my go.mod file as github.com/user/pkg/v3 which I want to replace with specific branch of my forked repo

How should I correctly solve this issue?

What I see when I tried to change go.mod of my forked repo to be github.com/myusername/pkg/v3 and then call go get github.com/myusername/pkg@mybranch is the following

go: github.com/myusername/pkg/v3@vxx-xx-xx: parsing go.mod:
    module declares its path as: github.com/user/pkg/v3
            but was required as: github.com/myusername/pkg/v3

CodePudding user response:

I found required solution. The trick was to perform the following series of steps:

  • fork original repo
  • create new branch
  • add modifications to the code
  • push branch into forked repo
  • tag this branch with version higher then original repo, e.g. if original repo had v3.1.1 then the tag I applied to my forked branch was v3.1.2
  • go to code which depends on this package
  • change go.mod file of my package to use replace directive and my new tag like this
replace github.com/user/pkg/v3 => github.com/myusername/pkg/v3 v3.1.2

Therefore, in order to use forked repo with new branch we must tag this branch in forked repo with version higher of the tag in upstream repo.

  • Related