如何将新的本地分支推送到远程 Git 存储库并对其进行跟踪?

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

How do I push a new local branch to a remote Git repository and track it too?

gitrepositorygit-branchgit-pushgit-remote

提问by Roni Yaniv

I want to be able to do the following:

我希望能够做到以下几点:

  1. Create a local branch based on some other (remote or local) branch (via git branchor git checkout -b)

  2. Push the local branch to the remote repository (publish), but make it trackable so git pulland git pushwill work immediately.

  1. 基于其他(远程或本地)分支(通过git branchgit checkout -b)创建本地分支

  2. 将本地分支推送到远程存储库(发布),但使其可跟踪git pullgit push立即工作。

How do I do that?

我怎么做?

I know about --set-upstreamin Git 1.7, but that is a post-creation action. I want to find a way to make a similar change when pushing the branch to the remote repository.

我知道--set-upstream在 Git 1.7 中,但这是创建后的操作。我想找到一种在将分支推送到远程存储库时进行类似更改的方法。

回答by Daniel Ruoso

In Git 1.7.0 and later, you can checkout a new branch:

在 Git 1.7.0 及更高版本中,您可以签出一个新分支:

git checkout -b <branch>

Edit files, add and commit. Then push with the -u(short for --set-upstream)option:

编辑文件,添加和提交。然后-u(简写--set-upstream选项推动

git push -u origin <branch>

Git will set up the tracking information during the push.

Git 会在推送过程中设置跟踪信息。

回答by ErichBSchulz

If you are not sharing your repo with others, this is useful to push allyour branches to the remote, and --set-upstreamtracking correctly for you:

如果您不与他人共享您的存储库,这有助于将您的所有分支推送到远程,并--set-upstream为您正确跟踪:

git push --all -u

(Not exactly what the OP was asking for, but this one-liner is pretty popular)

(不完全是 OP 所要求的,但这种单线很受欢迎)

If you are sharing your repo with others this isn't really good form as you will clog up the repo with all your dodgy experimental branches.

如果您与其他人共享您的 repo,这不是一个很好的形式,因为您会用所有狡猾的实验分支堵塞 repo。

回答by Lohrun

Prior to the introduction of git push -u, there was no git pushoption to obtain what you desire. You had to add new configuration statements.

在引入 之前git push -u,没有git push选择来获得您想要的东西。您必须添加新的配置语句。

If you create a new branch using:

如果您使用以下方法创建新分支:

$ git checkout -b branchB
$ git push origin branchB:branchB

You can use the git configcommand to avoid editing directly the .git/configfile.

您可以使用该git config命令避免直接编辑.git/config文件。

$ git config branch.branchB.remote origin
$ git config branch.branchB.merge refs/heads/branchB

Or you can edit manually the .git/configfile to had tracking information to this branch.

或者,您可以手动编辑该.git/config文件以获取该分支的跟踪信息。

[branch "branchB"]
    remote = origin
    merge = refs/heads/branchB

回答by piyushmandovra

Simply put, to create a new localbranch, do:

简单地说,要创建一个新的本地分支,请执行以下操作:

git branch <branch-name>

To push it to the remoterepository, do:

要将其推送到远程存储库,请执行以下操作:

git push -u origin <branch-name>

回答by bg17aw

A slight variation of the solutions already given here:

这里已经给出的解决方案略有不同:

  1. Create a local branch based on some other (remote or local) branch:

    git checkout -b branchname
    
  2. Push the local branch to the remote repository (publish), but make it trackable so git pulland git pushwill work immediately

    git push -u origin HEAD
    

    Using HEADis a "handy way to push the current branch to the same name on the remote". Source: https://git-scm.com/docs/git-pushIn Git terms, HEAD (in uppercase) is a reference to the top of the current branch (tree).

    The -uoption is just short for --set-upstream. This will add an upstream tracking reference for the current branch. you can verify this by looking in your .git/config file:

    Enter image description here

  1. 基于其他(远程或本地)分支创建本地分支:

    git checkout -b branchname
    
  2. 将本地分支推送到远程存储库(发布),但使其可跟踪git pullgit push立即工作

    git push -u origin HEAD
    

    使用HEAD是一种“将当前分支推送到远程同名的便捷方式”。来源:https: //git-scm.com/docs/git-push在 Git 术语中,HEAD(大写)是对当前分支(树)顶部的引用。

    -u选项只是--set-upstream. 这将为当前分支添加一个上游跟踪引用。您可以通过查看 .git/config 文件来验证这一点:

    在此处输入图片说明

回答by Arda

I simply do

我只是做

git push -u origin localBranch:remoteBranchToBeCreated

over an already cloned project.

在一个已经克隆的项目上。

Git creates a new branch named remoteBranchToBeCreatedunder my commits I did in localBranch.

Git 创建了一个新分支,命名为remoteBranchToBeCreated我在localBranch.

Edit: this changes your current local branch's (possibly named localBranch) upstream to origin/remoteBranchToBeCreated. To fix that, simply type:

编辑:这会将您当前本地分支的(可能命名的localBranch)上游更改为origin/remoteBranchToBeCreated. 要解决这个问题,只需键入:

git branch --set-upstream-to=origin/localBranch

So your current local branch now tracks origin/localBranchback.

因此,您当前的本地分支现在可以origin/localBranch回溯。

回答by VP.

I suppose that you have already cloned a project like:

我想你已经克隆了一个项目,如:

git clone http://github.com/myproject.git
  1. Then in your local copy, create a new branch and check it out:

    git checkout -b <newbranch>
    
  2. Supposing that you made a "git bare --init" on your server and created the myapp.git, you should:

    git remote add origin ssh://example.com/var/git/myapp.git
    git push origin master
    
  3. After that, users should be able to

    git clone http://example.com/var/git/myapp.git
    
  1. 然后在您的本地副本中,创建一个新分支并检查它:

    git checkout -b <newbranch>
    
  2. 假设你在你的服务器上创建了一个“git bare --init”并创建了 myapp.git,你应该:

    git remote add origin ssh://example.com/var/git/myapp.git
    git push origin master
    
  3. 之后,用户应该能够

    git clone http://example.com/var/git/myapp.git
    

NOTE:I'm assuming that you have your server up and running. If it isn't, it won't work. A good how-to is here.

注意:我假设您的服务器已启动并正在运行。如果不是,它将不起作用。一个很好的方法在这里

ADDED

添加

Add a remote branch:

添加远程分支:

git push origin master:new_feature_name

Check if everything is good (fetch origin and list remote branches):

检查一切是否正常(获取源并列出远程分支):

git fetch origin
git branch -r

Create a local branch and track the remote branch:

创建本地分支并跟踪远程分支:

git checkout -tb new_feature_name origin/new_feature_name

Update everything:

更新一切:

git pull

回答by Tobias Kienzler

editOutdated, just use git push -u origin $BRANCHNAME

编辑过时,只需使用git push -u origin $BRANCHNAME



Use git publish-branchfrom William's miscellaneous Git tools(gitorious repoand clone).

使用git publish-branch威廉的杂项的Git工具gitorious回购克隆)。

