MAC上Git安裝與GitHub基本使用


一、安裝Git

MAC 上安裝Git主要有兩種方式
首先查看電腦是否安裝Git,終端輸入:

git

安裝過則會輸出:

WMBdeMacBook-Pro:~ WENBO$ git
usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

1、通過homebrew安裝Git

  • 1、未安裝homebrew,需安裝homebrew

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

  • 2、安裝git

brew install git

2、通過Xcode安裝

直接從AppStore安裝Xcode,Xcode集成了Git,不過默認沒有安裝,你需要運行Xcode,選擇菜單“Xcode”->“Preferences”,在彈出窗口中找到“Downloads”,選擇“Command Line Tools”,點“Install”就可以完成安裝了。

二、創建ssh key、配置git

  • 1、設置username和email(github每次commit都會記錄他們)

git config --global user.name “wenbo”
git config --global user.email “[email protected]

  • 2、通過終端命令創建ssh key

ssh-keygen -t rsa -C “[email protected]

[email protected]是我的郵件名,回車會有以下輸出

Last login: Sat Jan  6 14:12:16 on ttys000
WMBdeMacBook-Pro:~ WENBO$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/WENBO/.ssh/id_rsa): 
/Users/WENBO/.ssh/id_rsa already exists.
Overwrite (y/n)? n
WMBdeMacBook-Pro:~ WENBO$ 

由於這裏我原來已經創建過,這裏我選n,沒有創建過的,會要求確認路徑和輸入密碼,我們這使用默認的一路回車就行。成功的話會在~/下生成.ssh文件夾,進去,打開id_rsa.pub,複製裏面的key。
終端查看.ssh/id_rsa.pub文件

open .ssh/id_rsa.pub

回車後,就會新彈出一個終端,然後複製裏面的key。
或者用cat命令查看

cat .ssh/id_rsa.pub

  • 3、登錄GitHub(默認你已經註冊了GitHub賬號),添加ssh key,點擊Settings,如圖

在這裏插入圖片描述

點擊New SSH key,如圖

在這裏插入圖片描述

添加key,如圖

在這裏插入圖片描述

  • 4、鏈接驗證

ssh -T [email protected]

終端輸出結果

Last login: Sat Jan  6 14:42:55 on ttys000
WMBdeMacBook-Pro:~ WENBO$ ssh -T [email protected] 
Hi wenmobo! You've successfully authenticated, but GitHub does not provide shell access.
WMBdeMacBook-Pro:~ WENBO$ 

說明已經鏈接成功。

發佈了77 篇原創文章 · 獲贊 14 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章