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

我如何:

  1. 从另一个分支创建一个本地分支(通过git branchgit checkout -b)。

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

5170255 次浏览

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

如果您使用以下命令创建新分支:

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

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

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

或者您可以手动编辑.git/config文件以将跟踪信息添加到此分支:

[branch "branchB"]remote = originmerge = refs/heads/branchB

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


William的各种Git工具使用git publish-branch

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

#!/bin/bashREMOTE=$1 # Rewrite this to make it optional...BRANCH=$2# 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}

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

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

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

    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.gitgit push origin master
  3. After that, users should be able to

    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

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

git fetch origingit branch -r

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

git checkout -tb new_feature_name origin/new_feature_name

更新一切:

git pull

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

git checkout -b <branch>

编辑文件、添加和提交。然后使用#0(#1的缩写)推送选项:

git push -u origin <branch>

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

如果您没有与他人共享您的repo,这对于将所有分支推送到远程很有用,并且--set-upstream为您正确跟踪:

git push --all -u

(不完全是OP所要求的,但这一句话很受欢迎)

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

简单地说,要创建一个新的当地分支,请执行:

git branch <branch-name>

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

git push -u origin <branch-name>

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

git checkout -b <new_branch>

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

git push -u origin <new_branch>

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

我做了一个别名,这样每当我创建一个新分支时,它都会相应地推送和跟踪远程分支。我将以下块放入.bash_profile文件中:

# Create a new branch, push to origin and track that remote branchpublishBranch() {git checkout -b $1git push -u origin $1}alias gcb=publishBranch

用法:只需键入gcb thuy/do-sth-koolthuy/do-sth-kool是我的新分支名称。

这里已经给出的解决方案的轻微变化:

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

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

    git push -u origin HEAD

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

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

    在此处输入图像描述

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

git checkout -b name_branch

(name_branch:master

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

git push -u origin name_new_branch

(name_new_branch,例如:feature

我只是做

git push -u origin localBranch:remoteBranchToBeCreated

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

Git在我在localBranch中的提交下创建了一个名为remoteBranchToBeCreated的新分支。

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

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

git branch -u origin/localBranch

所以您当前的本地分支现在跟踪origin/localBranch

基于这里的答案,我将这个过程包装为一个简单的Bash脚本,当然也可以用作Git别名。

对我来说重要的补充是,这提示我在提交之前运行单元测试,并默认传入当前分支名称。

$ git_push_new_branch.sh
Have you run your unit tests yet? If so, pass OK or a branch name, and try again
usage: git_push_new_branch {OK|BRANCH_NAME}
e.g.
git_push_new_branch           -> Displays prompt reminding you to run unit testsgit_push_new_branch OK        -> Pushes the current branch as a new branch to the origingit_push_new_branch MYBRANCH  -> Pushes branch MYBRANCH as a new branch to the origin

git_push_new_branch.sh

function show_help(){IT=$(cat <<EOF
Have you run your unit tests yet? If so, pass OK or a branch name, and try again
usage: git_push_new_branch {OK|BRANCH_NAME}
e.g.
git_push_new_branch.sh           -> Displays prompt reminding you to run unit testsgit_push_new_branch.sh OK        -> Pushes the current branch as a new branch to the origingit_push_new_branch.sh MYBRANCH  -> Pushes branch MYBRANCH as a new branch to the origin
)echo "$IT"exit}
if [ -z "$1" ]thenshow_helpfi
CURR_BRANCH=$(git rev-parse --abbrev-ref HEAD)if [ "$1" == "OK" ]thenBRANCH=$CURR_BRANCHelseBRANCH=${1:-$CURR_BRANCH}fi
git push -u origin $BRANCH

你可以在两个步骤中做到这一点:

1.使用checkout创建本地分支:

git checkout -b yourBranchName

如你所愿与你的分支机构合作。

2.使用push命令自动创建分支并将代码发送到远程存储库:

git push -u origin yourBanchName

有很多方法可以做到这一点,但我认为这种方法非常简单。

为了获得最大的灵活性,您可以使用自定义git命令。例如,在$PATH的某个地方以git-publish的名称创建以下Python脚本并使其可执行:

#!/usr/bin/env python3
import argparseimport subprocessimport sys

def publish(args):return subprocess.run(['git', 'push', '--set-upstream', args.remote, args.branch]).returncode

def parse_args():parser = argparse.ArgumentParser(description='Push and set upstream for a branch')parser.add_argument('-r', '--remote', default='origin',help="The remote name (default is 'origin')")parser.add_argument('-b', '--branch', help='The branch name (default is whatever HEAD is pointing to)',default='HEAD')return parser.parse_args()

def main():args = parse_args()return publish(args)

if __name__ == '__main__':sys.exit(main())

然后git publish -h将向您显示使用信息:

usage: git-publish [-h] [-r REMOTE] [-b BRANCH]
Push and set upstream for a branch
optional arguments:-h, --help            show this help message and exit-r REMOTE, --remote REMOTEThe remote name (default is 'origin')-b BRANCH, --branch BRANCHThe branch name (default is whatever HEAD is pointing to)

我认为这是最简单的别名,添加到您的~/.gitconfig

[alias]publish-branch = !git push -u origin $(git rev-parse --abbrev-ref HEAD)

你只管跑

git publish-branch

然后…它就出版了

将本地变更推送到新功能分支的完整Git工作流程如下所示

调出所有远程分支

git pull --all

现在列出所有分支

git branch -a

签出或创建分支(将<feature branch>替换为您的分支名称):

git checkout -b <feature branch>

显示当前分支。必须在它前面显示*

git branch

添加您的本地更改(此处是故意的)

git add .

现在提交您的更改:

git commit -m "Refactored/ Added Feature XYZ"

重要:从master获取更新:

git pull origin feature-branch

现在推送您的本地更改:

git push origin feature-branch

git push --set-upstream origin <your branch name>