创建本地项目并上传GitHub

5,038 阅读2分钟

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 'github.com/rangdandanf…'

原因:远程仓库和本地仓库不匹配,需要先合并,再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 命令相关介绍可参考文章:www.cnblogs.com/kevingrace/…

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了,地址:help.github.com/articles/co…

<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——新建项目并提交远程仓库: blog.csdn.net/qq_37049781…

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