Skip to content

Instantly share code, notes, and snippets.

@epicserve
Created May 17, 2012 18:27
Show Gist options
  • Select an option

  • Save epicserve/2720771 to your computer and use it in GitHub Desktop.

Select an option

Save epicserve/2720771 to your computer and use it in GitHub Desktop.
How to change your remote origin URL for all your Git repositories

How to change your remote origin URL for all your Git repositories

  1. First cd to the directory where all of your sites are located

     $ cd /srv/sites
    
  2. Change all previous urls from url = git@old-server.example.com:foo.git to url = git@cod.example.com:foo.git

     $ cwd=`pwd`; for d in *; do if [[ -e "$cwd/$d/.git/config" ]]; then echo "$cwd/$d/.git/config" | xargs perl -i.bak -wpe 's|(url = git\@old-server(\.example\.com)?):([-_a-z]+)\.git|url = git\@new-server.example.com:$3.git|g'; fi; done
    
  3. Check to see if the change worked

     $ cwd=`pwd`; for d in *; do if [[ -e "$cwd/$d/.git/config" ]]; then cat "$cwd/$d/.git/config" | grep url; fi; done
    
  4. If the change worked then you can remove the backup files

     $ cwd=`pwd`; for d in *; do if [[ -e "$cwd/$d/.git/config.bak" ]]; then rm "$cwd/$d/.git/config.bak"; fi; done
    
  5. If the change didn't work you can run the following to restore the original config. After restoring the original config go back to step 2 and change the perl search and replace so it works.

     $ cwd=`pwd`; for d in *; do if [[ -e "$cwd/$d/.git/config.bak" ]]; then cp "$cwd/$d/.git/config.bak" "$cwd/$d/.git/config" | grep url; fi; done
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment