我正在一个本地git仓库工作。有两个分支,master和feature_x。

我想将feature_x推到远程回购,但不想将更改推到主分支上。

git从我的feature_x分支(feature_x分支已经存在于远程)推送origin feature_x工作吗?

我不想在我的盒子上测试这个,因为我现在不能推到master。


当前回答

假设你有一个本地分支foo,一个远程分支origin和一个远程分支origin/master。

要将foo的内容推到origin/master,你首先需要设置它的upstream:

git checkout foo
git branch -u origin/master

然后你可以使用以下命令推到这个分支:

git push origin HEAD:master

在最后一个命令中,你可以添加——force来用foo替换origin/master的整个历史。

其他回答

是的,按照下面的步骤做

git checkout feature_x
git push origin feature_x

默认情况下,git push会更新所有远程分支。但是你可以配置git只更新当前分支到它的上游。

git config push.default upstream

这意味着当你执行git push时,git只会更新当前的(签出的)分支。

其他有效选项包括:

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 name on 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 name for local and remote branch. tracking : Deprecated, use upstream instead. current : Push the current branch to the remote branch of the same name on the receiving end. Works in both central and non-central workflows. simple : [available since Ver 1.7.11] in centralized workflow, work like upstream 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 as current. This is the safest option and is suited for beginners. This mode has become the default in Git 2.0.

不管你有什么配置,都要推送你当前的分支:

git push origin $(git branch --show-current)

在Karthik Bose回答的基础上做了一个小更新——你可以全局配置git,让你所有的工作区都像这样工作:

git config --global push.default upstream

假设你有一个本地分支foo,一个远程分支origin和一个远程分支origin/master。

要将foo的内容推到origin/master,你首先需要设置它的upstream:

git checkout foo
git branch -u origin/master

然后你可以使用以下命令推到这个分支:

git push origin HEAD:master

在最后一个命令中,你可以添加——force来用foo替换origin/master的整个历史。