OK, no Ruby, so - ignoring the safeguards! - take the last three lines of the script and create a bash script, git-publish-branch:

好吧,没有 Ruby,所以 - 忽略保护措施!- 取出脚本的最后三行并创建一个 bash 脚本,git-publish-branch

#!/bin/bash
REMOTE= # Rewrite this to make it optional...
BRANCH=
# Uncomment the following line to create BRANCH locally first
#git checkout -b ${BRANCH}
git push ${ORIGIN} ${BRANCH}:refs/heads/${BRANCH} &&
git config branch.${BRANCH}.remote ${REMOTE} &&
git config branch.${BRANCH}.merge refs/heads/${BRANCH}

Then run git-publish-branch REMOTENAME BRANCHNAME, where REMOTENAME is usually origin (you may modify the script to take origin as default, etc...)

然后运行git-publish-branch REMOTENAME BRANCHNAME,其中 REMOTENAME 通常是原点(您可以修改脚本以将原点作为默认值等...)

回答by cptHyman

To create a new branch by branching off from an existing branch

通过从现有分支分支来创建新分支

git checkout -b <new_branch>

and then push this new branch to repository using

然后使用这个新分支推送到存储库

git push -u origin <new_branch>

This creates and pushes all local commits to a newly created remote branch origin/<new_branch>

这会创建并将所有本地提交推送到新创建的远程分支 origin/<new_branch>

回答by Fadid

For GitLab version prior to 1.7, use:

对于 1.7 之前的 GitLab 版本,请使用:

git checkout -b name_branch

(name_branch, ex: master)

(name_branch,例如:master

To push it to the remote repository, do:

要将其推送到远程存储库,请执行以下操作:

git push -u origin name_new_branch

(name_new_branch, example: feature)

(name_new_branch,例如:feature