如何创建远程 Git 分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1519006/
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 do you create a remote Git branch?
提问by Jesper R?nn-Jensen
I created a local branch which I want to 'push' upstream. There is a similar question here on Stack Overflow on how to track a newly created remote branch.
我创建了一个本地分支,我想将其“推”到上游。Stack Overflow 上有一个关于如何跟踪新创建的远程分支的类似问题。
However, my workflow is slightly different. FirstI want to create a local branch, and I will only push it upstream when I'm satisfied and want to share my branch.
但是,我的工作流程略有不同。首先我想创建一个本地分支,只有当我满意并想要共享我的分支时,我才会将其推到上游。
- How would I do that? (my google searches did not seem to come up with anything).
- How would I tell my colleagues to pull it from the upstream repository?
- 我该怎么做?(我的谷歌搜索似乎没有找到任何东西)。
- 我如何告诉我的同事从上游存储库中提取它?
UPDATE With Git 2.0 there is a simpler answerI have written below: https://stackoverflow.com/a/27185855/109305
更新对于 Git 2.0,我在下面写了一个更简单的答案:https: //stackoverflow.com/a/27185855/109305
采纳答案by Jesper R?nn-Jensen
Simple Git 2.0+ solution:
简单的 Git 2.0+ 解决方案:
As of Git 2.0 the behaviour has become simpler:
从Git 2.0 开始,行为变得更简单:
You can configure git with push.default = current
to make life easier:
您可以配置 gitpush.default = current
使生活更轻松:
I added this so now I can just push a new branch upstream with
我添加了这个,所以现在我可以向上游推送一个新分支
$ git push -u
-u
will track remote branch of same name. Now with this configuration you will auto-guess the remote reference to git push. From git.config documentation:
-u
将跟踪同名的远程分支。现在使用此配置,您将自动猜测对 git push 的远程引用。从git.config 文档:
push.default
Defines the action git push should take if no refspec is explicitly given.
push.default = current
- push the current branch to update a branch with the same name on the receiving end. Works in both central and non-central workflows.
push.default
如果没有明确给出 refspec,定义 git push 应该采取的行动。
push.default = current
- 推送当前分支以更新接收端的同名分支。适用于中央和非中央工作流。
For me, this is a good simplification of my day-to-day Git workflow. The configuration setting takes care of the 'usual' use case where you add a branch locally and want to create it remotely. Also, I can just as easily create local branches from remotes by just doing git co remote_branch_name
(as opposed to using --set-upstream-to
flag).
对我来说,这是我日常 Git 工作流程的一个很好的简化。配置设置处理“通常”用例,您在本地添加分支并希望远程创建它。此外,我可以通过执行git co remote_branch_name
(而不是使用--set-upstream-to
标志)轻松地从远程创建本地分支。
I know this question and the accepted answers are rather old, but the behaviour has changed so that now configuration options exists to make your workflow simpler.
我知道这个问题和接受的答案相当陈旧,但行为已发生变化,因此现在存在配置选项以使您的工作流程更简单。
To add to your global Git configuration, run this on the command line:
要添加到全局 Git 配置,请在命令行上运行:
$ git config --global push.default current
回答by Ikke
First, you create your branch locally:
首先,在本地创建分支:
git checkout -b <branch-name> # Create a new branch and check it out
The remote branch is automatically created when you push it to the remote server. So when you feel ready for it, you can just do:
当您将其推送到远程服务器时,会自动创建远程分支。因此,当您准备好迎接它时,您可以这样做:
git push <remote-name> <branch-name>
Where <remote-name>
is typically origin
, the name which git gives to the remote you cloned from. Your colleagues would then just pull that branch, and it's automatically created locally.
哪里<remote-name>
通常是origin
git 为您克隆的远程设备提供的名称。然后你的同事会拉那个分支,它会在本地自动创建。
Note however that formally, the format is:
但请注意,正式的格式是:
git push <remote-name> <local-branch-name>:<remote-branch-name>
But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only :<remote-branch-name>
(with the colon), or the remote branch will be deleted!
但是当您省略一个时,它假定两个分支名称相同。话虽如此,请注意,不要犯仅指定:<remote-branch-name>
(带冒号)的严重错误,否则远程分支将被删除!
So that a subsequent git pull
will know what to do, you might instead want to use:
为了让后续git pull
知道该怎么做,您可能想要使用:
git push --set-upstream <remote-name> <local-branch-name>
As described below, the --set-upstream
option sets up an upstream branch:
如下所述,该--set-upstream
选项设置了一个上游分支:
For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands.
对于每个最新或成功推送的分支,添加上游(跟踪)引用,由无参数 git-pull(1) 和其他命令使用。
回答by dseminara
First, you must create your branch locally
首先,您必须在本地创建您的分支
git checkout -b your_branch
After that, you can work locally in your branch, when you are ready to share the branch, push it. The next command push the branch to the remote repository origin and tracks it
之后,您可以在您的分支本地工作,当您准备好共享分支时,推送它。下一个命令将分支推送到远程存储库源并对其进行跟踪
git push -u origin your_branch
Teammates can reach your branch, by doing:
队友可以通过执行以下操作到达您的分支:
git fetch
git checkout origin/your_branch
You can continue working in the branch and pushing whenever you want without passing arguments to git push (argumentless git push will push the master to remote master, your_branch local to remote your_branch, etc...)
您可以继续在分支中工作并随时推送,而无需向 git push 传递参数(无参数的 git push 会将 master 推送到远程 master,将 your_branch 本地推送到远程 your_branch 等...)
git push
Teammates can push to your branch by doing commits and then push explicitly
队友可以通过提交然后明确推送到你的分支
... work ...
git commit
... work ...
git commit
git push origin HEAD:refs/heads/your_branch
Or tracking the branch to avoid the arguments to git push
或者跟踪分支以避免 git push 的参数
git checkout --track -b your_branch origin/your_branch
... work ...
git commit
... work ...
git commit
git push
回答by Lucian
As stated in the previous answers,
如之前的回答所述,
git push <remote-name> <local-branch-name>:<remote-branch-name>
is enough for pushing a local branch.
足以推动本地分支。
Your colleagues, can pull all remote branches (including new ones) with this command:
您的同事可以使用以下命令拉取所有远程分支(包括新分支):
git remote update
Then, to make changes on the branch, the usual flow:
然后,要在分支上进行更改,通常的流程是:
git checkout -b <local-branch-name> <remote-name>/<remote-branch-name>
回答by Zenexer
Create a new branch locally based on the current branch:
根据当前分支在本地新建一个分支:
git checkout -b newbranch
Commit any changes as you normally would. Then, push it upstream:
像往常一样提交任何更改。然后,将其推向上游:
git push -u origin HEAD
This is a shortcut to push the current branch to a branch of the same name on origin
and track it so that you don't need to specify origin HEAD
in the future.
这是将当前分支推送到同名分支origin
并跟踪它的快捷方式,以便您origin HEAD
将来无需指定。
回答by sreekumar
If you want to create a branch from the current branch
如果要从当前分支创建分支
git checkout -b {your_local_branch_name}
you want a branch from a remote branch, you can try
你想要一个远程分支的分支,你可以试试
git checkout -b {your_local_branch_name} origin/<remote_branch_name>
If you are done with changes you can add the file.
如果您完成了更改,您可以添加文件。
git add -A or git add <each_file_names>
Then do a commit locally
然后在本地做一个提交
git commit -m 'your commit message'
When you want to push to remote repo
当你想推送到远程仓库时
git push -u origin <your_local_branch_name>
All together will be
都在一起会
git checkout -b bug_fixes
or If you want to create a branch from a remote branch say development
或者如果您想从远程分支创建分支,请说development
git checkout -b bug_fixesorigin/development
git checkout -b bug_fixesorigin/ development
You can push to the branch to remote repo by
您可以通过以下方式推送到分支到远程仓库
git push -u origin bug_fixes
Anytime you want to update your branch from any other branch say master.
无论何时您想从任何其他分支更新您的分支,请说master。
git pull origin master
.
git pull origin master
.
回答by Tassadar
If you wanna actually just create remote branch without having the local one, you can do it like this:
如果您实际上只想创建远程分支而没有本地分支,您可以这样做:
git push origin HEAD:refs/heads/foo
It pushes whatever is your HEAD to branch foothat did not exist on the remote.
它会将您的 HEAD 推送到远程不存在的分支foo。
回答by sapy
Easiest Solution... Drumm Roll... git version 2.10.1 (Apple Git-78)
最简单的解决方案...鼓... git 版本 2.10.1 (Apple Git-78)
1) git checkout -b localBranchNameThatDoesNotExistInRemote
2) Do your changes, and do a git commit
3) git push origin localBranchNameThatDoesNotExistInRemote --force
N.B. - The branch you just created in your local environment, and the remote non-existing branch where you are trying to push, must have the same name.
注意 - 您刚刚在本地环境中创建的分支,以及您尝试推送的远程不存在的分支,必须具有相同的名称。
回答by Javier C.
[Quick Answer]
[快速回答]
You can do it in 2 steeps:
您可以在 2 个陡坡中完成:
1.Use the checkout
for create the local branch:
1.使用checkout
for 创建本地分支:
git checkout -b yourBranchName
2.Use the push
command to autocreate the branch and send the code to the remote repository:
2.使用push
命令自动创建分支并将代码发送到远程存储库:
git push -u origin yourBanchName
There are mutiple ways to do this but I think that this way is really simple.
有多种方法可以做到这一点,但我认为这种方法非常简单。
回答by ipegasus
First you create the branch locally:
首先在本地创建分支:
git checkout -b your_branch
And then to create the branch remotely:
然后远程创建分支:
git push --set-upstream origin your_branch
Note: This works on the latests versions of git:
注意:这适用于最新版本的 git:
$ git --version
git version 2.3.0
Cheers!
干杯!