使用Xcode上傳代碼至GitHub

來源:互聯網
上載者:User

幾乎所有iOS程式員都上過GitHub尋找開源類庫,的確,GitHub上有大量優秀的開源類庫供大家學習。但是如何在Xcode中上傳代碼至GitHub呢?

 

(開始之前先安裝git,具體方法這裡講的很清楚:http://git.oschina.net/progit/1-起步.html)

開始

首先我們建立一個工程,記得要勾選Create git repository on:

這說明使用Source Control,會預設在工程中建立git repository。然後工程建立完成後,會在右側邊欄看到這些資訊,說明已經啟用Source Control

如果沒有使用Source Control,則是這樣的:

現在我們已經在工程中啟用了Source Control,這樣就可以使用git來管理工程版本了

但是如果我們想對一個未啟用git的工程加入git的功能怎麼做呢?我們可以使用命令列來開啟此功能,建立一個工程,不勾選Create git repository on,此時我們沒有開啟Source Control,然後我們手動建立git管理,如所示:

YiBantekiiMac-3:UseGit YiBan$ cd /Users/YiBan/Documents/iOS_Dev/ManualGitDemoYiBantekiiMac-3:ManualGitDemo YiBan$ git initInitialized empty Git repository in /Users/YiBan/Documents/iOS_Dev/ManualGitDemo/.git/

使用

git init

來初始化一個空的git倉庫,現在使用ls-la命令查看目錄下的所有檔案(包含隱藏檔案)

total 16
drwxr-xr-x   7 YiBan  staff   238  5 12 16:10 .drwxr-xr-x  52 YiBan  staff  1768  5 12 16:06 ..-rw-r--r--@  1 YiBan  staff  6148  5 12 16:10 .DS_Storedrwxr-xr-x   9 YiBan  staff   306  5 12 16:06 .gitdrwxr-xr-x  12 YiBan  staff   408  5 12 16:06 ManualGitDemodrwxr-xr-x   5 YiBan  staff   170  5 12 16:06 ManualGitDemo.xcodeprojdrwxr-xr-x   5 YiBan  staff   170  5 12 16:06 ManualGitDemoTests

此時我們看到除了三個檔案之外還有兩個隱藏檔案,.DS_Store和.git,.DS_Store是由OS X產生的檔案,包含了檔案夾中的位置屬性,.git則是啟用了Source Control自動產生的目錄,然後使用git status查看目前狀態:

YiBantekiiMac-3:ManualGitDemo YiBan$ git statusOn branch masterInitial commitUntracked files:  (use "git add <file>..." to include in what will be committed)    .DS_Store    ManualGitDemo.xcodeproj/    ManualGitDemo/    ManualGitDemoTests/nothing added to commit but untracked files present (use "git add" to track)

說明初始化成功了,顯示出了未被追蹤的檔案。不過我們並不希望把.DS_Store也加入的git中,因為那檔案對我們沒有任何用處,我們可以忽略它,具體做法是:建立一個檔案,命名為.gitignore,然後使用文字編輯器輸入以下資訊:

# Xcode

.DS_Store

*/build/*

*.pbxuser !default.pbxuser *.mode1v3 !default.mode1v3 *.mode2v3 !default.mode2v3 *.perspectivev3 !default.perspectivev3 xcuserdataprofile
*.moved-aside DerivedData.idea/*.hmap

儲存至工程檔案夾中,這樣我們目錄中就多出一個.gitignore檔案了,這時我們再用git status命令查看目前狀態:

YiBantekiiMac-3:ManualGitDemo YiBan$ git statusOn branch masterInitial commitUntracked files:  (use "git add <file>..." to include in what will be committed)    .gitignore    ManualGitDemo.xcodeproj/    ManualGitDemo/    ManualGitDemoTests/nothing added to commit but untracked files present (use "git add" to track)

這裡看到已經沒有.DS_Store了,說明.gitignore已經把.DS_Store忽略了。現在可以提交了,使用

git add .

此命令先將檔案添加至暫存地區,但還沒有提交,查看下狀態:

YiBantekiiMac-3:ManualGitDemo YiBan$ git statusOn branch masterInitial commitChanges to be committed:  (use "git rm --cached <file>..." to unstage)    new file:   .gitignore    new file:   ManualGitDemo.xcodeproj/project.pbxproj    new file:   ManualGitDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata    new file:   ManualGitDemo/AppDelegate.h    new file:   ManualGitDemo/AppDelegate.m    new file:   ManualGitDemo/Base.lproj/Main.storyboard    new file:   ManualGitDemo/Images.xcassets/AppIcon.appiconset/Contents.json    new file:   ManualGitDemo/Images.xcassets/LaunchImage.launchimage/Contents.json    new file:   ManualGitDemo/ManualGitDemo-Info.plist    new file:   ManualGitDemo/ManualGitDemo-Prefix.pch    new file:   ManualGitDemo/ViewController.h    new file:   ManualGitDemo/ViewController.m    new file:   ManualGitDemo/en.lproj/InfoPlist.strings    new file:   ManualGitDemo/main.m    new file:   ManualGitDemoTests/ManualGitDemoTests-Info.plist    new file:   ManualGitDemoTests/ManualGitDemoTests.m    new file:   ManualGitDemoTests/en.lproj/InfoPlist.strings

現在進行提交,使用git commit -m "Initail"命令,引號內的內容是提交的注釋,隨便寫什麼都可以:

YiBantekiiMac-3:ManualGitDemo YiBan$ git commit -m "Initial"[master (root-commit) 83bbefc] Initial 17 files changed, 803 insertions(+) create mode 100644 .gitignore create mode 100644 ManualGitDemo.xcodeproj/project.pbxproj create mode 100644 ManualGitDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 ManualGitDemo/AppDelegate.h create mode 100644 ManualGitDemo/AppDelegate.m create mode 100644 ManualGitDemo/Base.lproj/Main.storyboard create mode 100644 ManualGitDemo/Images.xcassets/AppIcon.appiconset/Contents.json create mode 100644 ManualGitDemo/Images.xcassets/LaunchImage.launchimage/Contents.json create mode 100644 ManualGitDemo/ManualGitDemo-Info.plist create mode 100644 ManualGitDemo/ManualGitDemo-Prefix.pch create mode 100644 ManualGitDemo/ViewController.h create mode 100644 ManualGitDemo/ViewController.m create mode 100644 ManualGitDemo/en.lproj/InfoPlist.strings create mode 100644 ManualGitDemo/main.m create mode 100644 ManualGitDemoTests/ManualGitDemoTests-Info.plist create mode 100644 ManualGitDemoTests/ManualGitDemoTests.m create mode 100644 ManualGitDemoTests/en.lproj/InfoPlist.strings

再查看下狀態:

YiBantekiiMac-3:ManualGitDemo YiBan$ git statusOn branch masternothing to commit, working directory clean

好了,當前工作區是乾淨的,代碼都已經提交完畢了。我們可以用Xcode提交代碼,也可以用命令來提交,但是用命令列的話可以做的事情更多一些。使用Xcode可以查看提交的曆史紀錄,Source Control->History:

 

添加工程至GitHub

首先必須有GitHub的帳號,沒有的話去註冊一個,並且還要建立SSH,GitHub使用了公私密鑰,確保與你的電腦通訊過程是安全的。

SSH建立過程是這樣的:

1. 在命令列輸入cd ~/.ssh,然後ls,看看此檔案夾下有哪些檔案,如果有id_rsa.pub或者id_dsa.pub(名字可能會不同),說明你已經有SSH keys了,你可以將它添加到你的賬戶中

2. 如果沒有的話,你講得到"No such file or directory "這個錯誤資訊,此時你可以通過命令產生出來:

ssh-keygen -t rsa -C "YOUR EMAIL"

在那裡填寫你的email地址,之後會被要求填寫密碼,此時的SSH keys就產生好了,有了SSH Keys後將其添加至你的GitHub賬戶中就可以了,在賬戶設定中找到SSH keys這一項,然後填寫title和key,現在,你的SSH Key就和GitHub賬戶綁定了

前往個人首頁,建立一個repository(網頁右上方),會要輸入一些資訊:

輸入Repository name和描述,然後選建立,會看到repository的連結:

把連結賦值下來,前往Xcode中,Source Control->第一項->Configure...,之後選Remotes:

Add Remote中,輸入Name(你工程的名字)和Address(之前的連結地址),然後Source Control->Push,選擇剛剛建立的連結,Push~

現在重新整理下GitHub首頁,你的工程已經添加成功了~!

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.