Git: Gitの設定に関する詳細ガイド:ユーザーIDからカスタム設定まで
Gitの設定は、Gitを使い始めるための第一歩であり、適切に設定することで、作業の効率化とプロフェッショナルな仕上がりにつながります。このレッスンでは、Gitの設定に関するあらゆる側面について詳しく解説します。
1. Gitの設定の概要
Gitの設定(git config)は、Gitの動作を指定したり、ユーザー情報を設定したりするために使用されます。設定情報は設定ファイルに保存され、Gitはこれらの設定を優先順位の高い順に読み込みます。
(1) なぜ設定が必要なのでしょうか?
Git には以下の情報が必要です:
- ユーザーの身元:投稿ごとに、投稿者情報(氏名、メールアドレス)が記録されます。
- エディタの選択: コミットメッセージを作成する際に使用するテキストエディタ
- 動作設定:デフォルトのブランチ名、改行の扱い、出力の色付けなど。
- 生産性向上ツール:コマンドエイリアス、オートコンプリートなど。
(2) 設定コマンドの基本構文
git config [<Level>] <Configuration Options> <value>
レベルパラメータ:
--system: システム全体(全ユーザー)--global: グローバルレベル(現在のユーザー)--local: ローカルレベル(現在のリポジトリ)- レベルが指定されていない場合、デフォルトは
--localです。
2. ユーザーIDの設定
ユーザーIDはGitの設定の中で最も重要なものです。この情報は、コミットが行われるたびに記録されます。
(1) なぜアイデンティティの設定が必要なのでしょうか?
身分証明のない世界を想像してみてください:
Submit1:Added a login feature(Who is the author??)
Submit2:Fixedbug(Who fixed it??)
Submit3:Performance has been optimized(Is there a way to contact the author??)
身元情報の役割:
- コミット履歴:各コミットには、作成者と提出者の情報が記録されています
- チームでの共同作業:チームメンバーは、誰が変更を加えたかを確認できます
- 責任の所在:問題が発生した際に、責任者を迅速に特定できること
- アカウントの連携:GitHub/GitLabは、メールアドレスに基づいてアカウントを連携します
(2) ユーザー名とメールアドレスを設定する
# Configure Global Username(We recommend using your real name or a nickname.)
git config --global user.name "Zhang San"
# Configure a Global Email Account(Recommended for useGitHub/GitLabAccount Email)
git config --global user.email "zhangsan@example.com"
▶ サンプル:ID情報の設定と確認
# Steps1:Configure Username
git config --global user.name "Zhang San"
# Steps2:Set Up Email
git config --global user.email "zhangsan@example.com"
# Steps3:Verify Configuration
git config user.name
# Output:Zhang San
git config user.email
# Output:zhangsan@example.com
# Steps4:View the contents of the configuration file
cat ~/.gitconfig
# Output:
# [user]
# name = Zhang San
# email = zhangsan@example.com
3. 設定レベルと優先順位
Gitの設定は、優先順位の高い順に、ローカル > グローバル > システムの3つのレベルに分かれています。
(1) 構成レベルの詳細な説明
graph TB
A[Local Configuration<br/>--local<br/>Priority:Highest] --> B[Global Configuration<br/>--global<br/>Priority:Intermediate]
B --> C[System Configuration<br/>--system<br/>Priority:Lowest]
style A fill:#e1f5ff
style B fill:#fff4e1
style C fill:#f0f0f0
設定レベルの説明表:
| レベル | パラメータ | ファイルの場所 | 適用範囲 | ユースケース |
|---|---|---|---|---|
| ローカル設定 | --local |
.git/config |
現在のリポジトリ | プロジェクト固有の設定 |
| グローバル設定 | --global |
~/.gitconfig |
現在のユーザーのすべてのリポジトリ | 個人設定 |
| システム構成 | --system |
/etc/gitconfig |
すべてのシステムユーザー | システムのデフォルト設定 |
(2) 設定ファイルの保存場所
Windows:
System Configuration:C:\ProgramData\Git\config
Global Configuration:C:\Users\Username\.gitconfig
Local Configuration:Project Directory\.git\config
Linux/Macシステム:
System Configuration:/etc/gitconfig
Global Configuration:~/.gitconfig
Local Configuration:Project Directory/.git/config
(3) 優先順位に関する規則
同じ設定オプションが複数のレベルで指定されている場合、Git は優先順位が最も高い値を使用します:
Local Configuration(Highest)→ Global Configuration(Intermediate)→ System Configuration(Lowest)
▶ サンプル:さまざまなレベルでの設定
# Scene:Global Use"Zhang San",But a certain project requires the use of"Li Si"
# Steps1:Configure Global Identity(Applicable to all projects)
git config --global user.name "Zhang San"
git config --global user.email "zhangsan@example.com"
# Steps2:Go to a specific project directory
cd /path/to/special-project
# Steps3:Configure Local Identity(Current project only)
git config --local user.name "Li Si"
git config --local user.email "lisi@company.com"
# Steps4:Verify the identity used for the current project
git config user.name
# Output:Li Si(Local settings override global settings.)
# Steps5:Verify Global Identity(In other projects)
cd /path/to/other-project
git config user.name
# Output:Zhang San(Using Global Settings)
4. 一般的な設定オプション
ユーザーアカウント以外にも、作業の効率化に役立つ便利な設定が数多く用意されています。
(1) エディタの設定
Gitでは、テキスト入力が必要な場合(コミットメッセージの記述時など)、テキストエディタが起動します。
# Set the default editor toVS Code(Recommendations)
git config --global core.editor "code --wait"
# Set the default editor toVim
git config --global core.editor "vim"
# Set the default editor toNano
git config --global core.editor "nano"
# Windows:Set the default editor toNotepad++
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
(2) 自動改行の設定
オペレーティングシステムによって、改行文字は異なります:
- Windows:CRLF(
\r\n) - Linux/Mac: LF (
\n)
# WindowsRecommended User Configurations
# Convert upon submission toLF,Convert to upon detectionCRLF
git config --global core.autocrlf true
# Linux/MacRecommended User Configurations
# Convert upon submission toLF,Do not convert when detected
git config --global core.autocrlf input
# Disable Auto-Conversion(Not recommended)
git config --global core.autocrlf false
(3) コマンドエイリアスの設定
エイリアスを使えば、頻繁に使用するコマンドに短い名前を割り当てることができ、作業効率を大幅に向上させることができます。
# Set Aliases for Common Commands
git config --global alias.st status # git st = git status
git config --global alias.co checkout # git co = git checkout
git config --global alias.br branch # git br = git branch
git config --global alias.ci commit # git ci = git commit
git config --global alias.unstage 'reset HEAD --' # git unstage
# Set an alias with options
git config --global alias.last 'log -1 HEAD' # git last = View the last commit
git config --global alias.lg "log --graph --oneline --decorate --all" # Formatted Logs
▶ サンプル:実用的な設定の組み合わせ
# Steps1:Set the default branch name tomain(rather thanmaster)
git config --global init.defaultBranch main
# Enable Color Output (make git output easier to read)
git config --global color.ui auto
# Steps3:Set to use when pullingmerge(rather thanrebase)
git config --global pull.rebase false
# Steps4:Configure push settings to push only to the current branch
git config --global push.default simple
# Steps5:Turn on AutoCorrect(Automatically attempt to correct a mistyped command)
git config --global help.autocorrect 1
# Steps6:Verify all configurations
git config --global --list | grep -E "(init|color|pull|push|help)"
# Output:
# init.defaultbranch=main
# color.ui=auto
# pull.rebase=false
# push.default=simple
# help.autocorrect=1
5. 設定の表示と管理
設定の表示や管理方法を理解することは、Git を使用するための基本的なスキルです。
(1) すべての設定を表示
# View All Configurations(Results of the Merger of the Three Levels)
git config --list
# View Global Settings
git config --global --list
# View Local Configuration
git config --local --list
# View System Configuration
git config --system --list
(2) 1つの構成アイテムを表示する
# View Username
git config user.name
# Check Email
git config user.email
# View Editor
git config core.editor
# View the default branch name
git config init.defaultBranch
(3) 設定を変更する
# Change Username(Overwrite directly)
git config --global user.name "New Name"
# Change Email Address
git config --global user.email "new.email@example.com"
(4) 構成項目の削除
# Delete Global Configuration Items
git config --global --unset user.name
# Delete Local Configuration Entries
git config --local --unset user.name
# Delete Alias
git config --global --unset alias.st
▶ サンプル:設定情報の表示
# Steps1:View All Configurations
git config --list
# Output Example:
# user.name=Zhang San
# user.email=zhangsan@example.com
# core.editor=code --wait
# init.defaultbranch=main
# color.ui=auto
# alias.st=status
# alias.co=checkout
# alias.br=branch
# alias.ci=commit
# Steps2:View a Single Configuration
git config user.name
# Output:Zhang San
# Steps3:View Configuration Source(Show which file the configuration comes from)
git config --show-origin user.name
# Output:file:C:/Users/Zhang San/.gitconfig Zhang San
# Steps4:View all configurations and their sources
git config --list --show-origin
# Output Example:
# file:C:/ProgramData/Git/config core.symlinks=false
# file:C:/Users/Zhang San/.gitconfig user.name=Zhang San
# file:.git/config core.repositoryformatversion=0
❓ よくある質問
git config --local --list を使用してローカル設定を確認するか、git config --show-origin user.name を使用して設定ソースを表示してください。git config --global --unset 配置项名称(例:git config --global --unset user.name)を使用して、グローバルユーザー名の設定を削除してください。git config コマンドを使用することをお勧めします。📖 まとめ
- Gitの設定では、ユーザーIDやエディタ、動作の好みなどを設定します。これはGitを使い始めるための最初のステップです。
- 設定は、ローカル(最優先)→ グローバル → システム(最下位)の3つのレベルに分類されます。
- ユーザー名とメールアドレスを設定する必要があります。この情報は、投稿のたびに記録されます。
- 一般的な設定には、エディタ、改行コードの扱い、コマンドのエイリアス、デフォルトのブランチ名などがあります。
git config --listを使用するとすべての設定を表示でき、git config <key>を使用すると単一の設定を表示できます- 設定ファイルの保存場所:グローバルでは
~/.gitconfig、ローカルでは.git/config
📝 練習問題
-
基本演習(難易度:⭐):Gitのグローバル認証情報(ユーザー名とメールアドレス)を設定し、設定が正常に行われたことを確認してください。
-
上級演習(難易度:⭐⭐):少なくとも3つの有用な設定オプション(デフォルトのブランチ名、出力の色、コマンドのエイリアスなど)を設定し、各オプションの目的を説明してください。
-
課題(難易度:⭐⭐⭐):テスト用リポジトリを作成し、そのリポジトリ内でローカルID(グローバルIDとは異なるもの)を設定し、ローカル設定がグローバル設定よりも優先されることを確認した上で、優先順位のルールを説明してください。