现在的情景是,你已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过该仓库来协作,真是一举多得。
首先
登陆GitHub,然后,在右上角找到“Create a new repo”按钮,创建一个新的仓库:
在Repository name填入learngit,其他保持默认设置,点击“Create repository”按钮,就成功地创建了一个新的Git仓库:
目前,在GitHub上的这个learngit仓库还是空的,GitHub告诉我们,可以从这个仓库克隆出新的仓库,也可以把一个已有的本地仓库与之关联,然后,把本地仓库的内容推送到GitHub仓库。
现在,我们根据GitHub的提示,在本地的learngit仓库下运行命令:
重点:关联本地库到远程库(另一种方法git clone)
git remote add origin git@github.com:123/learngit.git
下一步
就可以把本地库的所有内容推送到远程库上:
$ git push -u origin master
把本地库的内容推送到远程,用git push命令,实际上是把当前分支master推送到远程。
由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。
如果报错,错误如下:
lihy:mybatis lhy$ git push -u origin master
To https://github.com/123/Mybatis.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to ‘https://github.com/123/Mybatis.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.
解决办法:
git pull --rebase origin master
这样就好了,因为你在github上创建了git仓库 ,但是创建的时候多了一个readme.md文件,所以和本地仓库代码没有同步,
所以需要执行:git pull --rebase origin master
git本地代码提交到github远程仓库方法一
最新推荐文章于 2024-07-20 18:31:09 发布