背景
换电脑重装git,遇到一些坑
过程
1.查看是否已经有了ssh密钥:cd ~/.ssh
如果没有密钥(No such file or directory
)则不会有此文件夹,有则备份删除
2.生成SSH密钥$ ssh-keygen -t rsa -C "your_email@yourmail.com"
后面的your_email@youremail.com改为你的邮箱。
直接点回车,说明会在默认文件id_rsa上生成ssh key
然后系统要求输入密码,直接按回车表示不设密码
重复密码时也是直接回车,之后提示你shh key已经生成成功
最后得到了两个文件:id_rsa和id_rsa.pub
3.在github上添加ssh密钥,这要添加的是“id_rsa.pub”里面的公钥
github右上角个人中心->settings->SSH and GPG keys->New SSH key
title随便填,一般填写方便区分的名字,比如使用这个公钥的那台机器名,key粘贴刚才复制的公钥
4.验证是否成功$ ssh -T git@github.com
The authenticity of host ‘github.com (207.97.227.239)’ can’t be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘github.com,207.97.227.239′ (RSA) to the list of known hosts.
ERROR: Hi tekkub! You’ve successfully authenticated, but GitHub does not provide shell access
Connection to github.com closed.
5.使用GitHub
1)获取源码1
$ git clone git@github.com:snjylin/test.git
我这里安装的时候提示“git命令需要使用开发者工具。您要现在安装该工具吗”
查阅资料,可以通过Xcode安装,我这里安装了homebrew就正常了
安装 HomebrewHomebrew1
2/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew -v #验证brew是否安装成功
这个地方我安装完提示-bash: brew: command not found
,去网上找了解决办法,有一种提到比较多的方法1
2
3sudo vim .bash_profile #以root身份来打开并创建.bash_profile
export PATH=/usr/local/bin:$PATH #在.bash_profile文件中写入这句,为PATH添加/usr/local/bin的路径
source .bash_profile #更新配置后的环境变量
关闭Terminal再打开Terminal,brew -v
但是我这样操作并没有用,重新安装后就好了
2)在命令行上创建新的仓库,GitHub建议每个存储库都包含一个自述文件、许可证和.gitignore。1
2
3
4
5
6echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:snjylin/test.git
git push -u origin master
3)从命令行推送现有仓库1
2git remote add origin git@github.com:snjylin/test.git
git push -u origin master
4)从其他仓库导入代码
可以使用Subversion、Mercurial或TFS项目中的代码初始化仓库。
6.设置username和email,因为github每次commit都会记录他们
一般情况下需要设置全局config1
2$ git config --global user.name"your name"
$ git config --global user.email"your_email@youremail.com"
也可以每个项目单独设置config(因为之前公司项目和自己的项目是分开的,不是用的相同的config),cd到项目文件夹设置:1
2$ git config user.name"your name"
$ git config user.email"your_email@youremail.com"