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

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

步骤

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

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

bash 复制代码
git add . 

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

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

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

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

    例如:

    [email protected]:xxx/xxx.git

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

bash 复制代码
git remote add origin [email protected]: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

相关推荐
Gladiator5751 小时前
博客记录-day152-力扣+分布式
github
jstart千语2 小时前
【Git】连接github时的疑难杂症(DNS解析失败)
git·github
工具罗某人2 小时前
TortoiseGit使用图解
git
Zhuai-行淮2 小时前
vscode和git 踩坑
ide·git·vscode
这颗橘子不太甜QAQ4 小时前
Husky使用技巧
javascript·git·代码规范
fanTuanye4 小时前
Git基本使用(很详细)
git·github
忍者扔飞镖4 小时前
git
git
Evenknow4 小时前
将"修改源码"改为更专业的"二次开发",体现技术深度
前端·github
uhakadotcom4 小时前
rAthena:快速入门与基础知识详解,附实用示例代码
面试·架构·github
李菠菜6 小时前
解决Windows系统下Git克隆时报错“unable to checkout working tree”的方法详解
windows·git