01
設定と初期化
グローバルとローカル設定
Git は3つのレベルで設定を保存します:システム(/etc/gitconfig)、グローバル(ユーザーの ~/.gitconfig)、ローカル(リポジトリごとの .git/config)。下位レベルが上位レベルを上書きします。コミット前に必ず user.name と user.email を設定してください。そうしないと役に立たないデフォルトIDが使用されます。init.defaultBranch を main に設定すると、非推奨の master デフォルトを避け、現代の慣行に合わせることができます。
git
# set user identity (required for commits)
git config --global user.name "Alice Lee"
git config --global user.email "[email protected]"
# set default editor and branch name
git config --global core.editor "code --wait"
git config --global init.defaultBranch main
# view all settings and their origin
git config --list --show-origin
# edit config files directly
git config --global --edit # ~/.gitconfig
git config --edit # repo .git/configリポジトリの作成とクローン
git init はバージョンデータをすべて格納する隠し .git ディレクトリを追加して空のリポジトリを作成します。git clone は完全な履歴を含むリモートリポジトリをコピーします。最新のスナップショットのみが必要な場合(CI ビルドなど)は --depth 1 でシャロークローンを使用します — ダウンロードサイズを劇的に削減します。--single-branch は無関係なブランチの取得を避けます。
git
# create a new repo from scratch in current dir
mkdir my-app && cd my-app
git init
# clone a remote repository
git clone https://github.com/user/repo.git
# clone into a specific folder name
git clone https://github.com/user/repo.git my-folder
# clone a single branch (saves bandwidth)
git clone -b dev --single-branch https://github.com/user/repo.git
# shallow clone: only the latest commit
git clone --depth 1 https://github.com/user/repo.gitエイリアスとショートカット
エイリアスは頻繁に使用するコマンドやコマンドシーケンスに短い名前を定義できます。gitconfig の [alias] セクションに保存されます。! で始まるエイリアスはシェルコマンドとして実行され、複雑なワークフローを可能にします。上記の lg エイリアスは、ブランチ構造を理解するのに非常に便利なコンパクトな視覚的履歴グラフを生成します。
git