如何在 Git 的裸仓库中创建分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8382212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a branch in a bare repository in Git
提问by Gerry Eng
I current have a bare repo thats acts as a central repo for my team. The bare repo currently only have a branch "master". How can I create more branches on the bare repo?
我目前有一个裸仓库,作为我团队的中央仓库。裸仓库目前只有一个分支“master”。如何在裸仓库上创建更多分支?
采纳答案by KingCrunch
Usually you don't create branches directly in the bare repository, but you push branches from one work repository to the bare
通常你不会直接在裸仓库中创建分支,而是将分支从一个工作仓库推送到裸仓库
git push origin myBranch
Update: Worth to mention
更新:值得一提
Like Paul Pladijs mentioned in the comments with
就像 Paul Pladijs 在评论中提到的一样
git push origin localBranchName:remoteBranchName
you push (and create, if not exists) your local branch to the remote with a different branch name, that your local one. And to make it complete with
您将本地分支推送(并创建,如果不存在)使用不同的分支名称(即本地分支名称)发送到远程。并使其完整
git push origin :remoteBranchName
you delete a remote branch.
你删除了一个远程分支。
回答by Dmitry Egorov
git update-ref refs/heads/new_branch refs/heads/master
In that bare repository if you have direct access to it. You may supply any reference (a tag for instance) or a commit in the last argument.
如果您可以直接访问它,则在该裸存储库中。您可以在最后一个参数中提供任何引用(例如标签)或提交。
Below is a test script:
下面是一个测试脚本:
$ 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
回答by arcyqwerty
To create a new branch (locally) called branchname
创建一个名为 branchname 的新分支(本地)
git branch branchname
Then to sync it with the remote repository like GitHub (if applicable)
然后将其与 GitHub 等远程存储库同步(如果适用)
git push origin branchname
And to use it for development / make the branch the active branch
并将其用于开发/使分支成为活动分支
git checkout branchname