本地写了很久的一个项目A,今天想上传到GitHub,但是由于新手,遇到了写问题。
上传步骤的正确姿势如下:
- git init 在项目目录下面执行该命令,让该目录编程一个git可以管理的仓库。
- git add . 将想要上传的代码文件或配置文件添加到暂存库中,“.”是该文件夹中的所有文件,不建议将一些本地文件上传如target目录下面的。
- git commit -m "git learning" 将暂存区的文件提交到个你的仓库。
- git remote add origin git@github.com:AAAAAA/BBBBB.git 关联到远程仓库,
- git remote -v 可以查看关联的远程仓库。
- git pull --rebase origin master 获取远程库与本地库同步,如果远程库不为空,必须同步
- git push -u origin master 将本地库内容push到远程库,完毕。
由于本人新手,忽略了第6步,导致了一系列的错误。接下来说一下各种报错,并讲一下其中原因。
- 没有第6步,直接git push 导致出错:
fatal: The current branch master has no upstream branch. //说明本地master分支没有远程仓库对应的分支
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master //提示信息为:将本地分支与远程origin对应
执行git push -u origin master
To github.com:AAAAA/BBBBB.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:AAAAA/BBBBB.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.
接下来根据提示执行:git push --set-upstream origin master,
error: failed to push some refs to 'git@github.com:AAAAAAA/BBBBBB.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.
其提示我的分支比较滞后,让先pull远程,接着执行git pull
There is no tracking information for the current branch. //当前分支没有被远程追踪
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
按照提示,我执行 git branch --set-upstream-to=origin/master master,
Branch 'master' set up to track remote branch 'master' from 'origin'.//发现本地分支关联了远程分支
于是重新git pull ,报错如下:
fatal: refusing to merge unrelated histories //就是本地项目与远程是不同的项目,进行pull会出现该错误,
于是执行git pull origin master --allow-unrelated-histories //允许不相关的项目合并。
问题解决。可以进行自由的git pull 与git push了。