使用Git命令上传代码到GitHub

文章
林里克斯

使用git命令将本地代码上传到GitHub仓库

实验平台:Windows 10
首先需要Git命令的支持

使用Git命令上传代码到GitHub



一、下载Git客服端


1.客服端官网下载:

https://git-scm.com/downloads
#包括了现目前常用三大平台(Windows/Mac/Linux)

2.安装客服端

一路Next。安装完成后右键会多两个选项

Git GUI Here
Git Bash Here
#这两种方式均可上传代码到GitHub上,Git GUI Here是通过图形界面;Git Bash Here是通过命令的方式

二、使GitHub信任此电脑


1.创建项目

使用Git命令上传代码到GitHub

使用Git命令上传代码到GitHub

2.创建公私密钥
首先确认路径下面是否存在密钥

C:\Users\你电脑的用户名\.ssh
#电脑用户名默认Administrator,若修改过就是你修改的

若存在,删除执行下一步,若不存在直接执行下一步;

$ ssh-keygen -t rsa -C "your_email@gmail.com"
#这里填写你注册GitHub时的邮箱
一路回车

3.将密钥复制到GitHub上
拷贝id_rsa.pub里的内容到网页上

使用Git命令上传代码到GitHub

使用Git命令上传代码到GitHub

我这里已经添加过一次了,所有有一个存在,没添加过是没有的。

使用Git命令上传代码到GitHub

4.测试
右键打开Git Bash Here

ssh -T git@github.com

使用Git命令上传代码到GitHub


三、上传代码


1.在你要上传代码的文件夹右键选择Git Bash Here

$ git init
Initialized empty Git repository in E:/Jarbo/pdf/.git/
#会在当前目录下生成一个.git的隐藏文件夹,git的版本控制器

2.键入上传者信息

$ git config --global user.name "JarboU"  #区分提交者的名字
$ git config --global user.email "mylinux@kjarbo.com" #GitHub的登录邮箱

3.提交代码到本地仓库

$ git add .   #将当前目录放到准备提交到本地仓库里
$ git commit -m 'pdf'   #'描述'  #提交到本地仓库里

[master (root-commit) 17c25b4] pdf
 18 files changed, 1575 insertions(+), 0 deletions(-)
 create mode 100644 index.html
 create mode 100644 static/.DS_Store
 create mode 100644 static/css/.DS_Store
 create mode 100644 static/css/style.css
 create mode 100644 static/fonts/.DS_Store
 create mode 100644 static/fonts/demo.css
 create mode 100644 static/fonts/demo.html
 create mode 100644 static/fonts/iconfont.css
 create mode 100644 static/fonts/iconfont.eot
 create mode 100644 static/fonts/iconfont.svg
 create mode 100644 static/fonts/iconfont.ttf
 create mode 100644 static/fonts/iconfont.woff
 create mode 100644 static/image/.DS_Store
 create mode 100755 static/image/bg.jpg
 create mode 100644 static/image/weixin.png
 create mode 100644 static/js/.DS_Store
 create mode 100644 static/js/modal.js
 create mode 100644 static/js/script.js

4.提交到远程仓库

$ git remote add origin git@github.com:JarboU/pdf.git
$ git push -u origin master   #提交代码

5.测试
成功后就可以刷新网页端,即可查看到代码内容。


四、整理一下一些我遇到的报错


1.不同步导致(远程仓库有README)

$ git push -u origin master

To github.com:JarboU/pdf.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:JarboU/pdf.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first 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 pull --rebase origin master       #pull=fetch+merge

From github.com:JarboU/pdf
 * branch            master     -> FETCH_HEAD
First, rewinding head to replay your work on top of it...
Applying: pdf
#再次提交后成功提交

2.配置文件Url问题

error: The requested URL returned error: 403 Forbidden while accessing https://github.com/JarboU/cv.git/info/refs

解决报错

$ vim .git/config

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = https://github.com/JarboU/cv.git
        fetch = +refs/heads/*:refs/remotes/origin/*

修改为

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = https://JarboU@github.com/JarboU/cv.git
        fetch = +refs/heads/*:refs/remotes/origin/*

Over~

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

891 0 2017-02-05


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-04-24

今日天气
站点统计
  • 文章总数:240篇
  • 分类总数:29个
  • 评论总数:10条
  • 本站总访问量 215076 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!