我怎么做:
从另一个分支创建一个本地分支(通过 git 分支或 git checkout -b) 将本地分支推到远程存储库(即发布),但使其可追踪,以便 git pull 和 git push 工作。
我怎么做:
从另一个分支创建一个本地分支(通过 git 分支或 git checkout -b) 将本地分支推到远程存储库(即发布),但使其可追踪,以便 git pull 和 git push 工作。
当前回答
在 Git 1.7.0 或更高版本中,您可以查看一个新的分支:
git checkout -b <branch>
编辑文件,添加和承诺. 然后按下 -u (缩写为 --set-upstream) 选项:
git push -u origin <branch>
Git 在推时将设置跟踪信息。
其他回答
创建一个新的分支,通过从现有分支中分支
git checkout -b <new_branch>
然后将这个新分支推到存储库使用
git push -u origin <new_branch>
此创建并推动所有本地承诺到新创建的远程分支起源/<new_branch>
为了获得最大的灵活性,您可以使用自定义的 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)
对于 1.7 之前的 GitLab 版本,使用:
git checkout -b name_branch
(名称_branch,前:大师)
要将其推到远程存储库,请:
git push -u origin name_new_branch
(名称_new_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
基于这里的答案,我把这个过程作为一个简单的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