如何将新的本地分支推送到远程Git存储库并进行跟踪?
我希望能够做到以下几点:
- 
根据其他(远程或本地)分支(通过 git branch或git checkout -b)创build本地分支
- 
推送本地分支到远程存储库(发布),但使其可跟踪,所以 git pull和git push将立即工作。
我怎么做?
 我知道Git 1.7中的--set-upstream ,但是这是一个后期创build操作。 将分支推送到远程存储库时,我想find一种方法来进行类似的更改。 
在Git 1.7.0及更高版本中,您可以签出一个新的分支:
 git checkout -b <branch> 
 编辑文件,添加和提交。 然后用-u (简称--set-upstream )选项推送 : 
 git push -u origin <branch> 
Git会在推送过程中设置跟踪信息。
 这将推动你所有的分支到远程,并为您设置正确--set-upstream跟踪: 
 git push --all -u 
(并不是OP所要求的,但是这个单行程很受欢迎)
 在引入git push -u ,没有git push选项来获得你想要的东西。 你必须添加新的configuration语句。 
如果您使用以下方式创build新分支:
 $ 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 
简而言之,要创build一个新的本地分支,请执行:
 git branch <branch-name> 
要将其推送到远程存储库,请执行以下操作:
 git push -u origin <branch-name> 
这里已经给出了解决scheme的细微变化:
1)根据其他(远程或本地)分支创build本地分支:
 git checkout -b branchname 
  2)推送本地分支到远程存储库(发布),但使其可跟踪,所以git pull和git push将立即工作 
 git push -u origin HEAD 
 使用HEAD是一种“将当前分支推送到遥控器上的相同名称”的方便方法。 来源: https : //git-scm.com/docs/git-push在混帐术语中,HEAD(大写)是对当前分支(树)顶部的引用。 
  -u选项只是--set-setupstream 。 这将为当前分支添加上游跟踪参考。 你可以通过查看.git / config文件来validation这一点: 
  
 
我想你已经克隆了一个像这样的项目:
 git clone http://github.com/myproject.git 
- 
然后在你的本地副本中,创build一个新的分支并检查出来: git checkout -b <newbranch>
- 
假设您在服务器上创build了“git bare –init”并创build了myapp.git,您应该: git remote add origin ssh://example.com/var/git/myapp.git git push origin master
- 
之后,用户应该能够 git clone http://example.com/var/git/myapp.git
注:我假设你有你的服务器启动和运行。 如果不是,它将不起作用。 一个好的方法在这里 。
添加
添加一个远程分支:
 git push origin master:new_feature_name 
检查是否一切正常(获取原点和列表远程分支):
 git fetch origin git branch -r 
创build一个本地分支并跟踪远程分支:
 git checkout -tb new_feature_name origin/new_feature_name 
更新一切:
 git pull 
  编辑过时,只需使用git push -u origin $BRANCHNAME 
 使用来自William的各种Git工具 ( gitorious repo和clone )的git publish-branch 。 
 好,没有ruby,所以 – 忽略保护!  – 取脚本的最后三行并创build一个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通常是原点(你可以修改脚本以默认原点等等) 
通过从现有分支中分支来创build新分支
 git checkout -b <new_branch> 
然后使用这个新的分支到存储库
 git push -u origin <new_branch> 
 这将创build并将所有本地提交推送到新创build的远程分支origin/<new_branch> 
我只是做
 git push -u origin localBranch:remoteBranchToBeCreated 
在已经克隆的项目上。
  git在localBranch创build了一个名为remoteBranchToBeCreated的新分支。 
 我做了一个别名,以便每当我创build一个新的分支,它将相应地推动和跟踪远程分支。 我把下面的块放到.bash_profile文件中: 
 # 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 
  用法 :只需键入gcb thuy/do-sth-kool与thuy/do-sth-kool是我的新分支名称。 
对于1.7之前的GitLab版本,请使用:
 git checkout -b name_branch 
(name_branch,例如:master)
要将其推送到远程存储库,请执行以下操作:
 git push -u origin name_new_branch 
(name_new_branch,例如:function)
根据这里的答案稍微构build一下,我把这个过程简单地包装成一个简单的bash脚本,当然也可以用作git别名。
对我来说重要的是,这提示我在提交之前运行unit testing,并默认传入当前分支名称。
 $ 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} eg 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} eg 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 function old() { git push --set-upstream origin $BRANCH git push } git push -u origin $BRANCH 
 要上传公共存储库的本地分支,您需要cd到公共存储库,然后使用以下代码: 
 git push -u origin branchname