LoginSignup
11
13

More than 5 years have passed since last update.

会社のマシンで、社用とプライベートのgitアカウントを使い分けたい

Last updated at Posted at 2016-11-08

やりたいこと

プライベートで使ってるgitのリモートリポジトリ(github)に、自分のアカウントでコミット・プッシュしたい
(会社の名前を一切出さないようにする)

やること(会社のマシンで設定)

  1. プライベート用のssh鍵を作成し、リモートリポジトリに登録
  2. ~/.ssh/config にhost設定を追記

上記設定後、必要なリポジトリで個別に設定を行う

1. プライベート用のssh鍵を作成し、リモートリポジトリに登録

プライベート専用のssh鍵を作成

// sshディレクトリに移動
$cd ~/.ssh

// ssh鍵生成
$ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/(username)/.ssh/id_rsa):

まずファイル名を聞かれるので、プライベート用とわかるファイル名を入力

./id_private

パスフレーズを2回聞かれるので同じものを入力

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

リモートリポジトリに登録

$ pbcopy ~/.ssh/id_private.pub

使用するサービスに上記を登録。

2. ~/.ssh/config にhost設定する

~/.ssh/configに(なければ作成)下記を追加

Host github.private
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_private
  TCPKeepAlive yes
  IdentitiesOnly yes

各リポジトリ個別での設定(リポジトリを作成 or git clone するたびに設定する)

リモートリポジトリの設定

2.で設定したhost名を使用してgit clone(もしくはgit remote add)する

$ git clone github.private:account/sample.git

// いつもはこう
// $ git clone git@github.com:account/sample.git

個別のuser設定

接続はプライベートアカウントでできるようになったけど、コミットする際にglobal設定が適用されるので、コミットログにうっかり会社のメールアドレスが出てしまいヤバい。

該当リポジトリ配下でlocalのgit設定を行う。

$ git config --local user.name "user name"
$ git config --local user.email "username@example.com"

以上で、設定したリポジトリの操作は全てプライベートで行ってるように見えるはず。

会社では仕事を優先しましょうね!

11
13
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
11
13