如何将本地 Git 分支复制到远程仓库

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

How to copy a local Git branch to a remote repo

gitversion-controldistributed

提问by Coocoo4Cocoa

I've taken the following steps so far:

到目前为止,我已经采取了以下步骤:

  1. Cloned a remote Git repo
  2. Branched the master branch to an experimental
  3. edited/tested/committed code in the experimental branch
  1. 克隆了一个远程 Git 存储库
  2. 将主分支分支到一个实验
  3. 在实验分支中编辑/测试/提交的代码

Now, I'm not ready to merge experimental into master. I do however want to push it back to the remote repo as that's the repository I share with a few colleagues. I'd like for them to see what I've done in the experimental branch. I typically just access the remote repo via SSH.

现在,我还没有准备好将实验合并到主。但是,我确实想将它推回远程仓库,因为那是我与几个同事共享的存储库。我想让他们看看我在实验分支做了什么。我通常只通过 SSH 访问远程仓库。

How do I share my local branch on the remote repo, without affecting the remote repo's master branch?

如何在远程 repo 上共享我的本地分支,而不影响远程 repo 的 master 分支?

回答by VonC

According to git push manual page:

根据git push 手册页

 git push origin experimental

Find a ref that matches experimentalin the source repository (most likely, it would find refs/heads/experimental), and update the same ref (e.g. refs/heads/experimental) in origin repository with it.
If experimentaldid not exist remotely, it would be created.

experimental在源存储库中找到匹配的引用(很可能,它会找到refs/heads/experimental),并refs/heads/experimental用它更新源存储库中的相同引用(例如)。
如果experimental远程不存在,它将被创建

This is the same as:

这与:

git push origin experimental:refs/heads/experimental

Create the branch experimentalin the origin repository by copying the current experimentalbranch.
This form is only needed to create a new branch or tag in the remote repository when the local name and the remote name are different; otherwise, the ref name on its own will work.

experimental通过复制当前experimental分支在源存储库中创建分支。
这种形式只需要在本地名称和远程名称不同时在远程仓库中创建一个新的分支或标签;否则,ref 名称本身将起作用。

Or, like mentioned in git tip, you can set up a "Branch's Default Remote":

或者,就像git tip 中提到的,您可以设置一个“分支的默认远程”:

You can use git config to assign a default remote to a given branch. This default remote will be used to push that branch unless otherwise specified.

This is already done for you when you use git clone, allowing you to use git push without any arguments to push the local master branch to update the origin repository's master branch.

git config branch.<name>.remote <remote> 

can be used to specify this manually.

您可以使用 git config 将默认远程分配给给定分支。除非另有说明,否则此默认遥控器将用于推送该分支。

这在您使用 git clone 时已经为您完成,允许您使用不带任何参数的 git push 来推送本地主分支以更新源存储库的主分支。

git config branch.<name>.remote <remote> 

可用于手动指定。



Jansuggests (for git >= 1.7.0) the push -u(or push --set-upstream) option:

Jan建议(对于git >= 1.7.0push -u(或push --set-upstream)选项:

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

That way, you don't have to do any git config.

这样,您不必进行任何 git 配置。

git push -u origin experimental

回答by John Douthat

If the name of your branch is experimental, and the name of the remote is origin, then it's

如果您的分支名称是experimental,而远程名称是origin,那么它是

git push origin experimental

回答by brokenfoot

git push -u <remote-name> <branch-name>doesn't work if the newly created branch isn't spawned from the same repo, i.e. if you haven't created the new branch using git checkout -b new_branch, then this will not work.

git push -u <remote-name> <branch-name>如果新创建的分支不是从同一个 repo 生成的git checkout -b new_branch,则不起作用,即如果您没有使用 创建新分支,那么这将不起作用。

For eg, I had cloned two different repositories locally and I had to copy repo2/branch1 to repo1/ and then push it too.

例如,我在本地克隆了两个不同的存储库,我不得不将 repo2/branch1 复制到 repo1/ 然后也推送它。

Thislink helped me push my local branch (cloned from another repo) to my remote repo:

链接帮助我将本地分支(从另一个存储库克隆)推送到我的远程存储库:

回答by Ivan Prisyazhnyy

tl;dr

tl;博士

$ git push --set-upstream origin your_new_branch

more info

更多信息

after you have made few commits into your:

在您对以下内容进行少量提交之后:

$ git checkout -b your_new_branch
$ git add file
$ git commit -m "changed file"

you push your branch specifying an upstream into one of the remotes repositories like following:

您将指定上游的分支推送到远程存储库之一,如下所示:

$ git push --set-upstream REMOTE YOUR_BRANCH

remotes can be seen by

遥控器可以看到

$ git remote -v

usually, you will have single default remote origin. so you command would look like:

通常,您将有一个默认的 remote origin。所以你的命令看起来像:

$ git push --set-upstream origin your_new_branch

and all consequent pushes can be made just with git push.

并且所有随后的推送都可以使用git push.

回答by dylanfm

Here is the authoritative github page for github remote management http://github.com/guides/push-a-branch-to-github. It will help you answer all of your questions.

这里是github远程管理的权威github页面http://github.com/guides/push-a-branch-to-github。它将帮助您回答所有问题。