要将本地仓库中的代码推送到Github远程仓库,需要先在Github上创建一个新的仓库或者将本地已有的仓库与Github上的远程仓库关联起来。以下是具体步骤:
- 在Github上创建一个新的仓库,或者打开已有的仓库。
- 在本地使用命令行工具进入到需要推送到Github的项目文件夹中。
- 使用git remote add命令将本地仓库与Github远程仓库关联起来,例如:
git remote add origin https://github.com/your_username/your_repository.git
其中,origin是一个自定义的远程仓库名称,用于标识这个远程仓库;https://github.com/your_username/your_repository.git是你在Github上创建的远程仓库地址。
- 使用git add和git commit命令将本地修改后的代码提交到本地仓库中。
- 最后使用git push命令将代码推送到Github远程仓库中:
git push -u origin master
其中,-u选项表示关联当前分支与远程分支(第一次推送时需要加上),origin是之前添加过的远程仓库名称,master表示要推送到哪个分支。
如果遇到错误提示“Updates were rejected because the tip of your current branch is behind its remote counterpart”,可以尝试先执行以下操作:
git pull --rebase origin master
然后再执行git push命令进行推送。