I used git init --bare to create a bare repository on linux, but I want to set its source directory location at the same time, so that although the bare repository only saves git commit records, I can do it directly on linux. Find the source code.
A bare repo does not have the default worktree, but you can add one or as many as you want to.
If you create a bare repo from scratch, it does not have any commit yet. You need to either push commits from another repository or create at least one in the bare repo.
# 1. push from another repository "bar"
cd /path/to/bar
git push /path/to/foo master
# add a worktree for "master"
cd /path/to/foo
git worktree add /path/to/worktree master
# ------------------------------------------------
# 2. create commit from the bare repository "foo"
cd /path/to/foo
# create the empty tree
tree=$(git hash-object -w -t tree --stdin < /dev/null)
# create a commit from the tree
commit=$(git commit-tree -m "initial commit" $tree)
# create "master" from the commit
git update-ref refs/heads/master $commit
# add a worktree for "master"
git worktree add /path/to/worktree master
But now if you clone /path/to/foo
and make commits and then push master
back to /path/to/foo
, the status in the worktree /path/to/worktree
will be a bit odd. You need to run git reset --hard
in /path/to/worktree
to update its status and code.
Besides a worktree, you can also make a clone from /path/to/foo
.
git clone /path/to/foo worktree
cd worktree
# checkout branch "master", which should be already checked out by default
git checkout master
# update "master"
git pull origin master
# update other branches
git fetch
# checkout a new branch "dev" which has been pushed to /path/to/foo
git checkout dev