目录

 

一,推送至GitHub大致步骤:

具体步骤:

1,创建本地仓库

2,添加文件

3,将暂存区文件提交到本地仓库

4,添加SSH KEY

4.1 本地创建SSH KEY

4.2,GitHub添加SSH KEY

5,创建repositories

6,关联远程仓库

二,将上传到GitHub上的文件再推送到Gitee上

分享不易,希望各位看官在学习之余动动小手,点个赞把~~有问题也请大神及时指出,不惜赐教! 


经过多次踩坑后,我还是决定将平常把文件上传到github/gitee时遇到的一些问题做出总结,以便以后再次踩坑了可以回来看看,不用到处找博客资料解决问题。

一,推送至GitHub大致步骤:

  1. git init 在本地创建一个Git仓库;
  2. git add . 将项目添加到暂存区;
  3. git commit -m "注释内容" 将项目提交到Git仓库;
  4. 添加SSH KEY;
  5. 新建repositories;
  6. git remote add origin git@github.com:UserName/projectName.git 将本地仓库与远程仓库关联;
  7. git push -u origin master 将本地项目推送到远程仓库。

具体步骤:

注:(如果刚下载Git的朋友需要配置一下对应的配置信息:

 设置用户信息(用户名,邮箱)。
此步骤一般时刚刚安装完Git后设置的,以后你每次 Git 的提交都会使用这些信息。在 Git 终端输入一下命令即可设置你的用户信息。

$ git config --global user.name "name"
$ git config --global user.email "email@example.com"

可以通过以下命令,查看当前的设置:

$ git config --list

如果已经设置,则跳过到下一步。)

1,创建本地仓库

在需要上传的文件上使用cmd进入命令行界面,通过git init命令即可在本地对应的文件夹中创建一个.git文件,如图:

此时,该路径已经被Git比那成一个Git本地仓库了。之后如果随意修改文件夹名名称,可能会出错(我也没有手贱试过) 。

2,添加文件

通过给git add .(add和 . 之间有一个空格)添加文件内所有文件,它可以实现将文件添加到本地仓库的暂存区(并未正真添加到本地Git仓库),也可以通过git add 文件名来上传对应的文件到暂存区。

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git add .
warning: LF will be replaced by CRLF in 前端/yiyuan/.gitignore.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 前端/yiyuan/README.md.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 前端/yiyuan/babel.config.js.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 前端/yiyuan/package-lock.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 前端/yiyuan/package.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 前端/yiyuan/public/index.html.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 前端/yiyuan/src/App.vue.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 前端/yiyuan/src/assets/css/bootstrap.css.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 前端/yiyuan/src/assets/css/font-awesome.css.
The file will have its original line endings in your working directory…………

如果中途把不需要的文件误操作添加到暂存区,可以通过

$ git reset HEAD -- 文件名

或者

$ git rm -r --cached 文件名

来撤回/清除暂存区缓存文件。双杠前后都有空格!

3,将暂存区文件提交到本地仓库

$ git commit -m "提交时的注释"

这步操作一般是为了注释,方便自己或他人查看自己上传的文件信息。

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git commit -m "药品信息管理系统第一次提交"
[master (root-commit) 8cc3f51] 药品信息管理系统第一次提交
 366 files changed, 241016 insertions(+)
 create mode 100644 "R&D_Managmet\344\277\241\346\201\257\346\246\202\350\246\201.pdf"
 create mode 100644 "R&D_Managmet\345\274\200\345\217\221\346\226\207\346\241\243.pdf"
 create mode 100644 readme.txt
 create mode 100644 "\345\211\215\347\253\257/readme.txt"
 create mode 100644 "\345\211\215\347\253\257/yiyuan/.gitignore"
 create mode 100644 "\345\211\215\347\253\257/yiyuan/README.md"
 create mode 100644 "\345\211\215\347\253\257/yiyuan/babel.config.js"
 create mode 100644 "\345\211\215\347\253\257/yiyuan/package-lock.json"
 create mode 100644 "\345\211\215\347\253\257/yiyuan/package.json"
 create mode 100644 "\345\211\215\347\253\257/yiyuan/public/favicon.ico"
 create mode 100644 "\345\211\215\347\253\257/yiyuan/public/index.html"
 create mode 100644 "\345\211\215\347\253\257/yiyuan/src/App.vue"
 create mode 100644 "\345\211\215\347\253\257/yiyuan/src/assets/css/bootstrap.css"
 create mode 100644 "\345\211\215\347\253\257/yiyuan/src/assets/css/font-awesome.css"…………

此时文件就开始将选定的文件上传到本地仓库了。

4,添加SSH KEY

4.1 本地创建SSH KEY

由于本地Git仓库和Github仓库之间的传输是通过SSH加密的,所以连接时需要两边都有SSH KEY。

创建之前先看看你的电脑有没有创建过SSH KEY。先看一下你C盘用户目录下有没有.ssh目录,有的话里面有没有id_rsa和id_rsa.pub这两个文件,有就跳到下一步,没有就通过下面命令创建

$ ssh-keygen -t rsa -C "email@example.com"

然后一路回车。这时你就会在用户下的.ssh目录里找到id_rsa和id_rsa.pub这两个文件

4.2,GitHub添加SSH KEY

登录Github,找到右上角的图标,打开点进里面的Settings,再选中里面的SSH and GPG KEYS,点击右上角的New SSH key,然后Title里面随便填,再把刚才id_rsa.pub里面的内容全部复制到Title下面的Key内容框里面,最后点击Add SSH key,这样就完成了SSH Key的加密。

 ,如果像我这样早就备好了SSH KEY的可以跳过此步。

