首页 > 解决方案 > 创建 git init --bare 时如何设置源目录的存储目录?

问题描述

我在linux上使用git init --bare创建了一个裸仓库,但是我想同时设置它的源目录位置,这样虽然裸仓库只保存了git提交记录,但是我可以直接在linux上做。找到源代码。

标签: gitgit-bare

解决方案


裸仓库没有默认工作树,但您可以添加一个或任意多个。

如果你从头开始创建一个裸仓库,它还没有任何提交。您需要从另一个存储库推送提交或在裸存储库中创建至少一个。

# 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会有点奇怪。您需要运行git reset --hard/path/to/worktree更新其状态和代码。

除了工作树,您还可以从/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

推荐阅读