美文网首页互联网科技码农的世界
git上传代码到github入门学习和相关错误汇总

git上传代码到github入门学习和相关错误汇总

作者: xml_ | 来源:发表于2018-11-14 15:11 被阅读18次

    git和github作为版本控制的角色在项目开发中起到了重要的作用,今天我们就来学习如何使用git上传代码到github以及在操作中可能出现的问题,欢迎大家指正!


    一,注册github账号

    1,浏览器进入github:https://github.com/

    2,注册好以后New repository,建仓库

    3,填写仓库名称,可以选择公开或私密,私密不免费,最好把Initialize this repository with a README勾上

    4,到此仓库已经建好。


    二,添加(上传)文件到仓库

    • 上传单个文件

    上传单个文件比较简单,之间在仓库界面点击Create new file——>输入文件名(如果需要添加文件夹,可以输入文件夹名称之后,按下/ 就会自动分隔为文件夹路径了)——>输入文件内容,或描述——>选择commit new file,文件创建完毕

    • 上传多个文件(重点)

    1,首先下载git:https://gitforwindows.org/
    2,安装后,右键桌面会有以下两个选项:
      `Git GUI here` 和  `Git Bash here`
       我们选择`Git Bash here`这项
    
    3,在界面输入ssh-keygen -t rsa -C"email@qq.com"
    *目的是为了配置ssh,后面的email@qq.com改为你的邮箱,就是github上注册的那个邮箱,然后一路  回车三次。不出意外ssh key已经生成成功*
    
    4,进入本地磁盘,填写key到github
    • 电脑打开文件夹C:\Users\用户名.ssh

    • 可以看到生成了两个文件id_rsa和id_rsa.pub

    • 记事本打开id_rsa.pub,复制里面的内容。

    • 浏览器回到github,点击右上角个人头像向下三角形状选择setting-->ssh and GPG keys-->new ssh key

    • 将复制的内容填入key中

      title :自己填一个任意的

      key :将刚才复制的所有内容复制到里面

    5,验证刚刚填入的key

    在git的终端输入 ssh -T git@github.com,回车后,输入yes,会提示验证成功

    6,配置全局的user和email

    git config --global user.name"your name"your name换成你注册github时的用户名
    git config --global user.email"email@qq.com"email换成你注册github的邮箱

    7,建立本地与github仓库的连接

    testgit remote add origin git@github.com:yourName/yourRepo.git(把 your name换成你注册github时填写的名字,把yourRepo 换成在github建立的仓库的名字)

    8,进入需要上传代码的文件夹,例如:

    在git终端输入:cd c:/zsybim

    9,上传文件

    在git终端输入:git add ./上传该文件夹下面的所有的文件

    上传单个文件要指定路径,如:git add ./read.txt

    10,提交文件

    在git终端输入:git commit -m"commit"

    11,本地仓库推送到服务器

    在git终端输入:git push origin master

    12,最后查看文件是否上传成功

    进入github界面找到对应的仓库,查看刚刚上传的文件是否存在


    三,之后操作

    以后上传文件就不用这么麻烦了,因为准备工作都做好了,以后只需要下面的步骤就行:

    7,建立本地与github仓库的连接

    testgit remote add origin git@github.com:yourName/yourRepo.git(把 your name 换成你注册github时填写的名字,把yourRepo 换成在github建立的仓库的名字)

    8,进入需要上传代码的文件夹,例如:

    在git终端输入:cd c:/zsybim

    9,上传文件

    在git终端输入:git add ./ (上传该文件夹下面的所有的文件)
    上传单个文件要指定路径,如:git add ./read.txt

    10,提交文件

    在git终端输入:git commit -m"commit"

    11,本地仓库推送到服务器

    在git终端输入:git push origin master


    四,错误集锦

    其实在操作过程中还是会有很多错误出现的,对于刚刚上手的朋友来说很多问题是不知道怎么解决的,这里罗列出了我在操作过程中出现的问题错误给大家参考:

    1,输入:testgit remote add origin git@github.com:yourName/yourRepo.git

         报错:fatal: Not a git repository (or any of the parent directories): .git
    

    解决办法:输入 git init ,初始化一个本地仓库


    2,输入:testgit remote add origin git@github.com:yourName/yourRepo.git

          报错:fatal: remote origin already exists.
    

    解决办法:
    1 删除Git仓库中的origin信息:git remote rm origin

    2 重新添加Git仓库中的origin信息


    3,输入:git push origin master

    
         报错:fatal: 'origin' does not appear to be a git repository
    
                   fatal: Could not read from remote repository.
    
                  Please make sure you have the correct access rights and the repository exists.
    

    解决办法:重新输入一次:git remote add origin git@github.com:yourusername/test.git


    4,输入:git push origin master

    
          报错:fatal: remote error:
    
              XXXXXX@qq.com/myarea is not a valid repository name Email support@github.com for help
    

    解决办法:使用git remote rm origin 然后再使用上传命令


    5,输入:git push origin master

    
          报错:To git@git.oschina.net:yangzhi/hello.git
    
                    ! [rejected]        master -> master (fetch first)
    
                   error: failed to push some refs to 'git@git.oschina.net:yangzhi/hello.git'
    
                   hint: Updates were rejected because the remote contains work that you do
    
                   hint: not have locally. This is usually caused by another repository pushin
    
                   hint: to the same ref. You may want to first merge the remote changes (e.g.
    
                   hint: 'git pull') before pushing again.
    
                   hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    

    解决办法:出现这个问题是因为github中的README.md文件不在本地代码目录中。先进行代码合并 git pull --rebase origin master 再执行 git push origin master


    原创作者:梦凌小样
    作品链接:https://www.jianshu.com/p/c089d2b20ea7【原创不易,转载请注明出处,感谢理解】
    一位爱生活,爱创作,爱分享,爱自己的90后女程序员一枚,记录工作中的点点滴滴,一起学习,共同进步,期待能和优秀的您交上朋友

    相关文章

      网友评论

        本文标题:git上传代码到github入门学习和相关错误汇总

        本文链接:https://www.haomeiwen.com/subject/takjfqtx.html