你如何只推送一个 Git 分支(而不推送其他分支)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/820178/
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 push just a single Git branch (and no other branches)?
提问by Rafael Mueller
I am working on a local git repository. There are two branches, master
and feature_x
.
我正在处理本地 git 存储库。有两个分支,master
和feature_x
。
I want to push feature_x
to the remote repo, but I do not want to push the changes on the master
branch.
我想推feature_x
送到远程仓库,但我不想推送master
分支上的更改。
Will a git push origin feature_x
from my feature_x
branch (feature_x
branch already exists on remote) work?
会git push origin feature_x
从我的feature_x
分支(feature_x
分支已经存在于远程)的工作?
I do not want to test this on my box, because I cannot push to master right now.
我不想在我的盒子上测试这个,因为我现在无法推动掌握。
回答by cpjolicoeur
yes, just do the following
是的,只需执行以下操作
git checkout feature_x
git push origin feature_x
回答by Karthik Bose
By default git push
updates all the remote branches. But you can configure git to update only the current branch to it's upstream.
默认情况下git push
更新所有远程分支。但是你可以配置 git 只更新当前分支到它的上游。
git config push.default upstream
It means git will update only the current (checked out) branch when you do git push.
这意味着当你执行 git push 时,git 只会更新当前(签出)的分支。
Other valid options are:
其他有效选项是:
nothing
: Do not push anything (error out) unless a refspec is explicitly given. This is primarily meant for people who want to avoid mistakes by always being explicit.matching
: Push all branches having the same nameon both ends. (default option prior to Ver 1.7.11)upstream
: Push the current branch to its upstream branch. This mode only makes sense if you are pushing to the same repository you would normally pull from (i.e. central workflow). No need to have the same namefor local and remote branch.tracking
: Deprecated, useupstream
instead.current
: Push the current branch to the remote branch of the same nameon the receiving end. Works in both central and non-central workflows.simple
: [available since Ver 1.7.11] in centralized workflow, work likeupstream
with an added safety to refuse to push if the upstream branch's name is different from the local one. When pushing to a remote that is different from the remote you normally pull from, work ascurrent
. This is the safest option and is suited for beginners. This mode has become the default in Git 2.0.
nothing
:除非明确给出 refspec,否则不要推送任何内容(错误输出)。这主要适用于希望通过始终明确避免错误的人。matching
: 推送两端同名的所有分支。(Ver 1.7.11 之前的默认选项)upstream
:将当前分支推送到其上游分支。此模式仅在您推送到您通常从中提取的同一存储库(即中央工作流)时才有意义。本地和远程分支不需要具有相同的名称。tracking
: 已弃用,请upstream
改用。current
:将当前分支推送到接收端的同名远程分支。适用于中央和非中央工作流。simple
: [自 1.7.11 版起可用] 在集中式工作流中,upstream
如果上游分支的名称与本地分支的名称不同,则使用增加的安全性来拒绝推送。当推送到与您通常从中拉出的遥控器不同的遥控器时,请使用current
. 这是最安全的选择,适合初学者。此模式已成为 Git 2.0 中的默认模式。
回答by Bhaskar
Minor update on top of Karthik Bose's answer- you can configure git globally, to affect all of your workspaces to behave that way:
在Karthik Bose 的回答之上的小更新- 您可以全局配置 git,以影响所有工作区的行为方式:
git config --global push.default upstream
回答by CodeKid
So let's say you have a local branch foo, a remote called origin and a remote branch origin/master.
因此,假设您有一个本地分支 foo、一个名为 origin 的远程分支和一个远程分支 origin/master。
To push the contents of foo to origin/master, you first need to set its upstream:
要将 foo 的内容推送到 origin/master,首先需要设置其上游:
git checkout foo
git branch -u origin/master
Then you can push to this branch using:
然后你可以使用以下方法推送到这个分支:
git push origin HEAD:master
In the last command you can add --force to replace the entire history of origin/master with that of foo.
在最后一个命令中,您可以添加 --force 以将 origin/master 的整个历史记录替换为 foo 的历史记录。