我怎么做:

从另一个分支创建一个本地分支(通过 git 分支或 git checkout -b) 将本地分支推到远程存储库(即发布),但使其可追踪,以便 git pull 和 git push 工作。


在引入 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 = origin
    merge = refs/heads/branchB

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


使用William的错误的Git工具的Git出版分支。

好吧,没有卢比,所以 - 忽略保留! - 采取脚本的最后三行,并创建一个 bash 脚本, git-publish-branch:

#!/bin/bash
REMOTE=$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

然后在您的本地副本中,创建一个新的分支,并检查它: git checkout -b <newbranch> 假设您在您的服务器上创建了一个“git bare --init”并创建了 myapp.git,您应该: git remote 添加起源 ssh://example.com/var/git/myapp.git git push 起源主 之后,用户应该能够 git clone http://example.com/var/git/myapp.git

注意:我假设你有你的服务器上运行,如果没有,它不会工作。

添加

添加一个远程分支:

git push origin master:new_feature_name

检查一切是否好(Fetch起源和列出远程分支):

git fetch origin
git branch -r

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

git checkout -tb new_feature_name origin/new_feature_name

更新一切:

git pull

在 Git 1.7.0 或更高版本中,您可以查看一个新的分支:

git checkout -b <branch>

编辑文件,添加和承诺. 然后按下 -u (缩写为 --set-upstream) 选项:

git push -u origin <branch>

Git 在推时将设置跟踪信息。

如果你不分享你的repo与其他人,这是有用的推所有的分支到远程,并 -set-upstream跟踪正确为你:

git push --all -u

(不是OP想要什么,但这个单线是相当受欢迎的)

如果你正在与其他人分享你的repo,这不是一个很好的形式,因为你会用你所有的Dodgy实验分支来关闭repo。

简单地说,要创建一个新的地方分支,做:

git branch <branch-name>

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

git push -u origin <branch-name>

创建一个新的分支,通过从现有分支中分支

git checkout -b <new_branch>

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

git push -u origin <new_branch>

此创建并推动所有本地承诺到新创建的远程分支起源/<new_branch>

我创建了一个标签,所以每当我创建一个新的分支,它将按下推和跟踪远程分支。

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

使用: 只是用 thuy/do-sth-kool 输入 gcb thuy/do-sth-kool 是我的新分支名称。

这里已经提供的解决方案的轻微变化:

创建一个本地分支,基于某些其他(远程或本地)分支: git checkout -b 分支名称 将本地分支推到远程存储库(发布),但使其可追踪,所以 git pull 和 git push 将立即运行 git push -u 起源 HEAD 使用 HEAD 是一个“手动的方式,将当前分支推到相同的名称在远程”。 来源: https://git-scm.com/docs/git-push 在 Git 条款中, HEAD (in)

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

git checkout -b name_branch

(名称_branch,前:大师)

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

git push -u origin name_new_branch

(名称_new_branch,例子:属性)

我只是做

git push -u origin localBranch:remoteBranchToBeCreated

这是一个已经克隆的项目。

Git 创建了一个新的分支,名为远程BranchToBeCreated 在我在本地Branch 所做的承诺下。

Edit: This changes your current local branch ( possibly named localBranch) upstream to origin/remoteBranchToBeCreated. 要修复这一点,只需输入:

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

git branch -u origin/localBranch

因此,您目前的本地分支机构现在跟踪起源/本地分支。

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

对于我来说,重要补充是,这促使我进行单位测试,然后默认地在当前分支名称中进行。

$ 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 tests
  git_push_new_branch OK        -> Pushes the current branch as a new branch to the origin
  git_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 tests
  git_push_new_branch.sh OK        -> Pushes the current branch as a new branch to the origin
  git_push_new_branch.sh MYBRANCH  -> Pushes branch MYBRANCH as a new branch to the origin

  )
  echo "$IT"
  exit
}

if [ -z "$1" ]
then
  show_help
fi

CURR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$1" == "OK" ]
then
  BRANCH=$CURR_BRANCH
else
  BRANCH=${1:-$CURR_BRANCH}
fi

git push -u origin $BRANCH

你可以在2个步骤中完成:

使用支票来创建本地分支:

git checkout -b yourBranchName

随心所欲地与你的分支工作。

使用推命令自我创建分支,并将代码发送到远程存储库:

git push -u origin yourBanchName

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

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

#!/usr/bin/env python3

import argparse
import subprocess
import 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 REMOTE
                        The remote name (default is 'origin')
  -b BRANCH, --branch BRANCH
                        The branch name (default is whatever HEAD is pointing to)

我认为这是最简单的 alias,添加到你的 ~/.gitconfig

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

你只是跑

git publish-branch

而且......它出版了分支。

完整的 Git 工作流,以推动本地变更到另一个功能分支类似于此

排除所有远程分支

git pull --all

列出所有分支现在

git branch -a  

查询或创建分支(用分支名替换 <函数分支>):

git checkout -b <feature branch>

顯示目前的分支. 必須顯示與 * 前面

git branch      

添加您的本地变更(. 是目的在这里)

git add .

现在做你的改变:

git commit -m "Refactored/ Added Feature XYZ"

重要:从大师更新:

git pull origin feature-branch

点击您的本地变更:

git push origin feature-branch

git push --set-upstream 起源 <你的分支名称>

git push -u 起源 <你的分支名>

现在可以(Git 版本 2.37.0)设置 git config --global push.autoSetupRemote true。