You can easily accomplish your purpose by using the concept of multiple remotes.
Quick Answer
Within your repository execute the following sequence of commands:
git remote add upstream https://github.com/user_or_org_of_upstream_repo/upstream_repo
git fetch upstream
git checkout upstream/main
git checkout -b name_of_branch_you_want_to_track_in_your_repo
Make, add, and commit your edits, then push the changes to your branch back into your GitHub fork with:
git push --set-upstream origin name_of_branch_you_want_to_track_in_your_repo
The branch, completely disconnected from the other branches in your repo (other than any source tree parentage as far up the line as the most recent common ancestor), will now be available to use for a pull request in GitHub.
Voila!
Explanation
git remote add upstream https://github.com/user_or_org_of_upstream_repo/upstream_repo
adds another remote repository and labels it upstream
. The label could be anything -- upstream
is more or less the accepted standard name for the parent of your fork, but it could be 'bob', 'mary', 'dirt', or anything else. Also, the remote can be any repository at all, though I've only ever experimented with fetching from a repository that is at least a parallel fork of the same upstream remote.
git fetch upstream
Does the work of actually getting the main and other branches of the new repository and cloning them into your local. They are just sitting there now, waiting for you to do something with them.
git checkout upstream/main
Checks out the main branch of the repository you have labelled in your remotes list as 'upstream'. Of course, you could check out any other branch in the repository.
git checkout -b name_of_branch_you_want_to_track_in_your_repo
Creates a local branch where your changes can be tracked.
git push --set-upstream origin name_of_branch_you_want_to_track_in_your_repo
pushes your new branch back to the default remote, origin
. Again, you could push the branch to any other remote for which you have write permissions.
More Explanation:
When you fork and clone your repository, the default remote repository is your fork and the label for the fork is 'origin'. The git remote
command shows this -- for example this is the output of that command for a fun little repo I made to [unsuccessfully] master the online game 'Wordle'.
> git remote -v
origin https://github.com/jameshalgren/wordle_analyst_ideas (fetch)
origin https://github.com/jameshalgren/wordle_analyst_ideas (push)
As you work with your fork, the branches in your local repository will likely be based on the main branch within the origin repository. When you add/commit/push changes to your branches, the remote version of those same branches will be updated in your fork on GitHub.
That's why you see this message the first time you try to push a new branch:
fatal: The current branch test_new_branch has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin test_new_branch
Notice the label 'origin' in the instructions. This is a hint to the power of remotes. You can actually push to any other remote repository, as long as you have appropriate permissions. In the first command, git remote
, you can also see the origin
label. If you forked the repository in the example above, added remote repositories for your upstream and possibly several other collaborators, the git remote
command would start to look like this:
> git remote -v
origin https://github.com/youruser/wordle_analyst_ideas (fetch)
origin https://github.com/youruser/wordle_analyst_ideas (push)
upstream https://github.com/jameshalgren/wordle_analyst_ideas (fetch)
upstream https://github.com/jameshalgren/wordle_analyst_ideas (push)
collaborator https://github.com/collaboratoruser/wordle_analyst_ideas (fetch)
collaborator https://github.com/collaboratoruser/wordle_analyst_ideas (push)