【Git】アカウント設定方法

Git

主のPC環境一覧

PC:Windows10, Mac OS [Monterey]
CPU:Intel Core i7
Memory:32GB
Git:2.35.1


[1] Git の設定ファイルと保存場所

Gitアカウントには 3つの種類がある。
※以下の表は Windowsを例に示す (今、Windows PCの方で作成しているので ←)

種類影響範囲設定ファイルの保存場所備考
system 全ユーザーの全リポジトリC:\Program Files\Git\etc\gitconfig Gitのインストール場所
global該当ユーザーの全リポジトリ C:\Users\<username>\.gitconfigユーザーディレクトリのホーム直下 
local該当リポジトリ~\.git\.gitconfigローカルリポジトリの直下

上記の適応優先順位を次に示す。[ system < global < local ]


[2] アカウント設定

system

最も優先度が「」の設定。

【使用推奨ユーザー】 ※私は設定していない
システム全体で Gitアカウントを統一し使用したい方

// systemアカウントの設定
$ git config --system user.name <hogehoge>
$ git config --system user.email <example01@sample.com>

// 設定した systemアカウントの確認
$ git config --system -l
# ...
# ..
# user.name=hogehoge
# user.email=example01@sample.com

global

優先度が「」の設定

【使用推奨ユーザー】 ※個人の技術収め用として GitHubで利用している
Windowsであれば、現在ログインしているアカウント (ユーザー) のグローバル環境に設定するため、
ログインしている限りはコチラが使用される。
よく利用するアカウントを設定すると良い。

// globalアカウントの設定
$ git config --global user.name <fugafuga>
$ git config --global user.email <example02@sample.com>

// 設定した globalアカウントの確認
$ git config --global -l
# ...
# ..
# user.name=fugafuga
# user.email=example02@sample.com

local

最も優先度が「」の設定

【使用推奨ユーザー】 ※個人且つチーム開発の際に GitLabで利用している
リポジトリごとに設定するので優先度が 1番高い。
Gitを初期化した「.git\config」があるディレクトリのみでしか利用することができない。
「localアカウント」は、Gitコマンド実行時「–local」オプションを省略することが可能だが、
Git管理外で実行するとエラーとなるので気を付けてください。

// localアカウントの設定
$ git config --local user.name <piyopiyo>
$ git config --local user.email <example03@sample.com>

// 設定した localアカウントの確認
$ git config --local -l
# ...
# ..
# user.name=piyopiyo
# user.email=example03@sample.com

[*] Gitを利用した各サービス

GItを利用した有名なサービスは、以下の 4つが挙げられる。

  1. GitHub
  2. GitLab
  3. SourceTree
  4. Bitbucket

上記は Gitのサービスを利用した、また別のサービスなので勘違いしている方は注意

Test_1

ユーザー名:noanoachan
メールアドレス:honto@sample.com

// Test_1用にアカウント設定
$ git config --global user.name noanoachan
$ git config --global user.email honto@sample.com

// 新規ファイル作成
$ touch Test_1.txt

// リモートブランチへプッシュ
$ git add .
$ git commit -m "add: test_1"
$ git push

Test_2

ユーザー名:aaaaaa
メールアドレス:honto@sample.com

// Test_1用にアカウント設定
$ git config --global user.name aaaaaa
$ git config --global user.email honto@sample.com

// 新規ファイル作成
$ touch Test_2.txt

// リモートブランチへプッシュ
$ git add .
$ git commit -m "add: test_2"
$ git push

Test_3

ユーザー名:aaaaaa
メールアドレス:aaaaaa@sample.com

// Test_1用にアカウント設定
$ git config --global user.name aaaaaa
$ git config --global user.email aaaaaa@sample.com

// 新規ファイル作成
$ touch Test_3.txt

// リモートブランチへプッシュ
$ git add .
$ git commit -m "add: test_3"
$ git push

Test_4

ユーザー名:noanoachan
メールアドレス:aaaaaa@sample.com

// Test_1用にアカウント設定
$ git config --global user.name noanoachan
$ git config --global user.email aaaaaa@sample.com

// 新規ファイル作成
$ touch Test_4.txt

// リモートブランチへプッシュ
$ git add .
$ git commit -m "add: test_4"
$ git push

Test_5

ユーザー名:aaaaaa
メールアドレス:honto@sample.com

// Test_1用にアカウント設定
$ git config --global user.name aaaaaa
$ git config --global user.email honto@sample.com

// 新規ファイル作成
$ touch Test_5.txt

// リモートブランチへプッシュ
$ git add .
$ git commit -m "add: test_5"
$ git push


Result

結果:ユーザー名 (user.name) が異なっても「メールアドレス (user.email) 」が同一のものであれば、同じアカウントとして認識される。
これが、本当に正しいか否か厳密には断定できませんが、私がこの時、抱いた疑問は解決できたので良しとする。

Git Account Kind

<参考>

https://www.sejuku.net/blog/72246

comment 📝

タイトルとURLをコピーしました