你如何推动一个Git分支(而没有其他分支)?

时间:2021-09-05 07:58:40

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推送到远程仓库,但我不想在主分支上推送更改。

Will a git push origin feature_x from my feature_x branch (feature_x branch already exists on remote) work?

我的feature_x分支(feature_x分支已经存在于远程)工作中的git push origin feature_x会不会?

I do not want to test this on my box, because I cannot push to master right now.

我不想在我的盒子上测试这个,因为我现在无法推动掌握。

4 个解决方案

#1


yes, just do the following

是的,只需执行以下操作即可

git checkout feature_x
git push origin feature_x

#2


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只会在你执行git push时更新当前(已检出)分支。

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.
  • 没有:除非明确给出refspec,否则不要推送任何东西(错误输出)。这主要是针对那些希望通过始终明确避免错误的人。

  • matching : Push all branches having the same name on both ends. (default option prior to Ver 1.7.11)
  • 匹配:推送两端具有相同名称的所有分支。 (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.
  • current:将当前分支推送到接收端同名的远程分支。适用于*和非*工作流程。

  • 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.
  • 简单:[自Ver 1.7.11起可用]在集中式工作流程中,如果上游分支的名称与本地分支不同,则像上游一样工作,增加安全性以拒绝推送。当推送到与通常拉出的遥控器不同的遥控器时,请作为当前工作。这是最安全的选择,适合初学者。此模式已成为Git 2.0中的默认模式。

#3


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

#4


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以将原始/ master的整个历史记录替换为foo的历史记录。

#1


yes, just do the following

是的,只需执行以下操作即可

git checkout feature_x
git push origin feature_x

#2


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只会在你执行git push时更新当前(已检出)分支。

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.
  • 没有:除非明确给出refspec,否则不要推送任何东西(错误输出)。这主要是针对那些希望通过始终明确避免错误的人。

  • matching : Push all branches having the same name on both ends. (default option prior to Ver 1.7.11)
  • 匹配:推送两端具有相同名称的所有分支。 (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.
  • current:将当前分支推送到接收端同名的远程分支。适用于*和非*工作流程。

  • 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.
  • 简单:[自Ver 1.7.11起可用]在集中式工作流程中,如果上游分支的名称与本地分支不同,则像上游一样工作,增加安全性以拒绝推送。当推送到与通常拉出的遥控器不同的遥控器时,请作为当前工作。这是最安全的选择,适合初学者。此模式已成为Git 2.0中的默认模式。

#3


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

#4


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以将原始/ master的整个历史记录替换为foo的历史记录。