原文地址(超详细):http://blog.csdn.net/zamamiro/article/details/70172900
1、使用cd命令切换到项目目录,通过git init把它变成Git仓库,再通过git add .把项目添加到仓库;
2、再通过git commit -m "注释内容"把项目提交到仓库";
3、在Github上设置好SSH密钥后,新建一个远程仓库,通过
git remote add origin https://github.com/guyibang/TEST2.git
将本地仓库和远程仓库进行关联(这里为你的git项目的地址);
4、最后通过
git push -u origin master
把本地仓库的项目推送到远程仓库(也就是Github)上;
由于新建的远程仓库是空的,所以要加上-u这个参数
5、此时执行第5点命令可能会报如下错误:
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/shut-up/redisDemo.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 pull使远程仓库与本地仓库合并
7、此时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
8、此时再执行git pull命令,再执行git push -u origin master就可以上传成功了。
9、git pull 出现 fatal: refusing to merge unrelated histories 错误
其实这个问题是因为 两个 根本不相干的 git 库, 一个是本地库, 一个是远端库, 然后本地要去推送到远端, 远端觉得这个本地库跟自己不相干, 所以告知无法合并
具体的方法, 一个种方法: 是 从远端库拉下来代码 , 本地要加入的代码放到远端库下载到本地的库, 然后提交上去 , 因为这样的话, 你基于的库就是远端的库, 这是一次update了
第二种方法:
使用这个强制的方法
git pull origin master --allow-unrelated-histories
后面加上 --allow-unrelated-histories
, 把两段不相干的 分支进行强行合并
后面再push就可以了 git push gitlab master:init
gitlab是别名 , 使用
Java代码git remote add gitlab ssh://xzh@192.168.1.91:50022/opt/gitrepo/withholdings/WithholdingTransaction
master是本地的branch名字
init是远端要推送的branch名字
本地必须要先add ,commit完了 才能推上去
关于这个问题,可以参考http://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories。
在进行git pull 时,添加一个可选项
git pull origin master --allow-unrelated-histories