Last active
July 26, 2020 06:22
-
-
Save mortenpi/dd623aa71536f2255b4303707b5c494b to your computer and use it in GitHub Desktop.
A bash function to quickly add a GitHub fork as a remote to your git repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# git-remote-github-add: A bash function to quickly add a GitHub fork as a remote to your | |
# git repository. | |
# | |
# Copyright 2020 Morten Piibeleht | |
# | |
# License: MIT | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
# software and associated documentation files (the "Software"), to deal in the Software | |
# without restriction, including without limitation the rights to use, copy, modify, merge, | |
# publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons | |
# to whom the Software is furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all copies or | |
# substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | |
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | |
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
# DEALINGS IN THE SOFTWARE. | |
# git-remote-github-add USER | |
# | |
# It will look at the 'origin' remote of the current repository and adds another remote | |
# called USER where the user/org portion of the GitHub URL is replaced by user. | |
# | |
# INSTALLATION: just add this function into your .bashrc / .bash_aliases or .profile file. | |
function git-remote-github-add { | |
user="$1" | |
origin=`git remote get-url origin` | |
if ! [ $? == 0 ]; then | |
2>&1 echo "git-remote-github-add: Failed to run git. Not a repo?" | |
return 1 | |
fi | |
echo "origin='$origin'" | |
if [[ "$origin" =~ ^(https://|git@)github\.com[:/]([A-Za-z0-9]+)/([A-Za-z0-9\._-]+)$ ]]; then | |
origin_user="${BASH_REMATCH[2]}" | |
repo="${BASH_REMATCH[3]}" | |
else | |
2>&1 echo "git-remote-github-add: Unable to match for repo in origin=$origin" | |
return 2 | |
fi | |
echo ">>" $user $repo | |
echo $origin | |
cmd="git remote add $user [email protected]:${user}/${repo}" | |
echo "Calling: $cmd" | |
$cmd | |
if ! [ $? == 0 ]; then | |
2>&1 echo "git-remote-github-add: Failed to run git remote add" | |
return 1 | |
fi | |
git remote -v | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment