MAC上Git安裝與GitHub基本使用

屏幕快照 2018-01-06 下午4.16.20.png

目錄

  • 安裝git
  • 創(chuàng)建ssh key、配置git
  • 提交本地項(xiàng)目到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,不過默認(rèn)沒有安裝,你需要運(yùn)行Xcode,選擇菜單“Xcode”->“Preferences”,在彈出窗口中找到“Downloads”,選擇“Command Line Tools”,點(diǎn)“Install”就可以完成安裝了。

二、創(chuàng)建ssh key、配置git

  • 1、設(shè)置username和email(github每次commit都會記錄他們)
git config --global user.name "wenbo"
git config --global user.email "1050794513@qq.com"
  • 2、通過終端命令創(chuàng)建ssh key
ssh-keygen -t rsa -C "1050794513@qq.com"

1050794513@qq.com是我的郵件名,回車會有以下輸出

Last login: Sat Jan  6 14:12:16 on ttys000
WMBdeMacBook-Pro:~ WENBO$ ssh-keygen -t rsa -C "1050794513@qq.com"
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$ 

由于這里我原來已經(jīng)創(chuàng)建過,這里我選n,沒有創(chuàng)建過的,會要求確認(rèn)路徑和輸入密碼,我們這使用默認(rèn)的一路回車就行。成功的話會在~/下生成.ssh文件夾,進(jìn)去,打開id_rsa.pub,復(fù)制里面的key。
終端查看.ssh/id_rsa.pub文件

open .ssh/id_rsa.pub 

回車后,就會新彈出一個(gè)終端,然后復(fù)制里面的key。
或者用cat命令查看

cat .ssh/id_rsa.pub
  • 3、登錄GitHub(默認(rèn)你已經(jīng)注冊了GitHub賬號),添加ssh key,點(diǎn)擊Settings,如圖
    屏幕快照 2018-01-06 下午2.40.11.png

    點(diǎn)擊New SSH key,如圖
    屏幕快照 2018-01-06 下午2.57.15.png

    添加key,如圖
    屏幕快照 2018-01-06 下午2.53.07.png
  • 4、鏈接驗(yàn)證
ssh -T git@github.com 

終端輸出結(jié)果

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

說明已經(jīng)鏈接成功。

三、提交本地項(xiàng)目到GitHub

  • 1、在GitHub上新創(chuàng)建一個(gè) repository或者Start a Project,如圖:
    屏幕快照 2018-01-06 下午3.21.40.png
  • 2、填寫項(xiàng)目信息,如下圖所示:
    屏幕快照 2018-01-06 下午3.27.27.png

    點(diǎn)擊Create repository,就創(chuàng)好一個(gè)工程了。
  • 3、Clone工程到本地,首先復(fù)制ssh 地址


    屏幕快照 2018-01-06 下午3.32.04.png

    打開終端,這里只是測試,我想把工程克隆在桌面,首先在終端中切換路徑到桌面,輸入以下命令:

cd /Users/WENBO/Desktop/

然后克隆項(xiàng)目,終端輸入

git clone git@github.com:wenmobo/LearnGit.git

git@github.com:wenmobo/LearnGit.git是剛剛復(fù)制的ssh路徑。
終端完整輸出如下:

Last login: Sat Jan  6 15:17:17 on ttys000
WMBdeMacBook-Pro:~ WENBO$ cd /Users/WENBO/Desktop/
WMBdeMacBook-Pro:Desktop WENBO$ git clone git@github.com:wenmobo/LearnGit.git
Cloning into 'LearnGit'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (5/5), 5.2

這時(shí),工程已經(jīng)被克隆到桌面了,如下圖:


屏幕快照 2018-01-06 下午3.42.57.png
  • 4、在Xcode中新創(chuàng)建一個(gè)工程,保存的路徑為剛剛克隆下來的LearnGit文件夾下,如下圖所示:
    屏幕快照 2018-01-06 下午3.48.59.png
  • 5、提交修改,首先切換到LearnGit文件路徑:
cd /Users/WENBO/Desktop/LearnGit 

然后輸入:

