创建本地项目并上传GitHub

1. 创建本地项目

各人根据实际情况创建自己的项目(我在IntelliJ IDEA中创建了SpringBoot项目)

2. 在GitHub上创建仓库

GitHub上创建仓库1
GitHub上创建仓库2

3. 将本地项目上传到GitHub上

先cd进入创建好的本地项目的文件夹下,然后执行如下Git命令:

## 在GitHub创建仓库时没有添加README文件,先创建README.md文件
touch README.md

## Git初始化
git init
说明:当前目录执行git初始化,在当前目录中添加一个.git文件夹来存储一些git数据

## 添加所有文件到暂存区
git add *

## 将暂存区内容提交到本地仓库
git commit -m "项目本地仓库上传"

## 连接远程仓库(SSH和HTTPS方式都行)
git remote add origin git@github.com:rangdandanfei/git-use-demo.git

## 提交到远程仓库
git push -u origin master
说明:
git push 将当前分支推送至远程同名分支
git push origin [branch-name] 推送本地某分支至远程某分支
git push -u origin [branch-name] 推送本地某分支至远程某分支,并跟踪

FAQ :

1. 错误:error: failed to push some refs to 'https://github.com/rangdandanfei/git-using-demo.git'

原因:远程仓库和本地仓库不匹配,需要先合并,再push。

详细的错误信息如下:

$ git push -u origin master
To https://github.com/rangdandanfei/git-using-demo.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/rangdandanfei/git-using-demo.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.

解决方法:执行如下命令即可

## 先拉取(pull)远程仓库master分支代码,与本地进行合并,再将本地仓库master分支代码push到远程
git pull --rebase origin master
git push -u origin master

git pull --rebase 命令相关介绍可参考文章:https://www.cnblogs.com/kevingrace/p/5896706.html

2. 错误:git@github.com: Permission denied (publicKey). fatal: Could not read from remote respository.

原因:GitHub的SSH key不存在或者SSH key验证不通过。

详细的错误信息如下:

$ git push -u origin master
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

解决方法:

<1> SSH key不存在,则新建一个SSH key加入GitHub中。

命令总结如下:

  • 打开Git Bash,先检查是否已经存在SSH keys,输入如下命令:

    ls -al ~/.ssh
    
    说明:
    默认的公钥名为:
    id_dsa.pub
    id_ecdsa.pub
    id_ed25519.pub
    id_rsa.pub
    
    检查是否存在SSH keys
  • 若不存在SSH key,生成一个新的SSH key,输入如下命令:

    ssh-keygen -t rsa -b 4096 -c "your_email@example.com"
    

    生成了一个新的SSH key :

    > Generating public/private rsa key pair.
    
  • 当提示"Enter a file in which to save the key." 按Enter,保存到默认的文件位置:

    > Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
    
  • 提示输入安全密码,可以直接按回车Enter键

    > Enter passphrase (empty for no passphrase): [Type a passphrase]
    > Enter same passphrase again: [Type passphrase again]
    
  • 把SSH key复制到剪贴板

    $ clip < ~/.ssh/id_rsa.pub
    # Copies the contents of the id_rsa.pub file to your clipboard
    

    也可以打开C:/Users/you/.ssh/id_rsa文件,直接复制。

  • 把SSH key加入到GitHub账户上


    生成SSH key并添加到GitHub上

GitHub上有详细的教程,照着一步一步做就OK了,地址:https://help.github.com/articles/connecting-to-github-with-ssh/

<2> SSH key存在时,检测SSH连接和使用情况,尝试切换以HTTPS的方式连接远程仓库,若还是不行,则新建一个SSH key。

打开Git Bash,检测SSH连接情况,输入如下命令:

ssh -T git@github.com

若得到如下提示:

> The authenticity of host 'github.com (IP ADDRESS)' can't be established.
  > RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
  > Are you sure you want to continue connecting (yes/no)?
或者
> The authenticity of host 'github.com (IP ADDRESS)' can't be established.
  > RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
  > Are you sure you want to continue connecting (yes/no)?

输入yes,则看到如下提示,表示SSH连接验证成功

> Hi username! You've successfully authenticated, but GitHub does not
> provide shell access.

若得到如下提示:

$ ssh -T git@github.com
> git@github.com: Permission denied (publickey).

则在(3. 将本地项目上传到GitHub上)连接远程仓库时,尝试切换以HTTPS的方式连接。

## 连接远程仓库(HTTPS方式)
git remote add origin https://github.com/rangdandanfei/git-use-demo.git

若提示错误信息:fatal: remote origin already exists. 则 :

# 先输入
git remote rm origin
# 再输入
git remote add origin https://github.com/rangdandanfei/git-use-demo.git

检测SSH key的使用情况,输入如下命令:

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type -1
> debug1: identity file /Users/you/.ssh/id_rsa-cert type -1
> debug1: identity file /Users/you/.ssh/id_dsa type -1
> debug1: identity file /Users/you/.ssh/id_dsa-cert type -1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Trying private key: /Users/you/.ssh/id_rsa
> debug1: Trying private key: /Users/you/.ssh/id_dsa
> debug1: No more authentication methods to try.
> Permission denied (publickey).

或提示:
$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type 1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Offering RSA public key: /Users/you/.ssh/id_rsa
没找到SSH key的信息回显
找到SSH key的信息回显

扩展阅读:

GIT——新建项目并提交远程仓库: https://blog.csdn.net/qq_37049781/article/details/81873353

本地项目提交到GitHub: https://www.jianshu.com/p/4f3151195ef0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容