I recently needed to rename a Github Organization with about 80 repositories. While Github very helpfully creates automated redirects for your repos during the rename process, it is still best practice to go through and update the remote URLs for all of the repos you have cloned on your computer.
This is a pretty simple process for a single repository. You just need to set your
remote URL to use the new Organization name (
NewOrgName in this example).
> git remote set-url origin email@example.com:NewOrgName/my-repo.git
Note: This post assumes that your remote repo is named origin and you cloned via SSH, please adjust accordingly.
Now, if you are like me you probably have something like 50 repos that you'll need to perform this operation on. So instead of doing data entry for an hour, I put together this little bash one-liner that loops through all of my repos and performs the update.
for d in ./*/ ; do (cd "$d" && git remote set-url origin $(git remote get-url origin | sed s/:OldOrgName\\//:NewOrgName\\//) && echo "Remote Updated: $d"); done
A few notes about this script:
- It loops through the direct children of your current directory and uses
sedto replace the old org name with your new org name. This means your directory structure must look something like this for this to work:
- directory/ - repo-1/ - repo-2/ - etc...
- Make sure you update the
NewOrgNamevalues to reflect your actual values.