一、方法一(通过dmg包安装)
1、打开git官网下载页面,选择installer。
2、点击下载Download。
3、然后安装dmg包,安装完毕后,git会被安装到/usr/local/git/bin/目录下。
二、方法二(通过homebrew安装)
1、homebrew是什么?
Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。
2、安装homebrew
homebrew官网中介绍安装homebrew只需一句脚本。
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
通过终端输入此命令后,会出现连接拒绝。
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
解决办法一
- 修改hosts文件,添加以下内容到hosts中末尾。
sudo vi /etc/hosts
199.232.68.133 raw.githubusercontent.com
199.232.68.133 user-images.githubusercontent.com
199.232.68.133 avatars2.githubusercontent.com
199.232.68.133 avatars1.githubusercontent.com
- 重新输入安装脚本,泡一杯奶茶,静等下载(时间比较长)。
解决办法二
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
- 上面脚本中使用了中科大镜像来加速访问。
如果命令执行卡在下面信息:
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
Control + c中断脚本执行并运行以下命令:
cd "$(brew --repo)/Library/Taps/"
mkdir homebrew && cd homebrew
git clone git://mirrors.ustc.edu.cn/homebrew-core.git
成功执行之后继续执行安装命令:
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
3、输入命令brew install git
安装git。
三、配置git
1、设置username和email(github每次commit都会记录他们,建议名称与邮箱相同)
git config --global user.name "你的名称"
git config --global user.email "你的邮箱"
2、通过终端命令创建ssh key,需要回车三次。
ssh-keygen -t rsa -C "你的邮箱"
3、创建成功后会在~/下生成.ssh文件夹,打开id_rsa.pub文件,复制里面的key。
cat .ssh/id_rsa.pub
4、在github的setting下,添加ssh key,title随便写,key复制过来,保存,设置完成。
四、通过Sourcetree工具对git仓库进行管理
1、打开Sourcetree官网,下载Sourcetree并安装。
2、安装完成后,把本地git库直接拖入Sourcetree中,如图。
五、参考资料
1、https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git
2、https://github.com/hawtim/blog/issues/10
3、https://zhuanlan.zhihu.com/p/90508170