5,创建repositories

具体过程看图即可

6,关联远程仓库

通过该命令行关联github的远程仓库

git remote add origin git@github.com:curry-stephen/R-D-manager.git

 通过git remote -v 可以以查看远程仓库的关联情况

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git remote -v
origin  git@github.com:curry-stephen/R-D-manager.git (fetch)
origin  git@github.com:curry-stephen/R-D-manager.git (push)

 7,将本地仓库中的文件推送到远程仓库

通过以下命令:

$ git push -u origin master

由于新建的远程仓库是空的,所以要加上-u这个参数,等远程仓库里面有了内容之后,下次再从本地库上传内容的时候只需下面这样就可以了:

$ git push origin master

或者可以更简单的:

$ git push

当然,这样会默认推送到主支干master。
此时就容易容易出现踩坑的情况!

1,出现以下情况:

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git push -u origin master
error: src refspec master does not match any
error: failed to push some refs to 'github.com:curry-stephen/R-D-manager.git'

解决:这是因为新建的Github仓库的README.md文件不在本地仓库的目录中,(推荐:上传文件时,先将仓库创建好然后把README文件克隆到自己文件下)这个问题其实在多人协作开发中是一定存在的,多个人共同关联一个远程仓库,在把你本地的修改上传(push)之前,要把别人的代码更新到自己的本地仓库,不然如何达到协作开发。

将远程仓库最新的更新拉取到本地,并合并到主支干:

$ git pull --rebase origin master

然后我们再重新push就可以成功了。

2,出现以下情况:

To gitee.com:yao-shen/r-d-manager.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'gitee.com:yao-shen/r-d-manager.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

通过:

git push -f origin master  //强制覆盖

即可解决。

3,出现以下情况:

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to 'github.com:curry-stephen/R-D-manager.git'

注意看,这次是推送到main分支!因为发生异一场当时很火的“Black Lives Matter”运动。GitHub 从2020年 10 月 1 日起,在该平台上创建的所有新的源代码仓库将默认被命名为 “main”,而不是原先的"master"。所以关于pull和push的命令中的master都要改成main(前提是你创建的新的源代码仓库是默认命名,没有修改)。我么可以看一下,出现这个错误时,远程仓库和本地仓库的分支情况如何:

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git show-ref
7e7a93b6bf930b898ca7e211624e193d557f27ad refs/heads/master
907855b50df2c8fe93f830263e422c9200dc56de refs/remotes/origin/main

#查看本地分支
C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git branch
* master

#查看远程默认分支
C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git branch -r
  origin/main

因此我们需要修改一下本地分支命名:git branch -m oldname newname

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git branch -m master main

然后重新推送至main分支即可。


 

二,将上传到GitHub上的文件再推送到Gitee上

前面再Gitee上创建仓库的步骤大致一致,并且还是中文,我就不多叙述了。直接进入正题!

当我们关联GitHub后,可以看到本地库已经关联了一个名叫 origin 的远程库:

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git remote -v
origin  git@github.com:curry-stephen/R-D-manager.git (fetch)
origin  git@github.com:curry-stephen/R-D-manager.git (push)

 我们可以删除已有的 GitHub 远程库:

git remote rm origin

再关联 Gitee 的远程库(注意路径中需要填写正确的用户名):

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git remote add origin git@gitee.com:yao-shen/r-d-manager.git

此时,我们再查看远程库信息:

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git remote -v
origin  git@gitee.com:yao-shen/r-d-manager.git (fetch)
origin  git@gitee.com:yao-shen/r-d-manager.git (push)

现在可以看到,origin 已经被关联到 Gitee 的远程库了。

通过 git push 命令就可以把本地库推送到 Gitee 上。

有的小伙伴又要问了,一个本地库能不能既关联 GitHub,又关联 Gitee 呢?

答案是肯定的,因为 git 本身是分布式版本控制系统,可以同步到另外一个远程库,当然也可以同步到另外两个远程库。

使用多个远程库时,我们要注意,git 给远程库起的默认名称是 origin,如果有多个远程库,我们需要用不同的名称来标识不同的远程库。

仍然以我们自己的本地库为例,我们先删除已关联的名为 origin 的远程库:

git remote rm origin

然后,先关联 GitHub 的远程库:

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git remote add github git@github.com:curry-stephen/R-D-manager.git

注意,远程库的名称叫 github,不叫 origin 了

接着,再关联 Gitee 的远程库:

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git remote add gitee git@gitee.com:yao-shen/r-d-manager.git

同样注意,远程库的名称叫 gitee,不叫 origin。

现在,我们用 git remote -v 查看远程库信息,可以看到两个远程库:

C:\Users\young\Desktop\2022055579-参赛总文件夹\2022057545-02素材与源码>git remote -v
gitee   git@gitee.com:yao-shen/r-d-manager.git (fetch)
gitee   git@gitee.com:yao-shen/r-d-manager.git (push)
github  git@github.com:curry-stephen/R-D-manager.git (fetch)
github  git@github.com:curry-stephen/R-D-manager.git (push)

如果要推送到 GitHub,使用命令:

git push github master

如果要推送到 Gitee,使用命令:

git push gitee master

这样一来,我们的本地库就可以同时与多个远程库互相同步:

分享不易,希望各位看官在学习之余动动小手,点个赞把~~有问题也请大神及时指出,不惜赐教! 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