我使用 git init --bare 在 Linux 上创建了一个裸存储库,但我想同时设置其源目录位置,这样尽管裸存储库只保存 git 提交记录,但我可以直接在 Linux 上执行此操作。查找源代码。
裸存储库没有默认的工作树,但您可以根据需要添加一个或任意多个。
如果从头开始创建裸存储库,则它尚没有任何提交。您需要从另一个存储库推送提交,或者在裸存储库中创建至少一个。
# 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
但是现在,如果您克隆/path/to/foo
并进行提交,然后将master
推回/path/to/foo
,则工作树/path/to/worktree
中的状态将有点奇怪。您需要在 /path/to/worktree
中运行 git reset --hard
以更新其状态和代码。
除了工作树,您还可以从/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