LoginSignup
7
6

More than 5 years have passed since last update.

ghqでGithubの複数アカウントに対応する例

Last updated at Posted at 2016-04-27

会社で使っているGithubのアカウントと個人のGithubのアカウントが別なので、よく間違えてコミットするのを何とかしたい。複数アカウント管理に軽く調べてみると色々方法があるんだけど、URLの書き換えとか、cloneした後に手順が必要だったりしていつも忘れてしまうので、cloneした時に雑にユーザー切り替えてみる。

sshの設定は必要なので適当に設定しておく。下の例では個人のアカウントの方をprivate.github.comにしている。

private-ghq
#!/usr/bin/env ruby

github_host = "private.github.com"

username = "xxxx"
email = "xxxx"

if ARGV[0] == "get"
  args = []
  repo = ""

  ARGV[1..-1].each do |arg|
    repo = arg if arg.match(/^git@github.com/)
    args << arg.gsub(/^git@github.com/, "git@#{github_host}")
  end
  system "ghq", "get", *args
  exit_code = $?.to_i

  unless repo.empty?
    dir = repo.match(/^git@github.com:([^.]*)(\.git)?/)[1]
    ghq_root = `ghq root`.chomp

    Dir.chdir(File.join(ghq_root, "#{github_host}/#{dir}/")) do
      system "git config user.name '#{username}'"
      system "git config user.email #{email}"
    end
  end

  exit exit_code
else
  exec "ghq", *ARGV
end

後はこのファイルをprivate-ghqとか適当な名前で保存する。cloneしてくる際にはこんな感じになる。

$ private-ghq get git@github.com:hogehgoe/fugafuga

ポイントとしてはURLを書き換えなくてもいいし、最初のghqコマンドを変えるだけでcloneしてきた後はユーザーが切り替わっているので自分で特に何もする必要がない所。

今の所これで困っていないので、管理に困ってる人がいればぜひ試してみてください。

7
6
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
7
6