创建密匙:
第1步:创建SSH Key。在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa
和id_rsa.pub
这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:
$ ssh-keygen -t rsa -C "youremail@example.com"
你需要把邮件地址换成你自己的邮件地址,然后一路回车,使用默认值即可,由于这个Key也不是用于军事目的,所以也无需设置密码。
如果一切顺利的话,可以在用户主目录里找到.ssh
目录,里面有id_rsa
和id_rsa.pub
两个文件,这两个就是SSH Key的秘钥对,id_rsa
是私钥,不能泄露出去,id_rsa.pub
是公钥,可以放心地告诉任何人。
第2步:登陆GitHub,打开“Account settings”,“SSH Keys”页面:
然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub
文件的内容:
为了验证是否成功,在git bash下输入:
$ ssh -T git@github.com
如果是第一次的会提示是否continue,输入yes就会看到:You've successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。
接下来我们要做的就是把本地仓库传到github上去,在此之前还需要设置username和email,因为github每次commit都会记录他们。
$ git config --global user.name "your name"
$ git config --global user.email "your_email@youremail.com"
添加一个新的仓库,并提交到github:
echo "# mylearn" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:Jackery/XXXXX.git
git push -u origin master
在上传本地项目的时候,出现一下错误:
$ git push -u origin master
To git@github.com:JackeryShh/testtest.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:Jackery/XXX.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.
当时没有仔细看错误提示,错误原因为远程项目仓库与本地不一致,需要先把远程repo更新到本地,不要覆盖自己写的程序。
【即使是新建的repo,创建后,如果带有ReadMe,在提交本地代码时,也会出现此问题】
更新命令为:git pull origin master
问题1: 如果我想提交本地项目中的所有文件,怎么操作呢(一般针对新建的项目的首次提交)?
答:
git remote add origin git@github.com:Jackery/XXXX.git
git add ./
git commit -m “commit all files in solution”
git push -u origin master
问题2: 如何获取github项目源码?
(1)http方式 git clone https://github.com/JackeryShh/testtest.git
(2) git方式
(3)ssh方式
(4) 直接下载zip包