この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:7
ページ更新者:T
更新日時:2026-06-11 07:12:00

タイトル: ローカルリポジトリの作成
SEOタイトル: git ローカルリポジトリ作成完全ガイド — git init / .gitignore / リモート紐付け

この記事の要点
  • git init で現在のディレクトリを Git リポジトリ化。.git/ が作られる
  • デフォルトブランチ名: git config --global init.defaultBranch main を最初に設定推奨
  • 共有リモート用には git init --bare(作業ツリー無し、repo.git 慣習)
  • 初回コミット: .gitignore 作成 → git add .git commit -m "Initial commit"
  • リモート紐付け: git remote add origin git@github.com:user/repo.gitgit push -u origin main

git init の基本

既存のディレクトリを Git 管理下に置くには git init を実行します。.git/ という隠しディレクトリが作られ、ここに履歴やオブジェクトが格納されます。

# 既存ディレクトリを初期化
cd ~/projects/myapp
git init

# 出力 (Git 2.28+)
# Initialized empty Git repository in /home/user/projects/myapp/.git/

# 新規ディレクトリごと作成
git init myapp
cd myapp

# 現在の状態確認
git status
# On branch main
# No commits yet

デフォルトブランチ名を main に

Git 2.28 以前は master、それ以降は OS によって異なります。チームで揃えるためにグローバル設定:

# グローバルで main に統一(最初に一度だけ)
git config --global init.defaultBranch main

# 確認
git config --global init.defaultBranch
# main

# 既に master で init してしまったら改名
git branch -m master main

.gitignore の作成

追跡したくないファイル(依存ライブラリ、ビルド成果物、秘密情報)を .gitignore に列挙します。プロジェクトのルートに置きます。

# /.gitignore

# OS
.DS_Store
Thumbs.db

# Editor
.idea/
.vscode/
*.swp

# Node.js
node_modules/
npm-debug.log*
.env.local

# PHP / Laravel
vendor/
storage/*.key
storage/logs/*
storage/framework/cache/*
.env

# Python
__pycache__/
*.pyc
.venv/

# Build artifacts
dist/
build/
*.o
*.class

# Logs
*.log

テンプレートは github/gitignore リポジトリから言語別に取得できます。

初回コミット

# 状態確認
git status
# Untracked files: .gitignore, README.md, src/...

# ユーザ情報(未設定なら最初に)
git config user.name "Taro Yamada"
git config user.email "taro@example.com"

# 全ファイルをステージング
git add .

# ステージング内容を確認
git status

# コミット
git commit -m "Initial commit"

# 履歴
git log --oneline
# a1b2c3d (HEAD -> main) Initial commit

リモートリポジトリへの紐付け

# GitHub で空リポジトリを作成しておく
# https://github.com/new

# SSH で紐付け(推奨)
git remote add origin git@github.com:user/myapp.git

# HTTPS で紐付け(CI 等)
git remote add origin https://github.com/user/myapp.git

# 確認
git remote -v
# origin  git@github.com:user/myapp.git (fetch)
# origin  git@github.com:user/myapp.git (push)

# 初回 push(-u で upstream 設定)
git push -u origin main

# 以降は単に
git push
git pull

ベアリポジトリ (--bare)

自前のサーバを共有リモートとして立てる場合は ベアリポジトリ(作業ツリー無し)で初期化します。慣習として .git サフィックスを付けます。

# サーバ側 (例: 192.168.1.10)
ssh git@192.168.1.10
mkdir -p /srv/git/myapp.git
cd /srv/git/myapp.git
git init --bare

# 出力: Initialized empty Git repository in /srv/git/myapp.git/
# .git/ ではなく、カレント直下に HEAD / objects/ refs/ などが作られる

# クライアント側
git remote add origin git@192.168.1.10:/srv/git/myapp.git
git push -u origin main

.git ディレクトリの中身

パス役割
HEAD現在のブランチ参照(ref: refs/heads/main
configこのリポジトリ専用の設定
objects/全コミット/ツリー/blob のオブジェクトストア
refs/heads/ローカルブランチ参照
refs/remotes/リモートブランチのキャッシュ
refs/tags/タグ
hooks/pre-commit / pre-push 等のフック(サンプル付)
info/exclude個人用 .gitignore(共有しない)
indexステージング領域(バイナリ)
logs/reflog(HEAD の動き)

テンプレートを使った init

# テンプレートディレクトリを作っておく
mkdir -p ~/git-template/hooks
cat > ~/git-template/hooks/pre-commit <<'EOF'
#!/bin/sh
# コミット前に lint 実行
npm run lint || exit 1
EOF
chmod +x ~/git-template/hooks/pre-commit

# テンプレートを使って init
git init --template=~/git-template

# グローバル設定にしてもよい
git config --global init.templateDir ~/git-template

サブモジュール / Worktree

# 別リポジトリを子として組み込む
git submodule add https://github.com/user/lib.git libs/lib
git submodule init
git submodule update --recursive

# 1 つのリポジトリで複数ワーキングツリーを持つ
git worktree add ../myapp-hotfix hotfix/1.2.3
# ../myapp-hotfix で hotfix/1.2.3 ブランチを別ディレクトリで作業可能
git worktree list
git worktree remove ../myapp-hotfix

typical な初期化スクリプト

#!/bin/bash
# new-project.sh
set -euo pipefail

NAME=$1
mkdir "$NAME" && cd "$NAME"

git init -b main
cat > .gitignore <<EOF
node_modules/
.env
dist/
*.log
EOF

cat > README.md <<EOF
# $NAME

TODO: write description
EOF

git add .
git commit -m "Initial commit"

echo "Repository created: $(pwd)"
echo "Next: git remote add origin <URL> && git push -u origin main"

FAQ

Q: git init 後にやり直したい
A: rm -rf .git で履歴ごと削除して再 init。作業ファイルは消えません。

Q: 既存ディレクトリの一部だけ管理したい
A: ルートで init し、.gitignore で対象外を除外する方が素直。サブディレクトリ単独 init も可能だが複雑になる。

Q: bare と通常の違いは?
A: bare は作業ツリーが無く、push 受け取り専用。複数開発者で共有する中央リポジトリとして使う。GitHub 内部もベアリポジトリ。