GitHub-使用教程(上传代码)

一、下载Git并安装 Git for Windows

Git-2.17.0-64-bit.exe  按照默认步骤完成安装

二、设置SSH建立计算机与Github的链接

1、点击 开始菜单找到Git Bash


2、在git bash上运行命令 $ cd ~/.ssh 检查自己电脑上是否存在ssh keys

如果显示No such file or directory 则需要去创建一个新的ssh keys


3、创建新的ssh keys

运行命令:

$ ssh-keygen -t rsa -C "[email protected]" 点击回车

输入两次密码
注:在Enter passphrase 的时候,输入的密码是看不到的,其实已经输入了,输完后点击回车就可以了

这样一个新的keys就创建完成了,上面代码显示,密匙位置放在了C:/Users/用户名/.ssh/文件夹中。(.ssh文件夹可能是隐藏的,需要查看隐藏文件)


4、将生成的ssh keys 添加到github中

4.1 访问https://github.com/plans 先注册一个账号后, 点击“Account Settings” > 点击 “SSH Public Keys” > 点击“Add SSH  key”


在本机找到你创建的密匙文件id_rsa.pub ,使用记事本打开,复制里面所有的内容,粘贴到网站key的文本框中,点击Add Key 保存。

4.2 测试设置是否正确

输入命令:$ ssh -T [email protected]


输入$ yes
输入前面自己设置的passphrase,回车,显示如下即成功(忽略警告)


三、在本地设置Git信息,设置用户名和邮箱

$ git config --global user.name "Firstname Lastname"

$ git config --global user.email "[email protected]"


四、Git创建一个库

点击new repository,输入repository名称,勾选“Initialize this repository with a README”复选框

五、上传项目代码

5.1 先clone刚才新建的repository 到本地

在要放置的硬盘文件夹位置, 右击鼠标,点击Git Bash
输入命令:$ git clone https://github.com/Vmetrio/Register.git,在本地生成了Register文件夹


5.2 这个步骤以后你的本地项目文件夹下面就会多出个文件夹,该文件夹名即为你github上面的项目名,如图我多出了个Register文件夹,我们把本地项目文件夹下的所有文件(除了新多出的那个文件夹不用),其余都复制到那个新多出的文件夹下。

5.3 接着继续输入命令 cd Register,进入Register文件夹


5.4执行以下5个命令

$ git init  //命令1,初始化

$ git add .  //注:别忘记后面的 . ,此操作是把Test文件夹下面的文件都添加进来

$ git commit -m '提交说明'    //注:“提交信息”里面换成你需要,如“first commit”

$ git remote add origin [email protected]:github用户名/myssh.git  //命令4,为仓库添加源地址

$ git push origin master  //注:此操作目的是把本地仓库push到github上面,此步骤需要你输入帐号和密码



常见错误:
1、执行命令4时,出现错误:
fatal: remote origin already exists
则执行语句:$ git remote rm origin   //执行后,再重新执行命令4,就不会报错

2、执行命令5时,出现错误:
error:failed to push som refs to.......
则执行语句:$ git pull origin master  //先把远程服务器github上面的文件拉下来,再输入$ git push origin master

3、将命令4写成
$ git remote add origin https://github.com/Vmetrio/Register.git 可能会出现错误:
unable to find remote helper for 'htts'
解决方法,改成$ git remote add origin [email protected]:Vmetrio/Register.git


内容借鉴:https://www.cnblogs.com/jenniferhuang/p/3355248.html

猜你喜欢

转载自blog.csdn.net/w_meng_h/article/details/80062730