ryer.io

Git Workflow: Adding a Remote for Forked Branches

TL;DR

  • I don’t often work with forked branches off the GitLab repository, so I forget the process and need to remember it.
  • Use git remote add with a name (ideally matching the repo root) plus the full namespace URL to add the fork as a remote.
  • Example: adding a fork under namespace rfisher5/gitlab with git remote add rfisher5 git@gitlab.com:rfisher5/gitlab.git.
  • Use git checkout -b to create a local branch since it might not exist yet, and –track with remote/branch to track the remote branch.

Remembering the Process

I don’t often interact with forked branches off of the GitLab repository, so I forget how to switch to them. I’ve done this several times before with an automated script handling it for me, so now I need to remember the manual process.

Adding the Remote

Whether I’m in the GitLab UI or working with any GitHub or Git repository, the first step is git remote add. I give it a name—whatever I want, but ideally the name of the repository’s root. In this particular case, I’m adding a fork of the GitLab repository under a user’s namespace, rfisher5/gitlab. So I’d run git remote add rfisher5, followed by git@gitlab.com:rfisher5/gitlab.git. That gives me the full namespace path from the GitLab root and adds it as a remote, giving me a reference to all of its branches.

Checking Out and Tracking the Branch

From there, I can run git checkout -b to create the branch, since it might not already exist locally, specifying the branch name within that repository. One additional nice touch is adding –track, followed by the remote name and branch name, which sets up tracking between that remote branch and the local one I just created.