日本語

Creating a Local Practice Remote with git init –bare

​​Git​command line

A repository created with git init --bare remote.git can be used as the push and clone target in place of a remote such as GitHub. It needs no account and no server, so it is handy for testing the push, clone, and pull flow locally, or for reproducing on one machine the flow of several people sharing the same repository. I ran the commands in a test environment and include the output from before and after each operation as it appeared.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>), and set init.defaultBranch to main. I fixed the commit dates with environment variables. The folder paths in the output come from running the commands under the example path /path/to/work. I did not test pushing to a bare repository placed on a server over SSH or HTTPS, and I did not test on Windows or macOS.

The main setup uses three repositories side by side in one folder. remote.git is the practice remote, and neko-a and neko-b are working repositories, each assumed to belong to a different person. For comparison, I also create normal and neko-c partway through.

Diagram showing remote.git at the center, with neko-a pushing and pulling and neko-b cloning and pushing
The three repositories mainly used in this article and the order of operations

Creating a remote repository with git init –bare

I ran git init --bare in an empty folder. It is customary to end the name of a remote repository with .git.

console
$ 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

Inside remote.git are the entries that normally sit inside the .git folder, such as HEAD and objects, laid out directly. This is a bare repository. According to the explanation in the Git glossary, a bare repository is one that has no working directory with checked-out files. The management files that normally live inside the hidden .git folder are placed directly in a folder whose name ends in .git.

Comparing with a normal git init shows the difference. A normal repository has a .git inside it, and its core.bare setting is false. In the bare repository it was true.

console
$ 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

Because there is no working directory, git status cannot be used. Instead of editing files and committing directly inside the bare repository, you fill it by pushing from another repository. The newly created remote.git has no branches yet, and git ls-remote shows nothing for it. HEAD points to main.

console
$ 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

Pushing from a working repository

In neko-a I made one commit and registered remote.git as origin. Instead of a URL, you can write a folder path as the remote location. The Git URL section in the git push reference also says a local repository can be specified in forms such as /path/to/repo.git/ or file:///path/to/repo.git/. Here I used the relative path ../remote.git as seen from neko-a.

console
$ 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)

Before pushing, git status shows nothing about the relationship with the remote.

console
$ git status
On branch main
nothing to commit, working tree clean

I pushed with git push -u origin main. The main branch was created in remote.git. Because I used -u, main in neko-a now tracks origin/main, and git status shows that relationship with origin/main.

console
$ 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] 最初のコミット

On the remote.git side, git ls-remote now shows main, and git log confirms the commit. The contents of the folder are the same as before the push, and README.md does not appear. The commit is stored in objects.

console
$ 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

Cloning into another folder

I cloned remote.git into neko-b. The commit pushed from neko-a is there, and README.md has been checked out into the working folder.

console
$ 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 shows that origin was registered with the absolute path of remote.git, even though I gave a relative path when cloning. This differs from git remote add, which keeps the relative path as written.

Bringing changes pushed from neko-b into neko-a

The flow of two people sharing the same remote can also be tested as-is. In neko-b I added a file, committed it, and ran git push. A cloned repository already tracks origin/main from the start, so -u is not needed.

console
$ 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

I went back to neko-a and ran git status, which still showed up to date. The origin/main that git status compares against is the local copy, and it stays old until you fetch. After git fetch retrieved from remote.git, origin/main was updated, and the output showed it was one commit behind. In the fetch output the source is shown as From ../remote, with the final .git omitted. The official git fetch reference also states that fetching updates remote-tracking branches such as origin/main.

console
$ 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

When I brought the changes in with git pull, hello.txt, which I had added in neko-b, appeared in neko-a too.

console
$ 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

Pushing directly to a working repository is rejected

The reason to use a bare repository becomes clear when you try pushing to a normal one. I made a commit in neko-c, which I had cloned from remote.git, and pushed it directly to neko-a, which has a working folder with main checked out, instead of to remote.git.

console
$ 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'

The push was rejected with the reason branch is currently checked out, and the history of neko-a did not change.

console
$ cd ..

$ git -C neko-a log --oneline
14de653 hello.txt を追加
9d9b549 最初のコミット

As the error message says, if you rewrite the currently checked-out branch by pushing into a repository that has a working folder, the contents of the working folder and index no longer match the commit the branch points to. According to the explanation of receive.denyCurrentBranch, the default value of this setting is refuse. A bare repository has no working folder, so this mismatch does not occur. As shown in the previous section, I could push to main, which HEAD points to. This is why I create the practice remote with git init --bare.

When you finish practicing, delete the folder

A practice remote is just a folder. When you no longer need it, you can delete it with rm -rf remote.git. After deleting it, running git fetch in neko-a produced an error that the remote could not be found.

console
$ 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.
Warning

A bare repository deleted with rm -rf cannot be restored. Any commits that exist only in it are lost too. If you are deleting a bare repository that is actually used as a remote, not a practice one, check its contents first.

​Posted by 86