Last active
October 5, 2017 17:16
-
-
Save davidrleonard/19d4e172012f26f25751a0c64f7f8122 to your computer and use it in GitHub Desktop.
Add your Github token to existing remotes
This file contains hidden or 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
## If you have to create a Github access token (e.g. if you enabled two-factor auth for the same time) | |
## and you used https:// formatted git remotes for all your local repos, you won't be able to push | |
## or pull anymore. This allows you to fix all your remotes whenever you need to with a simple | |
## bash alias. | |
## | |
## Put this in your ~/.bash_profile or ~/.zsh_profile, then run `$ fixremote` in a repo directory | |
## to fix it. | |
## | |
## P.S. You should use SSH remotes for your Github repos. But if you're behind a corporate proxy | |
## that messes up SSH to the public internet, you can't... :( | |
function fixremote () { | |
# SET YOUR GITHUB USERNAME/ACCESS TOKEN | |
GITHUB_USERNAME="XXXXXX" | |
GITHUB_ACCESS_TOKEN="XXXXXX" | |
# DON'T TOUCH BELOW | |
OLD_ORIGIN_URL="$(git ls-remote --get-url origin)" | |
if [[ $OLD_ORIGIN_URL == *"https://"* ]]; then | |
OLD_PREFIX="https://github" | |
fi | |
if [[ $OLD_ORIGIN_URL == *"http://"* ]]; then | |
OLD_PREFIX="http://github" | |
fi | |
NEW_PREFIX="https://$GITHUB_USERNAME:$GITHUB_ACCESS_TOKEN@github" | |
NEW_ORIGIN_URL="${OLD_ORIGIN_URL/$OLD_PREFIX/$NEW_PREFIX}" | |
echo $NEW_ORIGIN_URL | |
git remote set-url origin $NEW_ORIGIN_URL | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment