如何将本地代码上传到GitHub上?
1. 打开网页版的GitHub,输入账号密码,登陆成功后,点击+,新建一个仓库“new repository‘’。(创建仓库需要输入你的仓库名,对这个仓库的描述,可以勾选上自动创建ReadMe文件)
2. 安装下载好Git
3. 进入你需要上传代码的文件夹,右击鼠标,打开“Git Bash Here”,进入命令行。
4. 首先在本地创建ssh key
命令:
$ ssh-keygen -t rsa -C "your_email@youremail.com"
后面的your_email@youremail.com
改为你在github上注册的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。
5. 成功的话会在~/
下生成.ssh
文件夹,进去,打开id_rsa.pub
,复制里面的key
。
6. 打开网页版的GitHub,点击 头像--》settings--》SSH and GPG keys-->New SSH key-->填好title,粘贴好你刚刚复制的key。
验证是否成功,在Git Bash中输入:
$ ssh -T git@github.com
如果是第一次的会提示是否continue,输入yes就会看到:You've successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。
7. 设置username和email,因为github每次commit都会记录他们。命令:
$ git config --global user.name "your name"
$ git config --global user.email "your_email@youremail.com"
8. 添加远程地址:
$ git remote add origin git@github.com:yourName/yourRepo.git
这个 git@github.com:yourName/yourRepo.git 就是你建的仓库的那个地址。方法:进入创建的仓库,点击 Clone or download,复制这个地址。
这时出错:
fatal: not a git repository (or any of the parent directories): .git
解决方法:
先输入命令:git init 再输入命令:git remote add origin git@github.com:yourName/yourRepo.git
9. 将你的工作目录添加到缓存区,命令:
git add <filename>
git add * 是添加目录下的所有文件
10. 使用如下命令以实际提交改动:
git commit -m "代码提交信息"
11. 可用命令:git status 查看提交状态。
12. 执行如下命令以将这些改动提交到远端仓库:
git push origin master
但是提交出现错误:
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:yangchao0718/cocos2d.git
hint: Updates were rejected because the tip of your current branch is behin
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.
错误原因:github中的README.md文件不在本地代码目录中
解决方法:https://jingyan.baidu.com/article/f3e34a12a25bc8f5ea65354a.html
输入命令:git pull --rebase origin master
再输入:git push -u origin master
13. 完成。
14. 如果你想把别人在GitHub上写的代码copy到本地,打开你要保存在本地的目录,右键鼠标--》Git Bash Here--》输入:
git clone 代码的SSH地址
15. 把本地更新后的代码传到GitHub仓库:
git add *
git commit -m "更新信息"
git push origin master
遇到的错误及解决方法: