如何将新的本地分支推送到远程Git仓库并进行跟踪

如何将新的本地分支推送到远程Git仓库并进行跟踪

技术背景

在使用Git进行版本控制时,我们经常需要创建新的本地分支来开发新功能、修复问题等。完成本地开发后,需要将这些新分支推送到远程仓库,并且让本地分支与远程分支建立跟踪关系,以便后续的git pullgit push操作更加便捷。

实现步骤

Git 1.7.0及更高版本

  1. 创建新的本地分支
1
git checkout -b <branch>
  1. 编辑文件,添加并提交更改
  2. 推送分支并设置跟踪信息
1
git push -u origin <branch>

旧版本Git

如果使用的是Git 1.7.0之前的版本,没有git push -u选项,需要手动添加跟踪信息。

  1. 创建新分支并推送到远程
1
2
git checkout -b branchB
git push origin branchB:branchB
  1. 添加跟踪信息
    • 使用git config命令:
1
2
git config branch.branchB.remote origin
git config branch.branchB.merge refs/heads/branchB
- 手动编辑`.git/config`文件:
1
2
3
[branch "branchB"]
remote = origin
merge = refs/heads/branchB

其他方法

使用HEAD推送当前分支

1
2
git checkout -b branchname
git push -u origin HEAD

自定义分支名称推送

1
git push -u origin localBranch:remoteBranchToBeCreated

若要修正当前本地分支的上游,可以使用:

1
2
3
git branch --set-upstream-to=origin/localBranch
# 或者
git branch -u origin/localBranch

完整工作流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 拉取所有远程分支
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

核心代码

创建并推送新分支

1
2
git checkout -b <new_branch>
git push -u origin <new_branch>

自定义Bash脚本(git_push_new_branch.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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

自定义Python脚本(git-publish

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/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())

最佳实践

  • 使用-u选项:在推送新分支时,使用-u--set-upstream)选项可以自动设置跟踪信息,方便后续操作。
  • 保持分支命名规范:使用有意义的分支名称,例如feature/xxxbugfix/xxx,便于团队协作和管理。
  • 定期拉取更新:在推送分支之前,先从远程仓库拉取最新的更改,避免冲突。

常见问题

  • 权限问题:如果没有足够的权限推送到远程仓库,会收到权限拒绝的错误。需要确保你有正确的访问权限。
  • 分支已存在:如果远程仓库已经存在同名分支,推送会失败。可以更改本地分支名称或删除远程分支后再推送。
  • 跟踪信息错误:如果手动设置跟踪信息时出现错误,可能会导致git pullgit push操作出现问题。可以使用git branch -u命令重新设置跟踪信息。

如何将新的本地分支推送到远程Git仓库并进行跟踪
https://119291.xyz/posts/2025-05-07.how-to-push-new-local-branch-to-remote-git-repo/
作者
ww
发布于
2025年5月7日
许可协议