github中main与master,master无法合并到main

文章目录

遇到问题

上传 github 时候,发现传上去的是 master,但是 github 竟然还有一个 main

背景

github 采用 main 替代 master 作为主分支不是出于技术背景,而是出于 2020 年全球范围内兴起的 "Black Lives Matter"

然后在 2020 年 6 月宣布将默认分支名称从 master 改为 main 了,,,

为什么 master 合并不到 main 上去呢?

从 Git 2.9 版本开始,Git 默认不允许合并没有共同祖先的分支,以此避免意外合并两个不同项

怎么做

shell 复制代码
git fetch origin
git checkout main
git merge master
# 可能提示错误:fatal: refusing to merge unrelated histories

git merge master --allow-unrelated-histories

然后你再正常提交

那这次你终于解决了,以后怎么办呢?

如果是本地已经有的项目,使用 master 分支名

shell 复制代码
# 重命名本地分支
git branch -M main

# 推送新分支到远程仓库
git push -u origin main

# 删除远程旧分支
git push origin --delete master

如果以后新项目想所有初始化项目都弄成 main 呢

shell 复制代码
git config --global init.defaultBranch main
相关推荐
Jooolin14 分钟前
【编程史】Git是如何诞生的?这可并非计划之中...
linux·git·ai编程
草梅友仁16 分钟前
AI 图片文字翻译与视频字幕翻译工具推荐 | 2025 年第 23 周草梅周报
开源·github·aigc
qianmoQ5 小时前
GitHub 趋势日报 (2025年06月04日)
github
Lw老王要学习5 小时前
VScode 使用 git 提交数据到指定库的完整指南
windows·git·vscode
去旅行、在路上5 小时前
Git & Svn
git·svn
星哥说事7 小时前
使用VuePress2.X构建个人知识博客,并且用个人域名部署到GitHub Pages中
开源·github
勤劳打代码8 小时前
步步为营 —— Github Connection refused 分层诊断
github
寻月隐君8 小时前
深入解析 Rust 的面向对象编程:特性、实现与设计模式
后端·rust·github
养意11 小时前
git提交代码和解决冲突修复bug
git·bug