Move a git repository to another location.

You might think that when moving a git repository from one location to another, all you have to do is to clone and then push to the new location. That is not true. You have to make sure that all remote branches and tags are included. Note that the following instructions will also push all your local branches to.

The following steps will move a repository called my_repo from Google Code to Github. Of course you can use the same steps to move from and to which ever git hosting service you like.

Step by step instructions
  1. Clone the the repository
    git clone https://code.google.com/p/my_repo
    cd my_repo
    
  2. List all local and remote branches. Note: All branches will be pushed later, both local and remote.
    git branch -a
    
  3. Track all remote branches.
    for remote in `git branch -r | grep -v '\->'`; do git branch --track ${remote#*/} $remote; done
    
  4. Add the new remote repository.
    git remote add new-origin git@github.com:user/my_repo.git
    
  5. List all remote repositories.

    git remote -v
    
  6. Push all branches to the new remote repository location.
    git push -u --all new-origin
    
  7. Push all tags to the new remote repository location.
    git push --tags new-origin
    
  8. Remove or rename the old origin
    git remote rm origin  # or rename ‘git remote rename origin old-origin'
    
  9. Rename the new-origin.
    git remote rename new-origin origin
    
  10. List all remote repositories.

    git remote -v
    
Thats it!

Regex and negative lookbehind

In my current project our application is built with Spring and we are using a couple of Spring profiles. Unfortunately we didn’t make them unique so if we deploy it with another application in the same application server we might get a name clash. We decided that we better try to use names that are specific for our application. So I started renaming all of our spring profiles by just adding the prefix apigw. to the name so it will be more unique.

auth-http-basic => apigw.auth-http-basic
auth-x509 => apigw.auth-x509

It was pretty easy to search for all occurrences and just add the prefix, totally 52 files and 92 changes. But what if I have missed any change that should have been done? It must be some way to use a regex to search for the string ‘auth-http-basic’ that is not preceded by ‘apigw.’. After a quick search with Google I found the answer; Negative Lookbehind.

“(?<!a)b matches a “b” that is not preceded by an “a”, using negative lookbehind. It doesn’t match cab, but matches the b (and only the b) in bed or debt.(?<=a)b (positive lookbehind) matches the b (and only the b) in cab, but does not match bed or debt.”

This is how the regex looks for me, (?<!apigw.)(auth-http-basic|auth-x509). Just fire up SublimeText and use Find in folder..