Three-way Code Management

alt text

# Assuming the current directory is <your-repo-name>
git remote add upstream https://github.com/alshedivat/al-folio.git
git fetch upstream

# check all branches
git branch -avv

git checkout main # local main tracking personal repo
git rebase upstream/main # rebase from upstream
git push -f # push to personal repo

Manually Add Upstream Branch

We can also manually do the changes. This is the original .git/config

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = https://github.com/yyy/zzz
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

Making changes like this:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = https://github.com/xxx/zzz
    fetch = +refs/heads/*:refs/remotes/origin/*
[remote "upstream"]
    url = https://github.com/yyy/zzz
    fetch = +refs/heads/*:refs/remotes/upstream/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

Sync New Commits from Public Repo

The best approach is:

git fetch upstream # this make sure we don't merge

# rebase if you want to rebase
git rebase upstream/main

# merge if you want to merge
git merge upstream/main

# It's not recommended to do git pull as the following. Because git pull will do two things
# - fetched and merged upstream/<branch> into your current branch
# - creates a merge commit
git pull upstream

If we haven’t make changes at master, we can do:

git checkout master
git pull upstream master  # pull public (upstream) to local branch
git push origin master

If there is conflict with your changes:

git checkout master
git fetch upstream
git merge --no-ff upstream/master
git push origin master

Or simply

git checkout main
git rebase upstream/main

Check Out an Upstream PR

# fetch the PR into a temporary or uniquely named branch like this:
git fetch upstream pull/1234/head:pr-1234
git checkout pr-1234