创建Git远程仓库&使本地仓库与Git远程仓库连接的详细步骤&连接过程中遇到的问题及错误详解

一.创建Git远程仓库

GitHub官网 : [GitHub官网](https://github.com/)

登录后,右上角+号,选择New repository

以下是页面一些内容的描述,挑重要的说,有些片面,见谅

    Repository name : 仓库名称

    Description (optional) : 对仓库的描述

    Public : 公开

    Private : 私有

    Add a README file : 添加一个README说明文档

    Add .gitignore : 添加一个 .gitignore文件 [从模板列表中选择不跟踪的文件]

    Choose a license : 选择许可证

    Apache License 2.0 : 指别人使用时注明来源

    MIT License : 指完全公开

    其他不清楚

搞定之后,点击 Create repository 创建

经过以上步骤,Git远程仓库就算是建好了

二.使本地仓库与Git远程仓库连接

命令是从官网拷来的,有些用不到,按序号运行即可,遇到错误就去解决

    1. git init 初始化项目

    git add README.md 更新README.md文档

    git commit -m "first commit" 提交更新,备注"first commit"

    git branch -M master 不知道是啥,我一般这句不执行

    2. git remote add origin url 建立远程连接,使本地仓库与Git远程仓库连接

    3. git push -u origin master 将本地项目更新到Git远程仓库

三.连接过程中遇到的问题及错误

第一个:运行 git remote add origin url 时出现

fatal: not a git repository (or any of the parent directories): .git

这个是在运行 git remote add origin url 时出现的,主要原因是因为没有初始化项目

运行以下命令,再次运行即可

git init

第二个:运行 git commit -m “first commit” 时出现

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"

  git config --global user.name "Your Name"

to set your account's default identity.

Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got '司先生@DESKTOP(none)')

这个一般是在运行 git commit -m "first commit" 时出现的,反正我是这样,大概意思就是 老子不知道你是谁,凭啥给你操作,给老子配置一下GIt的邮箱和用户名,让老子知道你是Git的人,老子才给你操作

按上面说的命令配置一下邮箱和用户名,再次运行即可

git config --global user.email "邮箱"

git config --global user.name "用户名"

第三个:运行 git push -u origin master 时出现

To https://github.com/sywdebug/mall.git

! [rejected]        master -> master (fetch first)

error: failed to push some refs to 'https://github.com/sywdebug/mall.git'

这个是在运行 git push -u origin master 时出现的,反正我是这样,大概意思就是没有同步远程的master

输入以下命令进行同步远程的master,再次运行即可

git pull origin master

第四个:运行 git push -u origin master 时出现

To https://github.com/sywdebug/mall.git

! [rejected]        master -> master (non-fast-forward)

error: failed to push some refs to 'https://github.com/sywdebug/mall.git'

出现第二个错误,运行过git pull origin master后,再次运行 git push -u origin master 时出现的,反正我是这样,大概意思就是 出现错误:无法推送某些 ref 至 Git仓库,为防止丢失历史记录,非快进更新已被拒绝 | 官方解释,不太懂

这时,我们再次输入以下命令获取和合并远程分支上所做更改与本地所做更改,发现会出现下一个问题

git pull origin master

第五个:运行 git pull origin master 时出现

From https://github.com/sywdebug/supermall

* branch            master    -> FETCH_HEAD

fatal: refusing to merge unrelated histories

这个是运行 git pull origin master 命令时出现的,反正我是这样,大概意思就是拒绝合并无关的来历不明的分支

在输入命令时,后面加上以下命令即可,而后便可以运行git push -u origin master

--allow-unrelated-histories

例如

git pull origin master --allow-unrelated-histories

这波看似应该和上面的写在一块,但是当时在我自己电脑上时运行git pull origin master并没有遇到这个问题,第二天在公司电脑上才遇到,所以认为应该分开写

第六个:运行 git push -u origin master 时出现

error: src refspec master does not match any

error: failed to push some refs to 'https://github.com/sywdebug/mall.git'

有时候我们运行git push -u origin master不是出现的上面错误,而是这个,我在自己的电脑上运行的时候出现过一次,便记录下来了,大概意思我也不知道

运行以下命令,再次运行即可

git pull origin master

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。