解決 git 執行錯誤 bad CPU type in executable: /usr/local/bin/git

macOS 更新後,執行 git 遇到「bad CPU type in executable:」錯誤,解決方式是切換成另一個可以運作的 git

Photo by Negative Space on StockSnap


錯誤狀況

macOS 更新後,執行 git 遇到錯誤

zsh: bad CPU type in executable: /usr/local/bin/git


問題原因

macOS 電腦安裝了兩個版本的 git,原本使用的 git 版本不支援新的作業系統:

/usr/local/bin/git 連到 /usr/local/git/bin/git,這是 2022 年用 git-osx-installer 裝的舊版 git,只有 x86_64 和 i386 版本。你的 Mac 是 Apple Silicon(arm64),又沒有執行 Rosetta(oahd 這個 Rosetta 背景程序沒在跑),所以系統無法執行它。

但是內建的 git 可以正常使用

/usr/bin/git -v

git version 2.54.0 (Apple Git-157)

查看 GIT 官方網頁最新版本是 2.55.0 版本,只差了一個版本號,所以直接切換成內建的 git。


解決方式

1. 移除舊版 git

開啟終端機 APP,輸入以下指令

sudo /usr/local/git/uninstall.sh

過程中會詢問輸入 yes 再確認是否刪除 git

This will uninstall git by removing /usr/local/git/, and symlinks

Type 'yes' if you are sure you wish to continue: yes

Forgot package 'com.git.pkg' on '/'.

Uninstalled

2. 刪除 ~/.zshrc 檔案中設定的舊版 git 連結關係

使用文字編輯程式,刪除以下這一行

alias git='/usr/local/bin/git'

重新開啟終端機 APP

3. 確認刪除結果

終端機 APP,輸入以下指令 git -v

確認有順利顯示版本號

參考資料


留言