如何在Git中的裸仓库上创建分支?

时间:2023-02-05 17:23:33

There are a dozen similar questions, with everyone posting long answers trying to talk the asker out of it.

有十几个类似的问题,每个人都发布了很长的答案,试图与提问者谈谈。

Is it possible to create a branch in such a situation?

在这种情况下是否可以创建分支?

I do not want to clone the repo, just so that I can create a branch in that copy's working directory, push it, and then delete the whole thing again. The script that will create this branch (after checking to see if it exists) already has access to the bare repo in Gitolite and can issue the commands directly. "git checkout" complains that the operation must be performed in a work tree.

我不想克隆repo,只是为了我可以在该副本的工作目录中创建一个分支,推送它,然后再次删除整个东西。创建此分支的脚本(在检查它是否存在之后)已经可以访问Gitolite中的裸存储库,并且可以直接发出命令。 “git checkout”抱怨操作必须在工作树中执行。

2 个解决方案

#1


3  

You can just go a little lower-level, in a bare repo.

你可以在一个简单的回购中稍微降低一点。

git branch is willing to create a new branch in a bare repo. Just give it a commit to start from and you've made a new branch name pointing at an existing commit:

git branch愿意在一个裸仓库中创建一个新的分支。只需给它一个提交开始,你已经创建了一个指向现有提交的新分支名称:

cd foo.git
git branch newbranch b6636ec88ba0750aec2706865653eb55031fb892

You can also use git update-ref, which is even lower-level than git branch and can create references outside of the refs/heads/ namespace:

你也可以使用git update-ref,它甚至比git分支更低级,并且可以在refs / heads / namespace之外创建引用:

git update-ref refs/special/ziggy b6636ec88ba0750aec2706865653eb55031fb892
git update-ref -d refs/special/delete

Edit: And, you can make indirect refs with git symbolic-ref, e.g.:

编辑:并且,您可以使用git symbolic-ref制作间接引用,例如:

git symbolic-ref MAGIC refs/tags/v1.3

(normally this would only be used to make HEAD be a different indirect ref; the main point here is that this command, like git update-ref, requires that you spell out the whole name-space name).

(通常这只会用来使HEAD成为一个不同的间接引用;这里的要点是这个命令,比如git update-ref,要求你拼出整个名字空间名称)。

#2


2  

Let's take a different angle than talking you out of it, then.

那么,让我们采取不同的角度,而不是说出来。

You can do anything you want to a bare repo.

你可以做任何你想做的事情。

  • either the hard way by just manipulating the files there the way you want
  • 或者通过以你想要的方式操作文件的艰难方式
  • or by setting GIT_DIR and using pure-repo commands
  • 或者通过设置GIT_DIR并使用pure-repo命令
  • possibly additionally setting GIT_INDEX_FILE if you need a local index
  • 如果需要本地索引,可能还需要设置GIT_INDEX_FILE
  • possible additionally setting GIT_WORK_TREE if you need one of those
  • 如果你需要其中一个,可以另外设置GIT_WORK_TREE

Some documentation about the relevant environment variables.

有关相关环境变量的一些文档。

#1


3  

You can just go a little lower-level, in a bare repo.

你可以在一个简单的回购中稍微降低一点。

git branch is willing to create a new branch in a bare repo. Just give it a commit to start from and you've made a new branch name pointing at an existing commit:

git branch愿意在一个裸仓库中创建一个新的分支。只需给它一个提交开始,你已经创建了一个指向现有提交的新分支名称:

cd foo.git
git branch newbranch b6636ec88ba0750aec2706865653eb55031fb892

You can also use git update-ref, which is even lower-level than git branch and can create references outside of the refs/heads/ namespace:

你也可以使用git update-ref,它甚至比git分支更低级,并且可以在refs / heads / namespace之外创建引用:

git update-ref refs/special/ziggy b6636ec88ba0750aec2706865653eb55031fb892
git update-ref -d refs/special/delete

Edit: And, you can make indirect refs with git symbolic-ref, e.g.:

编辑:并且,您可以使用git symbolic-ref制作间接引用,例如:

git symbolic-ref MAGIC refs/tags/v1.3

(normally this would only be used to make HEAD be a different indirect ref; the main point here is that this command, like git update-ref, requires that you spell out the whole name-space name).

(通常这只会用来使HEAD成为一个不同的间接引用;这里的要点是这个命令,比如git update-ref,要求你拼出整个名字空间名称)。

#2


2  

Let's take a different angle than talking you out of it, then.

那么,让我们采取不同的角度,而不是说出来。

You can do anything you want to a bare repo.

你可以做任何你想做的事情。

  • either the hard way by just manipulating the files there the way you want
  • 或者通过以你想要的方式操作文件的艰难方式
  • or by setting GIT_DIR and using pure-repo commands
  • 或者通过设置GIT_DIR并使用pure-repo命令
  • possibly additionally setting GIT_INDEX_FILE if you need a local index
  • 如果需要本地索引,可能还需要设置GIT_INDEX_FILE
  • possible additionally setting GIT_WORK_TREE if you need one of those
  • 如果你需要其中一个,可以另外设置GIT_WORK_TREE

Some documentation about the relevant environment variables.

有关相关环境变量的一些文档。