git在各操作系統平臺下的安裝和配置

git在各操作系統平臺下的安裝和配置

標籤 : git



本文是git系列博客的第一篇,主要介紹git在windows,linux,Mac OX等不同操作系統下的安裝和配置,主要以後兩者爲主。

工具下載

  • ubuntu:sudo apt-get intall git 安裝
  • windows:下載git for windows安裝即可
  • mac:個人對homebrew不是很安心,建議安裝macports,再用macports安裝git(sudo port install git +bash_completion)

環境相關配置

  • git config --global user.email "[email protected]" 配置郵件
  • git config --global user.name "Your Name" 配置用戶名
  • git config --global color.ui true 開啓顏色顯示
  • 創建SSH Key。在用戶主目錄下(~),看看有沒有.ssh目錄,如果有,再看看這個目錄下有沒有id_rsaid_rsa.pub這兩個文件,如果已經有了,可直接跳到下一步。如果沒有,打開Shell(Windows下打開Git Bash),創建SSH Key:ssh-keygen -t rsa -C "[email protected]"
  • 登陸GitHub,打開“Account settings”,“SSH Keys”頁面。然後,點“Add SSH Key”,填上任意Title,在Key文本框裏粘貼.ssh目錄下id_rsa.pub文件的內容,點“Add Key”

命令行顯示配置

  • 提示語換英語,mac下在.bash_profile裏添加下面內容,ubuntu在~/.profile下添加
# git language
export LANGUAGE='en_US.UTF-8 git'
  • 終端顯示分支,mac下在.bash_profile裏添加下面內容
# Git branch in prompt.

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h:\w\[\033[32m\]\$(parse_git_branch)\[\033[00m\]$ "

參考Add Git Branch Name to Terminal Prompt (Mac)

git補全

如果是linux或者windows用戶,一般不會出現這個問題,mac下我當時是bash環境沒設置好,按照這篇文章安的,使用的是Command Line Tools安裝的git,不能識別macports裏的git的補全。有幾種辦法可以完全補全

  • 方法一:直接下載補全文件並使其生效

參考git auto-complete for branches at the command line?

1.通過curl下載:curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

2.在~/.bash_profile裏添加

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi
  • 方法二:使用macports的bash環境

參考:How to use bash-completion

檢查的下自己的bash:which bash,which -a bash

  • 方法三:使用oh-my-zsh並啓用git plugins

檢查下支持的shell:cat /etc/shells
查看當前shell:echo $SHELL

參考資料


作者@brianway更多文章:個人網站 | CSDN | oschina

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章