Git: Gitの設定に関する詳細ガイド:ユーザーIDからカスタム設定まで

Gitの設定は、Gitを使い始めるための第一歩であり、適切に設定することで、作業の効率化とプロフェッショナルな仕上がりにつながります。このレッスンでは、Gitの設定に関するあらゆる側面について詳しく解説します。

1. Gitの設定の概要

Gitの設定(git config)は、Gitの動作を指定したり、ユーザー情報を設定したりするために使用されます。設定情報は設定ファイルに保存され、Gitはこれらの設定を優先順位の高い順に読み込みます。

(1) なぜ設定が必要なのでしょうか?

Git には以下の情報が必要です:

(2) 設定コマンドの基本構文

BASH
git config [<Level>] <Configuration Options> <value>

レベルパラメータ:



2. ユーザーIDの設定

ユーザーIDはGitの設定の中で最も重要なものです。この情報は、コミットが行われるたびに記録されます。

(1) なぜアイデンティティの設定が必要なのでしょうか?

身分証明のない世界を想像してみてください:

TEXT 📖 参照専用
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??)

身元情報の役割:

(2) ユーザー名とメールアドレスを設定する

BASH
# 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情報の設定と確認

BASH
# 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
💡 ヒント: GitHub/GitLab アカウントに登録されているメールアドレスを使用することをお勧めします。そうすることで、コミット履歴がアカウントに紐付けられ、アバターやユーザー名が表示されるようになります。



3. 設定レベルと優先順位

Gitの設定は、優先順位の高い順に、ローカル > グローバル > システムの3つのレベルに分かれています。

(1) 構成レベルの詳細な説明

100%
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:

TEXT 📖 参照専用
System Configuration:C:\ProgramData\Git\config
Global Configuration:C:\Users\Username\.gitconfig
Local Configuration:Project Directory\.git\config

Linux/Macシステム:

TEXT 📖 参照専用
System Configuration:/etc/gitconfig
Global Configuration:~/.gitconfig
Local Configuration:Project Directory/.git/config

(3) 優先順位に関する規則

同じ設定オプションが複数のレベルで指定されている場合、Git は優先順位が最も高い値を使用します:

TEXT 📖 参照専用
Local Configuration(Highest)→ Global Configuration(Intermediate)→ System Configuration(Lowest)

▶ サンプル:さまざまなレベルでの設定

BASH
# 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では、テキスト入力が必要な場合(コミットメッセージの記述時など)、テキストエディタが起動します。

BASH
# 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) 自動改行の設定

オペレーティングシステムによって、改行文字は異なります:

BASH
# 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) コマンドエイリアスの設定

エイリアスを使えば、頻繁に使用するコマンドに短い名前を割り当てることができ、作業効率を大幅に向上させることができます。

BASH
# 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

▶ サンプル:実用的な設定の組み合わせ

BASH
# 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) すべての設定を表示

BASH
# 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つの構成アイテムを表示する

BASH
# 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) 設定を変更する

BASH
# Change Username(Overwrite directly)
git config --global user.name "New Name"

# Change Email Address
git config --global user.email "new.email@example.com"

(4) 構成項目の削除

BASH
# 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

▶ サンプル:設定情報の表示

BASH
# 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

❓ よくある質問

Q 設定するメールアドレスは、GitHubに登録しているメールアドレスと一致している必要がありますか?
A 必須ではありませんが、一貫性を保つことを強くお勧めします。同じユーザー名を使用すると、GitHub がコミット履歴をあなたのアカウントに関連付け、プロフィール写真とユーザー名を表示し、あなたの貢献履歴がより明確になります。
Q 投稿された内容に間違ったユーザー名が表示されるのはなぜですか?
A ローカル設定がグローバル設定を上書きしている可能性があります。git config --local --list を使用してローカル設定を確認するか、git config --show-origin user.name を使用して設定ソースを表示してください。
Q 構成項目を削除するにはどうすればよいですか?
A git config --global --unset 配置项名称(例:git config --global --unset user.name)を使用して、グローバルユーザー名の設定を削除してください。
Q 設定ファイルは手動で編集できますか?
A はい。設定ファイルはプレーンテキストファイルですので、テキストエディタで直接開いて編集することができます。ただし、構文エラーを防ぐため、git config コマンドを使用することをお勧めします。

📖 まとめ


📝 練習問題

  1. 基本演習(難易度:⭐):Gitのグローバル認証情報(ユーザー名とメールアドレス)を設定し、設定が正常に行われたことを確認してください。

  2. 上級演習(難易度:⭐⭐):少なくとも3つの有用な設定オプション(デフォルトのブランチ名、出力の色、コマンドのエイリアスなど)を設定し、各オプションの目的を説明してください。

  3. 課題(難易度:⭐⭐⭐):テスト用リポジトリを作成し、そのリポジトリ内でローカルID(グローバルIDとは異なるもの)を設定し、ローカル設定がグローバル設定よりも優先されることを確認した上で、優先順位のルールを説明してください。

Web-Tutorial.com

Web-Tutorial 技術チーム

複数の開発者によって共同維持されているプログラミングチュートリアルプラットフォーム。各チュートリアルは専門分野の開発者が執筆・レビューしています。正確で信頼性の高いコンテンツを目指しています — 問題を見つけた場合はお知らせください。

100%