Working with multiple repositories in GitHub

Basic Terms

origin: origin is your fork: your own repository on GitHub, clone of the original repository on GitHub

Getting started

Start by forking the repository that you want to suggest changes to on GitHub. This will create a fork of the repository in your account. Typically you will only have read-only access to other repositories. Making a fork of a repository will make a copy of it with write access. You will be using this repository to make changes, test them and create PRs to the original fork as well as other forks of the repository.

Making a local copy a.k.a cloning

Next you will make a local copy of your GitHub repository on your computer where you can make changes on it.

git clone <repository url from your fork>

Check your git config

git config -l...user.name=<your name>user.email=<your email>remote.origin.url=<url of your repository>remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*branch.master.remote=originbranch.master.merge=refs/heads/master...

Adding remote repositories

It is a good practice to let your local git know of the original repository so it can keep track of other branches as well as allow you to create PRs with other forks. Run these commands from within the root folder of your local repository

git remote add <local name for repository> <remote repository URL>git remote add lprhodes git://github.com/lprhodes/homebridge-broadlink-rm.git
git remote add ababel git://github.com/AlexanderBabel/homebridge-broadlink-rm.git
git config -l 
git remote -v

Your changes

So you can now start making changes that you are interested in. You can make changes on the last commit or you can move back to a previous commit and make changes to that.

git checkout -b <new_branch_name> <commit_id>
git reset --hard <commit_id_A># Force master branch @origin to update to current local master.
# This deletes all commits newer than commit_id_A from the master
# branch on origin.
git push --force origin master
git branch <new_branch_name>

Commits

You can bundle your changes, “stage” and “commit” them so you can push them to your remote repositories.

# Stage: 
git add <file names OR '.' to add all files>
# Commit:
git commit -m “<commit message”

Finishing changes

Once you have tested your changes and are satisfied with it, you can update your server hosted repository aka origin by running the following command

# Finishing your changes with Option 1 (Direct commit to master)
git push
# OR git push origin master
# Finishing your changes with Option 2 (using a new branch)
git push -u origin <new branch name>
Click on branches to see all branches including the newly pushed branch
Click on pull requests to see the newly created pull requests

Porting changes to the forks

Now that you have pushed your changes to your origin, you may want to apply this change to other forks. If you have used Option 2 then you will have your work on a new branch and you can create pull requests by going to the branches page and clicking on the new pull request button.

Click on new pull request with the newly created branch

Fork is out of sync with the original repository

In this case the changes that you have made based on the original repository might not be directly compatible or merge well with the other fork “Fork B”. When you try to create a PR you will see multiple commits that are far greater than the changes you made due to this.

git checkout -b <new_branch_name> <remote__other_fork_branch>
git checkout <remote_other_fork_branch>git branch <new_branch_name> -u <remote_branch_usually_master>
git cherry-pick <commit_id>
git push -u origin <new_branch_name>

References

  1. Working with Git remotes and pushing to multiple Git repositories
  2. Working With Multiple Repositories On GitHub
  3. Configuring a remote for a fork — GitHub Docs
  4. What does base fork, base, head fork and compare mean when creating a pull request in GitHub?
  5. How to “pull request” a specific commit
  6. Git — Basic Branching and Merging
  7. Resolving a merge conflict using the command line — GitHub Docs
  8. How can I move HEAD back to a previous location? (Detached head) & Undo commits
  9. Git reset origin to commit | Christian Engvall
  10. Using git with multiple remote repositories

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store