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

时间:2022-04-04 16:24:34

I've taken the following steps so far:

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

  1. Cloned a remote Git repo
  2. 克隆了一个远程Git仓库

  3. Branched the master branch to an experimental
  4. 将主分支分支到实验室

  5. edited/tested/committed code in the experimental branch
  6. 在实验分支中编辑/测试/提交的代码

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?

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

5 个解决方案

#1


According to git push manual page:

根据git push手册页:

 git push origin experimental

Find a ref that matches experimental in 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 experimental did not exist remotely, it would be created.

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

This is the same as:

这与:

git push origin experimental:refs/heads/experimental

Create the branch experimental in the origin repository by copying the current experimental branch.
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.

通过复制当前实验分支在原始存储库中创建分支实验。仅当本地名称和远程名称不同时,才需要此表单在远程存储库中创建新分支或标记;否则,引用名称本身就可以使用。

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.

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

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 clone时,已经为您完成了此操作,允许您使用git push而不使用任何参数来推送本地主分支以更新源存储库的主分支。

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

can be used to specify this manually.

可用于手动指定。


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

Jan建议(对于git> = 1.7.0)push -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

#2


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

如果您的分支的名称是实验性的,并且远程的名称是原点,那么它就是

git push origin experimental

#3


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.

如果新创建的分支不是从同一个repo中生成的,即如果你没有使用git checkout -b new_branch创建新分支,则git push -u 不起作用这不行。

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 /然后再推送它。

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

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

#4


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。它将帮助您回答所有问题。

#5


tl;dr

$ 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:

通常,您将拥有单个默认远程原点。所以你的命令看起来像:

$ git push --set-upstream origin your_new_branch

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

并且所有随后的推送都可以通过git push进行。

#1


According to git push manual page:

根据git push手册页:

 git push origin experimental

Find a ref that matches experimental in 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 experimental did not exist remotely, it would be created.

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

This is the same as:

这与:

git push origin experimental:refs/heads/experimental

Create the branch experimental in the origin repository by copying the current experimental branch.
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.

通过复制当前实验分支在原始存储库中创建分支实验。仅当本地名称和远程名称不同时,才需要此表单在远程存储库中创建新分支或标记;否则,引用名称本身就可以使用。

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.

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

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 clone时,已经为您完成了此操作,允许您使用git push而不使用任何参数来推送本地主分支以更新源存储库的主分支。

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

can be used to specify this manually.

可用于手动指定。


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

Jan建议(对于git> = 1.7.0)push -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

#2


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

如果您的分支的名称是实验性的,并且远程的名称是原点,那么它就是

git push origin experimental

#3


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.

如果新创建的分支不是从同一个repo中生成的,即如果你没有使用git checkout -b new_branch创建新分支,则git push -u 不起作用这不行。

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 /然后再推送它。

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

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

#4


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。它将帮助您回答所有问题。

#5


tl;dr

$ 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:

通常,您将拥有单个默认远程原点。所以你的命令看起来像:

$ git push --set-upstream origin your_new_branch

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

并且所有随后的推送都可以通过git push进行。