0
0

More than 3 years have passed since last update.

gitのリモートURLを一斉に書き換える

Posted at

BitBucketで大量のGitリポジトリの所有者変更をした際に、手元にcloneしてあるリポジトリのURLも同時に書き換えるためのスクリプトです。git remote set-urlを該当するリポジトリで順番に実行して回ります。(安全のため、そのまま実行してもコマンドを出力するだけなので、実際に実行する場合は書き換えてください。


old_owner = "foo"
new_owner = "bar"

Dir.entries(".").sort.each do |d|
  next unless File.directory?(d+"/.git")
  Dir.chdir(d)
  puts Dir.pwd
  m = `git remote -v | grep fetch | grep #{old_owner}`
  if m.empty?
    Dir.chdir("..")
    next
  end
  new_path = m.split(/[\t ]+/)[1].sub(src,new_owner)
  cmd = "git remote set-url origin "+new_path
  puts cmd
  Dir.chdir("..")
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0