git常用之已存在的目录转换为一个 GIT 项目并托管到github仓库

git常用之已存在的目录转换为一个 GIT 项目并托管到github仓库

步骤

  1. 将目录初始化为一个 Git 项目
bash 复制代码
 git init
  1. 修改你git 想展示的用户
bash 复制代码
git config user.name xxx
git config user.email  xxx@xxx.com
  1. 创建 .gitignore 文件 文件

  2. 将所有文件放进新的本地 git 仓库

bash 复制代码
git add . 

如果你本地已经有 .gitignore 文件,会按照已有规则过滤不需要添加的文件。如果不想要添加所有文件,可以把 . 符号换成具体的文件名。

  1. 将添加的文件提交到仓库
bash 复制代码
 git commit -m "Initial commit"
  1. 访问 GitHub,创建一个新仓库

注意:为了避免冲突,先不要勾选 README 和 LICENSE 选项

  1. 在生成的项目github主页上,复制仓库地址

    例如:

    git@github.com:xxx/xxx.git

  2. 回到命令行终端界面,将本地仓库关联到远程仓库

bash 复制代码
git remote add origin git@github.com:xxx/xxx.git
git remote -v
  1. 把Git默认分支master修改为main
bash 复制代码
  git branch
* master

Github 把 master 默认分支改为了 main ,我们把本地 Git 配置文件里的 master 改成 main

0) 重命名本地 Git 存储库中的 "master" 分支

bash 复制代码
 git branch -m master main
 git branch

1) Windows 中 Git 的配置文件在 C:\Users<用户名> 下 .gitconfig

修改为:

bash 复制代码
[init]
    defaultBranch = main

2) 直接使用 Git 命令:

bash 复制代码
git config --global init.defaultBranch main
  1. 提交代码到 GitHub 仓库
    设置本地 main 分支与远程 main 分支的追踪关系
bash 复制代码
git branch --set-upstream-to=origin/main main  

git pull 如果报错:
fatal: refusing to merge unrelated histories

则执行

bash 复制代码
git pull origin main --allow-unrelated-histories  

如果报错:Automatic merge failed; fix conflicts and then commit the result.

则执行,使用以下命令强制合并远程分支,并以本地的 文件为准:

bash 复制代码
git pull origin main --allow-unrelated-histories -X ours  
git commit -m "Resolve merge conflict " 
git push origin main  

其他参考

如何在 Git 中将 master 分支重命名为 main

参考URL: https://www.git-tower.com/learn/git/faq/git-rename-master-to-main

相关推荐
郑州光合科技余经理7 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
我命由我123457 小时前
Git 推送报错:error: src refspec main does not match any
运维·git·gitee·github·运维开发·学习方法·版本控制
阿里嘎多学长8 小时前
2026-09-20 GitHub 热点项目精选
开发语言·程序员·github·代码托管
峰向AI9 小时前
skill-up: [阿里开源] 让 AI Agent Skills 自己进化的评估工具
github
Lcr3s12 小时前
Git基础之(0):如何在Ubuntu(Linux)上安装git
git
子林super12 小时前
顺序表用Python3定义与增删改查
github
子林super12 小时前
Python非空判断与类型检查写法
github
子林super12 小时前
Python list与array关键区别及使用场景
github
子林super12 小时前
Python中None与null的区别
github
u13013015 小时前
GitHub 热榜项目:周榜(2026-09-20)
人工智能·github