標籤:

在 Mac 上使用 Git 一定要安裝 Xcode 嗎?

  • 問題背景: 最近更新了 Mac 系統 OS X EI Capitan 10.11 由於 Xcode 更新到 7 才能使用,於是卸載了 Xcode 。

此時我去官網下載Git (Git - Downloading Package) ,安裝後在終端輸入 git 命令 Mac 老提示讓我下載 Xcode ,Xcode 7.1 4G 多超難下(當時就崩潰了,偶滴歌神啊!),故求大神解答:在 Mac 上使用 Git 一定要安裝 Xcode 嗎?


親自測試過(請注意看錯誤提示)

-&> git
xcrun: error: active developer path
("/Applications/Xcode.app/Contents/Developer")
does not exist, use `xcode-select --switch path/to/Xcode.app`
to specify the Xcode that you wish to use for command line
developer tools (or see `man xcode-select`)

解決辦法

  1. xcode-select --install # 單獨安裝CommandLineTools,不需要Xcode
  2. sudo xcode-select --switch /Library/Developer/CommandLineTools # 指定路徑

再執行 git,顯示OK了。

-&> git
usage: git [--version] [--help] [-C &

] [-c name=value]
[--exec-path[=&

]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=&

] [--work-tree=&

] [--namespace=&]
& [&]


單獨安裝git for mac v2.10.1時本人也遇到了這個問題,上述回答無法解決,說到底,還是個環境變數path的配置優先問題。先來看安裝包中的說明文檔,其實已經可以發現問題了:

Install Package
------------------------
Double-click the package in this disk image to install. This installs
git to /usr/local/git, and places symlinks into /usr/local/bin and
/usr/share/man/.

Step 2 - Remove stubs (Yosemite and earlier)
--------------------------------------------
When you run `git`, you might see this message:

The "git" command requires the command line developer
tools. Would you like to install the tools now?"

This is because OS X ships with stubs, and these stubs are taking
precedence over /usr/local/bin.

To resolve, run the following:

sudo mv /usr/bin/git /usr/bin/git-system

This should not be a problem in OS X 10.11 (El Capitan), as
/usr/local/bin takes precedence over /usr/bin

mac中自帶的命令行工具配置目錄是/usr/bin,而我們新版的是/usr/local/bin。並且,它將/usr/local/bin 和/usr/share/man/ 中的git鏈接也替換成了新版,OSX10.11也默認/usr/local/bin的優先順序大於/usr/bin了,那我們的OSX10.11以上版本怎麼還會有錯呢?個人推測是由於我們是從舊版本系統一路升級而來,保留了原先的環境變數即path中的值。解決方法可按照上面 英文官方文檔中的來(未經測試),也可以參考下面這篇文章,下面是由於git版本老舊按照新版的問題,其實和我們的問題本質上一樣,我使用了第一種方法,簡單明了,完美解決。

——————————————————————————————————————————

Mac中git的安裝問題 - 昔我往矣,楊柳依依。 - SegmentFault

mac中的git默認是隨著xcode一起安裝的,所以一般不必手動安裝。但是隨著時間的推移,git版本不斷升級,自帶的git就顯得有點老舊了,因此我們需要自己來安裝最新的版本。

有兩種方式可以安裝git

  • 到GitGit下載最新的os x安裝包,一路next即可
  • 使用Homebrew

安裝完之後,執行git --version,如果你發現依舊是老版本,可以使用下面的方式解決。

以上兩種方式,最終都會把執行路徑放到/usr/local/bin下面,xcode安裝的git會放在/usr/bin下面,而且如果你查看一下PATH

echo $PATH

會發現,/usr/bin是處在/usr/local/bin前面的,所以就導致了舊版本依舊覆蓋了新版本。知道了原因之後,解決方式就很簡單了,我們只需要把/usr/local/bin放到前面即可。有兩種方式

第一種:

編輯/etc/paths(需要管理員許可權),調整/usr/local/bin在最前面

第二種:

編輯~/.bashrc或者~/.bash_profile

PATH=/usr/local/bin:$PATH
export PATH

然後執行source ~/.bashrc或者source ~/.bash_profile使改變生效。

此時再次執行git --version應該就會看到新版本了。


只要安裝 Command Line Tools就行。


根據提示安裝就行了,說的是安裝Xcode ,其實就裝一個Command Line Tools就行了,不到2分鐘搞定


推薦閱讀:

Mac上首次出現word宏惡意軟體 可竊取用戶敏感數據
為什麼攝影師愛用 Mac,僅僅是因為色彩好嗎?
互聯網優質資訊盡在 TechVendor(macOS)

TAG:Mac | Git |