Java技术

创建本地项目并上传GitHub

2019-02-19  本文已影响0人  程序猿蛋蛋哥

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.

解决方法:新建一个SSH key加入GitHub中。

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

生成SSH key并添加到GitHub上

扩展阅读:

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

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

上一篇 下一篇

猜你喜欢

热点阅读