【mac上安裝&配置&使用git】

轉自:https://www.jianshu.com/p/7edb6b838a2ehtml

目錄

  • 安裝git
  • 建立ssh key、配置git
  • 提交本地項目到GitHub

1、安裝Git

MAC 上安裝Git主要有兩種方式git

首先查看電腦是否安裝Git,終端輸入:github

git

安裝過則會輸出:shell

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.

 

一、經過homebrew安裝Git

  • 一、未安裝homebrew,需安裝homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

 

  • 二、安裝git
brew install git

 

二、經過Xcode安裝

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

2、建立ssh key、配置git

  • 一、設置username和email(github每次commit都會記錄他們)
git config --global user.name "wenbo" git config --global user.email "1050794513@qq.com"

 

  • 二、經過終端命令建立ssh key
ssh-keygen -t rsa -C "1050794513@qq.com"

 

1050794513@qq.com是個人郵件名,回車會有如下輸出xcode

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$

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

open .ssh/id_rsa.pub

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

cat .ssh/id_rsa.pub
  • 三、登陸GitHub(默認你已經註冊了GitHub帳號),添加ssh key,點擊Settings,如圖

 

點擊New SSH key,如圖ssh

添加key,如圖curl

 

  • 四、連接驗證
ssh -T git@github.com

終端輸出結果

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$

說明已經連接成功。

3、提交本地項目到GitHub

  • 一、在GitHub上新建立一個 repository或者Start a Project,如圖:

  • 二、填寫項目信息,以下圖所示:

點擊Create repository,就創好一個工程了。

  • 三、Clone工程到本地,首先複製ssh 地址

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

cd /Users/WENBO/Desktop/

而後克隆項目,終端輸入

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

git@github.com:wenmobo/LearnGit.git是剛剛複製的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

  • 四、在Xcode中新建立一個工程,保存的路徑爲剛剛克隆下來的LearnGit文件夾下,以下圖所示:

 

  • 五、提交修改,首先切換到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上的項目,已經上傳成功啦,以下圖所示:

相關文章
相關標籤/搜索