我怎么做:

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


当前回答

基于这里的答案,我把这个过程作为一个简单的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 工作流,以推动本地变更到另一个功能分支类似于此

排除所有远程分支

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

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

git push --all -u

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

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

我只是做

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

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

我假设你已经克隆了这样的项目:

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

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

git checkout -b name_branch

(名称_branch,前:大师)

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

git push -u origin name_new_branch

(名称_new_branch,例子:属性)