Git Forked Repository Operations

From NovaOrdis Knowledge Base
Revision as of 22:46, 1 November 2023 by Ovidiu (talk | contribs) (→‎Overview)
Jump to navigation Jump to search

External

Internal

Overview

For terminology, see upstream/base and head repositories.

The typical GitHub forked repository topology is similar to:

A Typical GitHub Fork Situation.png

Initial Setup

Fork

Go to GitHub UI and click on the "Fork" button at the top of the page. Use your own "personal" organization ("ovidiu") to fork into.

Clone

Clone as usual:

git clone git@github.com:ovidiu/blue.git

Setup Relationships

Setup the "upstream" Repository

Establish a direct relationship between the local clone and the "upstream" repository. This will allow to pull the latest version of branches directly from the upstream repository.

git remote add upstream git@github.com:blue/blue.git

.git/config will contain:

[remote "upstream"]
      url = git@github.com:blue/blue.git
      fetch = +refs/heads/*:refs/remotes/upstream/*

This will allow us to fetch directly from "upstream"

git fetch upstream

Configure the "main" Branch to Update From Upstream

git fetch upstream
git branch --set-upstream-to=upstream/main main

This will configure the local "main" branch to track upstream's "main" branch. The .git/config will look like:

[branch "main"]
     remote = upstream
     merge = refs/heads/main

git pull will automatically apply upstream's "main" branch changes to the local main.

PR Cycle

Send a PR

Push the commit in the head repository.

It will show up in the UI.

Click "Compare & pull request"

The UI will give you the default choice to send the PR against the base repository while "Create pull request". Use it.

Merge the PR

Upon approval, merge the PR in upstream. Usually, the repository is configured to remove the branch from where? What branch is deleted? Probably the origin's. Check.

Sync the Repository after the PR Merge

From the GitHub UI

In the fork repository UI, use "Fetch upstream" button. Then git pull from the local clone.

CLI

Assuming the local repository was set as shown above, git pull will bring the latest "main" changes and will allow us to delete the local feature branch after it was merged in upstream.

git pull


To clean up branches:

How to deal with the leftover branch in the origin repository?


?
git remote prune upstream


How to deal with the origin and upstream growing out of sync?