情景,本地git与远程Git仓库同步
Administrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit$ git initInitialized empty Git repository in D:/workspace/learngit/.git/Administrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ git remote add origin git@github.com:ZhuBaker/learngit.gitAdministrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ git push -u origin mastererror: src refspec master does not match any.error: failed to push some refs to 'git@github.com:ZhuBaker/learngit.git'
原因分析
引起该错误的原因是,目录中没有文件,空目录是不能提交上去的
解决方法
Administrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ touch READMYFILEAdministrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ git add READMYFILEAdministrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ git commit -m 'add READMYFILE'[master (root-commit) 28ed8af] add READMYFILE 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 READMYFILEAdministrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ git push -u origin masterTo github.com:ZhuBaker/learngit.git ! [rejected] master -> master (fetch first)error: failed to push some refs to 'git@github.com:ZhuBaker/learngit.git'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changeshint: (e.g., 'git pull ...') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.
提示首先要fetch 代码 或者 git pull
或者是强推 git push -f
Administrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ git pull origin masterFrom github.com:ZhuBaker/learngit * branch master -> FETCH_HEADfatal: refusing to merge unrelated histories
提示拒绝merge两个不相关的git
因为他们是两个不同的项目,要把两个不同的项目合并,git需要添加一句代码,在git pull,这句代码是在git 2.9.2版本发生的,最新的版本需要添加--allow-unrelated-histories
假如我们的源是origin,分支是master,那么我们 需要这样写git pull origin master --allow-unrelated-histories需要知道,我们的源可以是本地的路径
Administrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ git pull origin master --allow-unrelated-historiesFrom github.com:ZhuBaker/learngit * branch master -> FETCH_HEADMerge made by the 'recursive' strategy. abc.txt | 0 dd | 1 + ddd.txt | 0 3 files changed, 1 insertion(+) create mode 100644 abc.txt create mode 100644 dd create mode 100644 ddd.txtAdministrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ lsabc.txt dd ddd.txt READMYFILE
Administrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ git pullThere is no tracking information for the current branch.Please specify which branch you want to merge with.See git-pull(1) for details. git pullIf you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/ master
直接使用git pull 命令会提示该提示:
让我们使用 git pull 或者 建立本地分支与远程分支的关联才能使用
Administrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ git branch --set-upstream-to=origin/master masterBranch master set up to track remote branch master from origin.Administrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ git pullAlready up-to-date.Administrator@EZ-20170513RQHH MINGW32 /d/workspace/learngit (master)$ git pushCounting objects: 4, done.Delta compression using up to 4 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (4/4), 465 bytes | 0 bytes/s, done.Total 4 (delta 1), reused 0 (delta 0)remote: Resolving deltas: 100% (1/1), completed with 1 local object.To github.com:ZhuBaker/learngit.git 6af3724..c01f0f2 master -> master