LoginSignup
27
31

More than 1 year has passed since last update.

sourcetreeで複数のgitアカウント管理

Last updated at Posted at 2018-09-30

はじめに

gitアカウントを複数使い分けているときに発生したトラブルの解決法です。
プロジェクトによってGithub, GitBucket, BitBucketなど複数のアカウントを使い分けたい場合にも有効です。

環境

Mac

エラー

ちなみに、うまく設定できていないとpushするときなどに以下のようなエラーが出ます。
"remote: Permission to ユーザー名1/リポジトリ名 denied to ユーザー名2."

構成

以下のようなGitで管理しているプロジェクトの場合を想定しています。

project
├ .git
│  └config

手順

Sourcetreeで設定する場合

以下のように設定してください。
スクリーンショット 2018-09-30 16.52.48.png

.gitのconfigを編集する場合

Sourcetree画面の「Congig ファイルを編集」からも編集できますがうまく反映されないことがあるため、以下はMacのターミナルを使った場合です。

$ vi gitプロジェクト/.git/config

以下のようにプッシュ先を登録してあげます。
pushurl = https://[gitアカウント名]@github.com/User/test.git

ちなみに、以下のようにするとパスワードも設定できます。
pushurl = https://[gitアカウント名]:[パスワード]@github.com/User/test.git

config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/User/test.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    pushurl = https://gitTestUser@github.com/User/test.git
[branch "master"]
    remote = origin
    merge = refs/heads/master
                                ・
                                ・
                                ・

これで指定したアカウントでpushできるようになっているはずです。

【番外編】クローンで失敗する場合

アカウントを複数使用していると、URLからクローンするときにURLの認証で失敗します。クローンの時も同様にURLに「アカウント名」と「パスワード」を入れることができます。

https://[gitアカウント名]:[パスワード]@github.com/User/test.git

27
31
2

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
27
31