My colleague1 Veronika Kabátová wrote a great piece on setting up your environment to work with remote repositories. In it she outlines the strategy of letting your main branch point at the main code repository and have your other branches in your fork.
In her article, she describes how to edit your
.git/config file to achieve this. I wanted to share some ways to do this without having to hand edit files. These commands assume you’re starting from the beginning and haven’t cloned or forked the repository yet.
1. Clone the Repository
git clone -o upstream <repository url>
The key part of this command is the extra option,
-o upstream. This clones the repository but names the remote upstream. Now you can do things like
git pull upstream to keep your main branch in sync.
2. Fork the Repo
Do whatever the git forge your using requires to create your fork. This may mean visiting a website and clicking a button. No matter what the process is, you should end up with a URL for your cloned repository. This is typically an SSH style URL.
cd into the clone you made above and add your fork as a remote. We will call it origin in keeping with Veronika’s advice and what is typical practice.
git remote add origin <repository url>
This works for any git forge out there. If you’re using GitHub, and have installed their https://cli.github.com/, you can simplify the commands above with:
gh repo clone <repository path> -- -o upstream cd <repository> gh repo fork --remote
Notice that we are passing the
-o upstream git option when we do the clone. The
gh repo fork command automatically creates the fork for you and with the
--remote option automatically adds it named as
Good luck and happy
I love when I read something cool online and discover it is by a fellow Red Hatter I haven’t yet met in person! ↩