简体   繁体   English

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

[英]git, How to push local branch into the specific remote

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 DEV features to be pushed into origin/dev and 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/dev by default instead of origin/master 2)我应该在本地设置什么/何地/如何在默认情况下而不是origin/master进入origin/dev

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. 现在,您拥有Remote分支,然后您可以从该拉动的远程分支创建本地分支。 It will create a new local branch from your current Remote branch. 它将从您当前的Remote分支创建一个新的本地分支。

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. 其中<remote-name>通常是origin ,git为您克隆的远程提供的名称。 Your colleagues would then just pull that branch, and it's automatically created locally. 然后你的同事就会拉出那个分支,它会在本地自动创建。

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 -u <remote-name> <local-branch-name>

As described below, the -u option 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 . 您可以参考以下文档: https//git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging It has pretty good examples. 它有很好的例子。

Switch to the dev branch locally and then push to the dev branch on the origin remote: 在本地切换到dev分支,然后推送到origin远程的dev分支:

git checkout dev
git push -u origin dev

The -u option on git push sets upstream tracking such that when you are on the dev branch, git push and git pull automatically do the same thing as git push origin dev and git pull origin dev . git push上的-u选项设置上游跟踪,这样当你在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 --all to push all your branches to the origin remote. 如果我误解了你的问题并想要将你的所有分支机构用“dev ...”推送到各自的分支机构,你可以为每个分支执行上述步骤,或者你可以执行git push origin --all to将所有分支推送到原始远程。 So on origin, you'd have origin/dev , origin/dev_3_feature , etc. 所以在原点上,你有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. 如果我再次误解了你的问题并且想要将你的所有分支机构用“dev ...”推入一个远程分支,那么,我建议不要这样做。 It's probably best if you merge/rebase all your dev branches into one branch and then push that to origin. 如果将所有dev分支合并/重新绑定到一个分支然后将其推送到origin,这可能是最好的。 Let's say you want to use the one branch called 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_feature doesn't really tell you what the feature is. 作为最后一个注释,您可能需要一些更具描述性的分支名称用于将来的功能分支,因为像dev_second_feature这样的名称并不能真正告诉您该功能是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM