我怎么做:

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


当前回答

我只是做

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

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

其他回答

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

# 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 是我的新分支名称。

基于这里的答案,我把这个过程作为一个简单的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

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

git branch <branch-name>

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

git push -u origin <branch-name>

编辑 过时,只需使用 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 push --set-upstream 起源 <你的分支名称>

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