Git alias and hook to automatically add upstream remote using auto-detected URL when cloning your public or private forks.
git clone https://github.com/thebengeu/auto-git-remote-add-upstream.git sh auto-git-remote-add-upstream/setup-git-alias-and-hook.shIf the upstream remote is not set and there are commits where you aren't the author and the origin remote URL seems to be a GitHub repo under your username, git rau will attempt to add the upstream remote using the auto-detected URL of:
- The upstream repo that
originwas forked from, if explicitly set on GitHub. - The earliest-created repo on GitHub that contains the latest local commit not authored by you.
- This heuristic should be good enough for most cases, whether your fork is public or private.
- If that latest local commit isn't recent, in rare cases another fork containing that commit, that isn't explicitly set as a fork on GitHub, may have overtaken the earliest-created repo in some measures. Any such repos created later but with more stars, forks, open issues or watchers will be listed for your consideration.
The setup script adds git rau to the post-checkout Git hook within the template directory set in the init.templateDir Git configuration variable, which will be set to $XDG_CONFIG_HOME/git/template if not already set.
- Run
setup-git-alias-and-hook.sh <alias-name>to use an alias name other thanrau. - Set the
GH_TOKENorGITHUB_TOKENenvironment variables to a GitHub personal access token to:- Increase the GitHub API rate limit from 60 to 5000 requests per hour.
- Handle cases where the upstream repo is private.
- Set the
GH_USERNAMEenvironment variable to your GitHub username if your GitHub username can't be auto-detected from theuser.emailGit configuration variable.
Currently, most cases should be covered. There may be some edge cases that haven't been addressed to avoid over-complicating the initial versions. Please create an issue for any real-world unhandled cases.