git上传代码到github

git上传代码到github

准备

安装git,拥有github账户

方法

1.配置github
(1) 首先在本地创建ssh key
$ ssh-keygen -t rsa -C “youremail@youremail.com”
后面的your_email@youremail.com改为你的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在C:/当前用户下生成.ssh文件夹,打开id_rsa.pub,复制里面的全部内容。回到github,进入Settings,SSH and GPG keys,Add SSH Key,填写title,粘贴赋值的内容。
(2)为了验证是否成功,在git bash下输入:
$ ssh -T git@github.com
如果是第一次的会提示是否continue,输入yes就会看到:You’ve successfully authenticated, but GitHub does not provide shell access 表示已成功连上github。
2. 进入要上传的代码的文件夹,右键git bash
初始化仓库
$ git init
添加远程地址:
$ git remote add origin git@github.com:yourName/yourRepo.git
后面的yourName和yourRepo表示你再github的用户名和刚才新建的仓库,以后提交代码到github,origin就等同于git@github.com:yourName/yourRepo.git的别名.加完之后进入.git,打开config,这里会多出一个remote “origin”内容,这就是刚才添加的远程地址,也可以直接修改config来配置远程地址。
3.上传代码前,需要从github那拉取代码
$ git pull
如果提示如下:

error: failed to push some refs to 'https://github.com/dummymare/Hello-World.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代码,把本地代码更新到最新
git pull出错,提示如下:
qq_20131210105027
执行命令:git branch –set-upstream-to=origin/ master,替换为自己的分支名
4.git branch -a 无法查看到所有的分支的异常处理,若没有跳过
$ git pull +github中的项目URL
拉取一次代码后,再次git branch -a ,就可以查看到本地的master分支,如果无法查看到本地分支无法git push代码
5.git status 查看文件状态
红色 本地未提交到暂存区的文件
绿色 提交到暂存区的文件
6.git add 添加文件到暂存区
$ git add . 添加所有的文件到暂存区
$ git add +filename 添加file到暂存区
7.git commit 提交文件到本地仓库
$ git commit -m “提交的描述信息”
8.git push 提交代码到远程仓库
$ git push origin master:master 提交本地master分支作为远程的master分支

posted @ 2016-10-19 19:54  Mr青春  阅读(194)  评论(0编辑  收藏  举报