linux上传本地已有项目至GitHub
工具:git,ssh
1,登录github
2,创建仓库(空)
名称与本地项目目录名称保持一致,如:lbt
3,进入项目根目录下,初始化本地仓库
[root@wxfeng html]# cd lbt/ [root@wxfeng lbt]# git init
4,提交当前项目下的源代码之github仓库分支
[root@wxfeng html]# git add . [root@wxfeng html]# git commit -m "first commit" [root@wxfeng html]# git remote add origin git@github.com:GitHub用户名/lbt.git [root@wxfeng html]# git push -u origin master
补充:如果在GitHub远程仓库中已包含文件,在执行“git push -u origin master”会出现错误:
[root@wxfeng test]# git push -u origin master To github.com:xfcy1990/test.git ! [rejected] master -> master (non-fast-forward) error: 无法推送一些引用到 'git@github.com:xfcy1990/test.git' 提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。 提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见 提示:'git push --help' 中的 'Note about fast-forwards' 小节。
原因:远程代码库有某些文件本地代码库中不存在,会有冲突的隐患。
解决方法:需要先执行Git pull的操作,把远程代码先拿下来,如下:
[root@wxfeng test]# git pull origin master 来自 github.com:xfcy1990/test * branch master -> FETCH_HEAD fatal: 拒绝合并无关的历史
这时还是有错误,这是因为git拒绝合并无关的历史纪录,解决方法是在git pull时加上–allow-unrelated-histories,如下:
[root@wxfeng test]# git pull origin master --allow-unrelated-histories 来自 github.com:xfcy1990/test * branch master -> FETCH_HEAD Merge made by the 'recursive' strategy. LICENSE | 21 +++++++++++++++++++++ README.md | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 LICENSE create mode 100644 README.md
最后在push即可
[root@wxfeng test]# git push -u origin master 对象计数中: 5, 完成. Delta compression using up to 4 threads. 压缩对象中: 100% (3/3), 完成. 写入对象中: 100% (5/5), 522 bytes | 0 bytes/s, 完成. Total 5 (delta 0), reused 0 (delta 0) To github.com:xfcy1990/test.git e53b210..5e35203 master -> master 分支 master 设置为跟踪来自 origin 的远程分支 master。