$ 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
# 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
#!/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}
13 回答
简单地说,要创建一个新的 local 分支,请执行以下操作:
要将其推送到 remote 存储库,请执行以下操作:
对于1.7之前的GitLab版本,请使用:
git checkout -b name_branch
(name_branch,ex:master)
要将其推送到远程存储库,请执行以下操作:
git push -u origin name_new_branch
(name_new_branch,示例:功能)
要上传公共存储库的本地分支,需要
cd
到公共存储库,然后使用以下代码:这里给出的解决方案略有不同:
git pull
和git push
立即生效使用
HEAD
是"handy way to push the current branch to the same name on the remote" . 来源:https://git-scm.com/docs/git-push在Git术语中,HEAD(大写)是对当前分支(树)顶部的引用 .-u
选项只是--set-setupstream
的缩写 . 这将为当前分支添加上游跟踪参考 . 您可以通过查看.git / config文件来验证这一点:我想你已经克隆了一个项目,如:
NOTE: 我'm assuming that you have your server up and running. If it isn' t,它不起作用 . 一个好的方法是here .
已添加
添加远程分支:
检查一切是否正常(获取原点并列出远程分支):
创建本地分支并跟踪远程分支:
更新一切:
稍微根据这里的答案构建,我将这个过程包装成一个简单的Bash脚本,当然也可以用作Git别名 .
对我来说重要的补充是,这提示我在提交之前运行单元测试,默认情况下传入当前分支名称 .
git_push_new_branch.sh
我创建了一个别名,这样每当我创建一个新分支时,它都会相应地推送和跟踪远程分支 . 我将以下块放入
.bash_profile
文件中:Usage :只需输入
gcb thuy/do-sth-kool
,thuy/do-sth-kool
是我的新分支名称 .edit 已过时,只需使用
git push -u origin $BRANCHNAME
从William's miscellaneous Git tools(gitorious repo和clone)使用
git publish-branch
.好的,没有Ruby,所以 - 忽略了保护措施! - 获取脚本的最后三行并创建一个bash脚本,
git-publish-branch
:然后运行
git-publish-branch REMOTENAME BRANCHNAME
,其中REMOTENAME通常是原点(您可以修改脚本以将原点作为默认值等等)如果您不与他人共享您的仓库,将 all 您的分支机构推送到远程,以及
--set-upstream
正确跟踪您将非常有用:(不完全是OP的要求,但这个单线很受欢迎)
如果你与其他人分享你的回购,这不是一个很好的形式,因为你会用你所有狡猾的实验分支堵塞回购 .
我只是这样做
在已经克隆的项目上 .
Git在
localBranch
的提交下创建了一个名为remoteBranchToBeCreated
的新分支 .通过从现有分支分支来创建新分支
git checkout -b <new_branch>
然后使用将此新分支推送到存储库
git push -u origin <new_branch>
这会创建并将所有本地提交推送到新创建的远程分支
origin/<new_branch>
在Git 1.7.0及更高版本中,您可以签出一个新分支:
编辑文件,添加和提交 . 那么push with the -u (short for --set-upstream)选项:
Git将在推送期间设置跟踪信息 .
在引入
git push -u
之前,没有git push
选项来获得您想要的东西 . 您必须添加新的配置语句 .如果您使用以下方法创建新分支:
您可以使用
git config
命令避免直接编辑.git/config
文件 .或者,您可以手动编辑
.git/config
文件以获得此分支的跟踪信息 .