git -解决push报错问题
问题出现场景
github新建仓库,想把本地仓库推送到github
操作步骤
进入本地仓库,假设项目为test
cd testgit init查看状态
git status把更改的文件添加到本地git仓库
git add .设置用户名邮箱
git config --global user.name 'jimi'git config --global user.email '[email protected]提交
git commit -m "first commit"把本地test项目和githhub的test项目进行关联
git remote add origin xxxxx@xxxx向远程仓库提交代码(配置好ssh或者密码)
git push origin master可能会出现问题:
shell$ git push origin master To github.com:JasonLi-cn/test.git ! [rejected] master -> master (fetch first) error: failed to push some refs to '[email protected]:JasonLi-cn/test.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
git pull origin master这时可能会遇到问题:
$ git pull origin master From github.com:JasonLi-cn/test * branch master -> FETCH_HEAD fatal: refusing to merge unrelated histories解决办法:
git pull origin master --allow-unrelated-histories这时就可以push了.
如果远程仓库有和本地仓库一样的文件还不能直接push,git status发现:
On branch master You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both added: LICENSE只需要执行如下操作然后重新提交就行了
Shellgit rm LICENSE git add . git commit -m "merge" git push origin master
最好推荐几篇写的非常不错的git入门文章:
从0开始学习 GitHub 系列之「向GitHub 提交代码」

