55

MacOS 升级git

 5 years ago
source link: https://www.tuicool.com/articles/MN3ey2b
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

系统自带的git 版本一般会比较的老

比如我的macbook pro macOS Mojave 10.14.5 的git版本是 2.12

于是在日常开发中会遇到一个问题

想用 husky 等 工具控制 git hook 来控制不合规代码的 commit 就会报错,无法顺利执行 pre-commit 操作

那么保持git版本的更新就很必要了。

mac上最方便的 大概就是 HOMEBREW 了

所以我们就用 HOMEBREW 来安装可控的git吧。

但是 HOMEBREW 的标准镜像在国外,所以。。。连接很有问题。。。

所以我们先切换到国内镜像吧!

第一步安装(已安装的就忽略直接下一步)

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

第二步 切换国内镜像(清华大学的镜像站还是很给力的)

$ cd "$(brew --repo)"
$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

$ brew update

# 如果使用 iterm2 在 `.zshrc` 中 加入 `source ~/.bash_profile`
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile  
$ source ~/.bash_profile

第三步 更新 HOMEBREW

$ brew update && brew upgrade

第四步 安装git

$ brew install git

完美!

之后就能直接用 brew link git 控制 git的版本了

当然 以上并没有结束

你会发现你的 git版本还是老的。。。

这时候呢 我们打开 ~/.bash_profile

加入以下代码(一定要加在尽量靠下的地方 所有的 export 都是从上往下读取的,所以 越往下 权重越高)

# Add HOMEBREW tools to $PATH (/usr/local/bin)
export PATH=$(brew --prefix)/bin:$PATH

好了 我们重启下 Terminal 或者 iTerm2 再看一下 git --version

现在我们的 git 就变成 brew 安装的最新版的 git了~

精彩!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK