首次使用git要做的工作:
- 配置用户信息
git config –global user.name “nc”
git config –global user.email “nicahead@gmail.com” - 配置SHH
配置shh key是让本地git项目与远程的github建立联系
先检查是否已经有SSH Key,打开Git Bash,输入cd ~/.ssh
如果没有.ssh这个目录,则生成一个新的SSH,输入ssh-keygen -t rsa -C "your e-mail"
接下来一顿回车,可能中间会输入yes
出现这种类型的图案,则配置成功
- 添加 SSH Key 到 GitHub
打开Git Bash,然后输入cd ~/.ssh
进入到.shh文件夹中再输入ls,查看是否有id_rsa.pub文件
输入cat命令,打开id_rsa.pub文件cat id_rsa.pub
将该ssh字符串复制下来,进入Github官网,点击+旁边的头像,再按settings进入设置 ,点击New SSH key创建新的 SSH Key - 验证配置是否成功
打开Git Bash,然后输入ssh -T git@github.com
, 显示success信息
一次完整的本地代码上传到github的操作:
- github上新建一个仓库 new repository
- 在本地项目的文件夹下执行
git init
命令 ,初始化版本库 - 执行
git add 文件名
,添加文件到版本库,可以执行git add .
将文件夹下的所有文件添到版本库 - 执行
git commit -m “标示”
,创建这次版本的标示
出现提示:On branch master nothing to commit, working directory clean
解决方法:rm -rf .git/
然后从第二步开始重新操作 - 和仓库建立连接
git remote add origin git@github.com:yourName/yourname.github.io.git
出现提示:fatal: remote origin already exists.
解决方法:git remote rm origin
- 开始上传
git push -u origin master
出现提示:fatal: unable to access 'https://github.com/yourname/yourname.github.io.git/': The requested URL returned error: 403
解决方法: 通过ssh的方式与仓库建立连接git remote rm origin
出现提示:$ git push -u origin master
To git@github.com:nicahead/nicahead.github.io.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:nicahead/nicahead.github.io.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 --rebase origin master 然后 git push -u origin master