Skip to content

Instantly share code, notes, and snippets.

@SHaTRO
Last active July 14, 2021 07:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SHaTRO/b08287026680bbb29ff7f172655debba to your computer and use it in GitHub Desktop.
Save SHaTRO/b08287026680bbb29ff7f172655debba to your computer and use it in GitHub Desktop.
BASH script to change "original" upstream clone to newly forked repo
#!/bin/bash
## If you cloned a repository, then forked it and want to change your clone to point to your fork...
## Usage:
## git-forked-to <NEW ORIGIN URL> [<BRANCH>]
##
## <NEW ORIGIN URL> should be your new forked repo URL
## <BRANCH> defaults to "master"
##
## For example: git-forked-to git@github.com:SHaTRO/conveyance.git
##
## Thanks to James Gregory (https://gist.github.com/jagregory/710671) for his notes
## In his (jagregory's) words:
## Technically, when you fork "origin" should be your fork and "upstream" should be the project you forked.
##
# So here it is, developed under git v.1.9.1; should work for git >= 1.8.0
ORIGIN=${1}
BRANCH=${2:-master}
echo -n "Renaming 'origin' to 'upstream': " && git remote rename origin upstream && echo "done." && \
echo -n "Adding 'origin': " && git remote add origin ${ORIGIN} && \
git fetch origin && \
( git branch -d origin/${BRANCH} || echo "But that is ok, we will assign it as upstream now" ) && \
git branch --set-upstream-to origin/${BRANCH} ${BRANCH} && \
echo -n "Pulling: " && git pull && \
git status
@SHaTRO
Copy link
Author

SHaTRO commented Jan 28, 2017

Thanks, @jagregory for the notes at "How to move to a fork after cloning"!

@Christopher-Hayes
Copy link

Works like a charm, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment