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

相关推荐
WindrunnerMax7 分钟前
以轻量 LoopAgent 替代 RAG 实现文档检索
人工智能·架构·github
做运维的阿瑞18 分钟前
利用GitHub配置镜像加速,解决 Docker镜像拉取问题
运维·docker·容器·github
怕浪猫1 小时前
AI Agent 的安全怎么保障?面试最后一关的终极问题
算法·面试·github
m4Rk_1 小时前
【论文阅读】Agent 记忆机制(97):MCMA——让 Agent 学会如何抽象记忆,实现跨任务经验迁移
论文阅读·人工智能·学习·开源·github
miofly1 小时前
GitHub 日榜趋势速报 | 2026-10-12
开源·github
高频因子挖掘机2 小时前
量化研究中,为什么拿到 Pandas 行情数据不等于完成数据接入?
后端·github·api
小弥儿2 小时前
PPT Master:用 AI 把PDF/Word一键转成可编辑 PPT
学习·开源·github
高频因子挖掘机3 小时前
股票 API 接入回测框架时最容易忽略的几个问题:复权、时间对齐与数据泄露
后端·github·api
mister_guo10 小时前
开发指南:Git日常维护与常用命令
git