文章参考:https://www.centos.bz/2018/03/git-%E5%87%BA%E7%8E%B0-fatal-refusing-to-merge-unrelated-histories-%E9%94%99%E8%AF%AF/,https://blog.csdn.net/huanhuaqian/article/details/81986064。
使用git时遇到的问题及解决方法。
将代码上传到github时的步骤:
1.在本地建一个仓库,使用init命令。
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01
$ git init
Initialized empty Git repository in D:/workspace/flutter_app_demo01/.git/
2.在github上建一个仓库。
3.将两个仓库关联。
4.拉取或提交代码都可以。
------------------------------------------------------------------------
问题:执行commit时无法确定身份。
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$ git commit -m "first commit"
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
解决:
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$ git config --global user.email "ynparis@163.com"
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$ git config --global user.name "YNParis"
-------------------------------------------------------------------------
问题:push代码时报错,无法关联到远程仓库。
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$ git push origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
解决:关联到远程仓库。
1.
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$ git remote add origin git@github.com:YNParis/FlutterDome01.git
2.
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$ git fetch origin
warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:YNParis/FlutterDome01
* [new branch] master -> origin/master
3.
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$ git remote -v
origin git@github.com:YNParis/FlutterDome01.git (fetch)
origin git@github.com:YNParis/FlutterDome01.git (push)
4.
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$ git branch -a
* master
remotes/origin/master
问题:提交代码时版本落后
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$ git push origin master
To github.com:YNParis/FlutterDome01.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:YNParis/FlutterDome01.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决:拉取最新版本代码,合并后再提交。
问题:拉取代码时不关联的分支不能合并
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$ git pull origin master
From github.com:YNParis/FlutterDome01
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$
解决:加上--allow-unrelated-histories强制执行
nn@PARIS MINGW64 /d/workspace/flutter_app_demo01 (master)
$ git pull origin master --allow-unrelated-histories
From github.com:YNParis/FlutterDome01
* branch master -> FETCH_HEAD
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.