各人根据实际状况建立本身的项目(我在IntelliJ IDEA中建立了SpringBoot项目)html
先cd进入建立好的本地项目的文件夹下,而后执行以下Git命令: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] 推送本地某分支至远程某分支,并跟踪
复制代码
缘由:远程仓库和本地仓库不匹配,须要先合并,再push。github
详细的错误信息以下:shell
$ 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/…bash
缘由:GitHub的SSH key不存在或者SSH key验证不经过。ssh
详细的错误信息以下:ide
$ 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.
复制代码
解决方法:fetch
<1> SSH key不存在,则新建一个SSH key加入GitHub中。ui
命令总结以下:
打开Git Bash,先检查是否已经存在SSH keys,输入以下命令:
ls -al ~/.ssh
说明:
默认的公钥名为:
id_dsa.pub
id_ecdsa.pub
id_ed25519.pub
id_rsa.pub
复制代码
若不存在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帐户上
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
复制代码
GIT——新建项目并提交远程仓库: blog.csdn.net/qq_37049781…
本地项目提交到GitHub: www.jianshu.com/p/4f3151195…