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


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 then the forked repo appears as
  • 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 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 and then call go get is the following

go: parsing go.mod:
    module declares its path as:
            but was required as:


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 => 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.

Answered By – Valentin

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.