LoginSignup
0
1

More than 3 years have passed since last update.

複数の Git アカウントを使い分ける方法

Last updated at Posted at 2019-12-25

普段から、GitHub だけでなく、Bitbucket や GitLab も併用していて、それぞれのサイトで異なるアカウントを作成していると、複数の Git アカウントを持つことになり、

例えば、新しいプロジェクトをリモートリポジトリに push しようとすると、以下のように失敗してしまうことがあります。

% git push -u origin master
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/user/repository.git/'

原因は、次のコマンドを実行してみれば分かる通り、

% git config user.name

使用するアカウントが、以下のコマンドを実行するか、もしくは Git の設定ファイル( ~/.gitconfig )を見れば確認できる、Git の「 グローバル設定 」から参照されているためです。

% git config --global user.name

もちろん、使おうとしているのが、メインで使用するアカウントであれば、この「 グローバル設定 」を上書きしておけばいいのですが、

そうでないなら、各リポジトリに固有の「 ローカル設定 」に、user.name を追加する方がいいでしょう。

追加の仕方は簡単で、例えば「 user 」という user.name を設定したい場合には、次のようなコマンドを実行します。

% git config user.name user

確認のコマンドに、引数を一つ増やすだけですね。

ちなみに –global に対応する –local というオプションは( デフォルトが –local になっているので )指定してもしなくても構いません。

これで、該当リポジトリの設定ファイル( .git/config )に user.name が追加されました。

% cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
[remote "origin"]
        url = https://github.com/user/repository.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[user]
        name = user
[branch "master"]
        remote = origin
        merge = refs/heads/master

必要に応じて、user.email も追加しておいてくださいね。

その他、詳細については、公式ドキュメントを参照してください。

0
1
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
1