git,如何将本地分支推送到特定的远程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39128477/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:12:40  来源:igfitidea点击:

git, How to push local branch into the specific remote

gitgit-branchgit-remote

提问by Eugene

could you explain me how to push local branch to the specific remote branch

你能解释一下如何将本地分支推送到特定的远程分支吗

$ git branch -vv 
dev 4d46c96 [origin/dev] Merge branch '1783' into dev
dev_3_feature 226b914 second commit in dev_3_feature
dev_second_feature 6b5f10f second commit in dev_2_feature
master baf5fc0 [origin/master: ahead 1] master feature
* myFeature da5cc64 second commit in dev_1_feature
test 334cf7e commiting my super changes locally

1) i want my DEVfeatures to be pushed into origin/devand stay there as branches, how can i do that ?

1)我希望我的DEV功能被推入origin/dev并作为分支留在那里,我该怎么做?

2) what/where/how should i set up locally to push into origin/devby default instead of origin/master

2)我应该在本地设置什么/在哪里/如何origin/dev默认推送而不是origin/master

回答by Ajay P. Prajapati

Update :

更新 :

So, generally when you work with Remote, First of all you need to pull the repository or branch.

所以,一般在使用 Remote 时,首先需要拉取仓库或分支。

If its repository then

如果它的存储库那么

git pull origin

if its branch then

如果它的分支然后

git pull origin <yourRemoteBranchName>

after you pulled it, it will be on your machine. Now your current branch is yourRemoteBranchName.

拉动它后,它将在您的机器上。现在您当前的分支是yourRemoteBranchName.



Now, you have above Remote branch, then you can create your local branch from that pulled remote branch. It will create a new local branch from your current Remote branch.

现在,你有了上面的远程分支,然后你可以从那个拉取的远程分支创建你的本地分支。它将从您当前的远程分支创建一个新的本地分支。

git checkout -b your_branch

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>通常是origingit 为您克隆的远程设备提供的名称。然后你的同事会拉那个分支,它会在本地自动创建。

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 pullwill know what to do, you might instead want to use:

为了让后续git pull知道该怎么做,您可能想要使用:

git push -u <remote-name> <local-branch-name>

As described below, the -uoption sets up an upstream branch:

如下所述,该-u选项设置了一个上游分支:

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) 和其他命令使用。

If you want to merge directly with upstream branch,

如果你想直接与上游分支合并,

git merge branchName

You can refer to this documentation : https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging. It has pretty good examples.

你可以参考这个文档:https: //git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging。它有很好的例子。

回答by Lawrence Lee

Switch to the dev branch locally and then push to the dev branch on the origin remote:

在本地切换到 dev 分支,然后推送到 origin remote 上的 dev 分支:

git checkout dev
git push -u origin dev

The -uoption on git pushsets upstream tracking such that when you are on the dev branch, git pushand git pullautomatically do the same thing as git push origin devand git pull origin dev.

-u期权git push套上游,当你在Dev分支跟踪这样,git pushgit pull自动做同样的事情,git push origin devgit pull origin dev



If I misunderstood your question and you want to push all your branches with "dev..." into their respective branches on origin, you can do the above step for each of those branches, or you can do git push origin --allto push allyour branches to the origin remote. So on origin, you'd have origin/dev, origin/dev_3_feature, etc.

如果我误解了您的问题并且您想将所有带有“dev...”的分支推送到它们各自的原始分支中,您可以对每个分支执行上述步骤,或者您可以git push origin --all所有分支推送到原产地远程。因此,对原产地,你就会有origin/devorigin/dev_3_feature



If I doubly misunderstood your question and you want to push all your branches with "dev..." into a single remote branch, well, I'd advise not doing that. It's probably best if you merge/rebase all your dev branches into one branch and then push that to origin. Let's say you want to use the one branch called dev:

如果我双重误解了您的问题,并且您想将所有带有“dev...”的分支推送到一个远程分支中,那么我建议不要这样做。如果您将所有开发分支合并/重新设置为一个分支,然后将其推送到原点,这可能是最好的。假设您要使用名为 的一个分支dev

git checkout dev
git merge dev_3_feature
git merge dev_second_feature
git push -u origin dev

After each merge, you might have to resolve merge conflicts, so be warned.

每次合并后,您可能必须解决合并冲突,因此请注意。

As a last note, you may want some more descriptive branch names for future feature branches, as names like dev_second_featuredoesn't really tell you what the feature is.

最后要注意的是,您可能需要一些更具描述性的分支名称用于未来的功能分支,因为名称 likedev_second_feature并不能真正告诉您该功能是什么。