//文件添加到倉庫(.代表提交所有文件)
git add .
//把文件提交到倉庫
git commit -m "First Commit"
//上傳到github
git push

終端完整輸出如下:

Last login: Sat Jan  6 15:49:54 on ttys000
WMBdeMacBook-Pro:~ WENBO$ cd /Users/WENBO/Desktop/LearnGit 
WMBdeMacBook-Pro:LearnGit WENBO$ git add .
WMBdeMacBook-Pro:LearnGit WENBO$ git commit -m "First Commit"
[master ae3bbe9] First Commit
 11 files changed, 649 insertions(+)
 create mode 100644 LearnGitDemo/LearnGitDemo.xcodeproj/project.pbxproj
 create mode 100644 LearnGitDemo/LearnGitDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata
 create mode 100644 LearnGitDemo/LearnGitDemo/AppDelegate.h
 create mode 100644 LearnGitDemo/LearnGitDemo/AppDelegate.m
 create mode 100644 LearnGitDemo/LearnGitDemo/Assets.xcassets/AppIcon.appiconset/Contents.json
 create mode 100644 LearnGitDemo/LearnGitDemo/Base.lproj/LaunchScreen.storyboard
 create mode 100644 LearnGitDemo/LearnGitDemo/Base.lproj/Main.storyboard
 create mode 100644 LearnGitDemo/LearnGitDemo/Info.plist
 create mode 100644 LearnGitDemo/LearnGitDemo/ViewController.h
 create mode 100644 LearnGitDemo/LearnGitDemo/ViewController.m
 create mode 100644 LearnGitDemo/LearnGitDemo/main.m
WMBdeMacBook-Pro:LearnGit WENBO$ git push
Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.
Counting objects: 20, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (20/20), 6.80 KiB | 0 bytes/s, done.
Total 20 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To github.com:wenmobo/LearnGit.git
   1000218..ae3bbe9  master -> master
WMBdeMacBook-Pro:LearnGit WENBO$ 

查看GitHub上的項(xiàng)目,LearnGit已經(jīng)上傳成功啦,如下圖所示:

屏幕快照 2018-01-06 下午3.54.48.png

總結(jié)

自己現(xiàn)在公司項(xiàng)目都是用SVN托管的,沒有用Git托管。用到Git的地方就是用碼云托管自己寫的小Demo,我也是通過這篇文章了解下上傳項(xiàng)目到GitHub的基本流程,和碼云上傳其實(shí)也沒多大的區(qū)別,碼云上傳我沒有用終端,而是用的SourceTree,Git圖形化工具還是挺多的,可以自己去了解與使用吧。文章中所寫的也是一些最基本的使用,還是多多了解下吧,萬一以后的公司要用呢,哈哈。

參考文章

1、Git教程
2、【Github教程】史上最全github使用方法:github入門到精通
3、GotGitHub

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.git的安裝 1.1 在Windows上安裝Git msysgit是Windows版的Git,從https:/...
    落魂灬閱讀 12,834評論 4 54
  • 二十四個(gè)春秋交替,是,的確是二十四個(gè),沒有多一個(gè)也沒有少一個(gè),沒有多一天,也沒有少一天,終于我們成了他們口中的和我...
    一十六洲閱讀 1,408評論 0 2
  • 姓名:郭志宏 公司:內(nèi)蒙古金仕頓大酒店 【日精進(jìn)打卡第63天】 一、【知~學(xué)習(xí)】 《六項(xiàng)精進(jìn)》大綱1遍共309遍 ...
    黑山小妖G閱讀 212評論 0 0
  • 文/大劉的自由時(shí)光 追求快樂、安逸是人的本性,每個(gè)人都想要舒服閑適的生活狀態(tài),拒絕勞累和奔波。這一點(diǎn),不管什么年代...
    大劉的英語世界閱讀 4,205評論 1 4
  • 很久很久以前,有一個(gè)叫云的人,他很溫柔,對人很好,很有禮。你走在他身邊,和他說話,他會很溫柔的回答你。 有一個(gè)叫風(fēng)...
    落燼喵閱讀 218評論 0 1

友情鏈接更多精彩內(nèi)容