如何在Git中的裸存储库中创建分支

Ger*_*Eng 21 git

我目前有一个简单的回购,作为我的团队的核心回购.裸仓库目前只有一个分支"主".如何在裸仓库上创建更多分支?

Kin*_*nch 20

通常,您不直接在裸存储库中创建分支,但是您将分支从一个工作存储库推送到裸存储库

git push origin myBranch
Run Code Online (Sandbox Code Playgroud)

更新:值得一提

就像Paul Pladijs在评论中提到的那样

git push origin localBranchName:remoteBranchName
Run Code Online (Sandbox Code Playgroud)

您将本地分支推送(并创建,如果不存在)到具有不同分支名称的本地分支,即本地分支.并使其完整

git push origin :remoteBranchName
Run Code Online (Sandbox Code Playgroud)

你删除了一个远程分支.

  • 这是什么吉卜雅柏?那家伙问了一个简单的问题:“如何在裸仓库中创建分支?”。当然,任何建议都可能受到欢迎,但请在回答问题后保留到最后。有人问过你“通常”会发生什么吗?也许他是经理,没有也不想要签出的工作树。正确的答案是@arcyqwerty的答案(git分支branchname)。请将其标记为已接受。 (2认同)

Dmi*_*rov 8

git update-ref refs/heads/new_branch refs/heads/master
Run Code Online (Sandbox Code Playgroud)

在那个裸存储库中,如果您可以直接访问它.您可以在最后一个参数中提供任何引用(例如标记)或提交.

以下是测试脚本:

$ mkdir non-bare-orig

$ cd non-bare-orig/

$ git init
Initialized empty Git repository in D:/Temp/bare-branch/non-bare-orig/.git/

$ touch file1

$ git add --all && git commit -m"Initial commit"
[master (root-commit) 9c33a5a] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1

$ touch file2

$ git add --all && git commit -m"Second commit"
[master 1f5673a] Second commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file2

$ git tag some_tag

$ touch file3

$ git add --all && git commit -m"Third commit"
[master 5bed6e7] Third commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file3

$ cd ../

$ git clone --bare non-bare-orig bare-clone
Cloning into bare repository 'bare-clone'...
done.

$ cd bare-clone/

$ git update-ref refs/heads/branch1 refs/heads/master

$ git update-ref refs/heads/branch2 some_tag

$ git update-ref refs/heads/branch3 9c33a5a

$ git branch -vv
  branch1 5bed6e7 Third commit
  branch2 1f5673a Second commit
  branch3 9c33a5a Initial commit
* master  5bed6e7 Third commit
Run Code Online (Sandbox Code Playgroud)


arc*_*rty 5

创建一个名为branchname的新分支(本地)

git branch branchname
Run Code Online (Sandbox Code Playgroud)

然后将它与远程存储库同步,如github(如果适用)

git push origin branchname
Run Code Online (Sandbox Code Playgroud)

并将其用于开发/使分支成为活动分支

git checkout branchname
Run Code Online (Sandbox Code Playgroud)

  • 在裸存储库中,这会导致错误:*致命:不是有效的对象名称:'master'.* (3认同)