git init –bareで、手元に練習用のリモートを作る方法
git init --bare remote.git で作ったリポジトリは、GitHub などのリモートの代わりに、push や clone の宛先として使えます。アカウントもサーバーも要らないので、push、clone、pull の流れを手元で試したいときや、複数人で同じリポジトリを使う流れを1台で再現したいときに便利です。検証環境でコマンドを実行し、操作の前と後の出力をそのまま載せています。
検証環境は Ubuntu 24.04、Git 2.43.0 です。作者の名前とメールアドレスには例示用の値(Neko <neko@example.com>)を設定し、init.defaultBranch を main にしています。コミットの日時は環境変数で決めています。出力に出るフォルダーの場所は、例示用のパス /path/to/work で実行したものです。SSH や HTTPS でサーバーに置いたベアリポジトリへの push、Windows と macOS での動作は試していません。
主に使うのは、同じフォルダーに並べた3つのリポジトリです。remote.git が練習用のリモート、neko-a と neko-b が、それぞれ別の人の作業用のリポジトリという想定です。比べるために、途中で normal と neko-c も作ります。

git init –bare でリモート用のリポジトリを作る
空のフォルダーで git init --bare を実行しました。リモートとして使うリポジトリは、名前の最後に .git を付けるのが習慣です。
$ ls
$ git init --bare remote.git
Initialized empty Git repository in /path/to/work/remote.git/
$ ls remote.git
HEAD
branches
config
description
hooks
info
objects
refs
remote.git の中には、ふだん .git フォルダーの中にある HEAD や objects などが、そのまま並んでいます。これがベアリポジトリです。Git の用語集の説明によると、ベアリポジトリは、ファイルを取り出した作業フォルダーを持たないリポジトリです。ふつうは隠しフォルダー .git の中にある管理用のファイルが、〜.git という名前のフォルダーに直接置かれます。
ふつうの git init と比べると、違いが分かります。ふつうのリポジトリは中に .git があり、設定 core.bare が false です。ベアリポジトリでは true でした。
$ git init -q normal
$ ls -A normal
.git
$ git -C normal config --get core.bare
false
$ git -C remote.git config --get core.bare
true
$ git -C remote.git rev-parse --is-bare-repository
true
作業フォルダーがないので、git status は使えません。ベアリポジトリの中で直接ファイルを編集してコミットするのではなく、ほかのリポジトリから push して中身を入れます。作ったばかりの remote.git にはまだブランチがなく、git ls-remote で見ても何も表示されません。HEAD は main を指しています。
$ git -C remote.git status
fatal: this operation must be run in a work tree
$ git ls-remote remote.git
$ git -C remote.git symbolic-ref HEAD
refs/heads/main
作業用のリポジトリから push する
neko-a でコミットを1つ作り、remote.git を origin として登録しました。リモートの場所には、URL の代わりにフォルダーのパスを書けます。git push のリファレンスにある、Git の URL の書き方の説明にも、手元のリポジトリは /path/to/repo.git/ や file:///path/to/repo.git/ の形で指定できると書かれています。ここでは neko-a から見た相対パス ../remote.git にしました。
$ git init -q neko-a
$ cd neko-a
$ printf "# neko\n" > README.md
$ git add README.md && git commit -m "最初のコミット"
[main (root-commit) 9d9b549] 最初のコミット
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ git remote add origin ../remote.git
$ git remote -v
origin ../remote.git (fetch)
origin ../remote.git (push)
push する前の git status には、リモートとの関係が何も表示されていません。
$ git status
On branch main
nothing to commit, working tree clean
git push -u origin main で push しました。remote.git に main ブランチが作られました。-u を付けたので、neko-a の main は origin/main を追跡するようになり、git status にも origin/main との関係が出ました。
$ git push -u origin main
To ../remote.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
$ git branch -vv
* main 9d9b549 [origin/main] 最初のコミット
remote.git の側では、git ls-remote で main が見えるようになり、git log でコミットも確かめられます。一方、フォルダーの中身は push の前と同じで、README.md は現れません。コミットは objects の中に入っています。
$ cd ..
$ git ls-remote remote.git
9d9b549bb61eda886701a479d4d2aa6d8b00ada2 HEAD
9d9b549bb61eda886701a479d4d2aa6d8b00ada2 refs/heads/main
$ git -C remote.git log --oneline
9d9b549 最初のコミット
$ ls remote.git
HEAD
branches
config
description
hooks
info
objects
refs
別のフォルダーに clone する
remote.git を neko-b に clone しました。neko-a で push したコミットが入っていて、作業フォルダーには README.md が取り出されています。
$ git clone remote.git neko-b
Cloning into 'neko-b'...
done.
$ cd neko-b
$ git log --oneline
9d9b549 最初のコミット
$ git branch -vv
* main 9d9b549 [origin/main] 最初のコミット
$ ls
README.md
$ git remote -v
origin /path/to/work/remote.git (fetch)
origin /path/to/work/remote.git (push)
git remote -v を見ると、clone のときは相対パスで指定したのに、origin は remote.git の絶対パスで登録されていました。neko-a のように git remote add で書いた相対パスが、そのまま残るのとは違います。
neko-b で push した変更を neko-a に取り込む
2人で同じリモートを使う流れも、そのまま試せます。neko-b でファイルを追加してコミットし、git push しました。clone したリポジトリは最初から origin/main を追跡しているので、-u は要りません。
$ printf "hello\n" > hello.txt
$ git add hello.txt && git commit -q -m "hello.txt を追加"
$ git push
To /path/to/work/remote.git
9d9b549..14de653 main -> main
neko-a に戻って git status を見ると、まだ up to date(最新)と表示されます。git status が比べている手元の origin/main は、fetch するまで古いままだからです。git fetch で remote.git から取得すると、origin/main が更新され、1コミット遅れていることが表示されました。From ../remote のように、取得元の表示では最後の .git が省かれていました。git fetch の公式リファレンスにも、fetch でリモート追跡ブランチ(origin/main など)が更新されると書かれています。
$ cd ../neko-a
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
$ git fetch
From ../remote
9d9b549..14de653 main -> origin/main
$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
git pull で取り込むと、neko-b で追加した hello.txt が neko-a にも現れました。
$ git pull
Updating 9d9b549..14de653
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 hello.txt
$ git log --oneline
14de653 hello.txt を追加
9d9b549 最初のコミット
$ ls
README.md
hello.txt
作業用のリポジトリに直接 push すると拒否される
ベアリポジトリを使う理由は、ふつうのリポジトリに push してみると分かります。remote.git から clone した neko-c でコミットを作り、remote.git ではなく neko-a(作業フォルダーがあり、main をチェックアウトしている)に直接 push しました。
$ git clone -q remote.git neko-c
$ cd neko-c
$ printf "c\n" > c.txt
$ git add c.txt && git commit -q -m "c.txt を追加"
$ git push ../neko-a main
remote: error: refusing to update checked out branch: refs/heads/main
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To ../neko-a
! [remote rejected] main -> main (branch is currently checked out)
error: failed to push some refs to '../neko-a'
branch is currently checked out(そのブランチはチェックアウトされている)という理由で拒否され、neko-a の履歴は変わりませんでした。
$ cd ..
$ git -C neko-a log --oneline
14de653 hello.txt を追加
9d9b549 最初のコミット
エラーの文面のとおり、作業フォルダーのあるリポジトリで、チェックアウトしているブランチを push で書き換えると、作業フォルダーやインデックスの中身と、ブランチの指すコミットが食い違ってしまいます。receive.denyCurrentBranch の説明によると、この設定の既定値は refuse(拒否する)です。ベアリポジトリには作業フォルダーがないので、この食い違いが起きません。前の節のとおり、HEAD が指している main にも push できました。練習用のリモートを git init --bare で作るのは、このためです。
練習が終わったら、フォルダーを消す
練習用のリモートは、ただのフォルダーです。要らなくなったら rm -rf remote.git で消せます。消したあとに neko-a で git fetch すると、リモートが見つからないというエラーになりました。
$ rm -rf remote.git
$ git -C neko-a fetch
fatal: '../remote.git' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
rm -rf で消したベアリポジトリは元に戻せません。ほかのリポジトリにないコミットが入っていると、それも消えます。練習用ではなく、実際にリモートとして使っているベアリポジトリを消すときは、先に中身を確かめてください。





