幾乎全部iOS程序員都上過GitHub尋找開源類庫,的確,GitHub上有大量優秀的開源類庫供你們學習。可是如何在Xcode中上傳代碼至GitHub呢?html
(開始以前先安裝git,具體方法這裏講的很清楚:http://git.oschina.net/progit/1-起步.html)git
開始
首先咱們新建一個工程,記得要勾選Create git repository on:程序員

這說明使用Source Control,會默認在工程中建立git repository。而後工程新建完成後,會在右側邊欄看到這些信息,說明已經啓用Source Controljson

若是沒有使用Source Control,則是這樣的:xcode

如今咱們已經在工程中啓用了Source Control,這樣就可使用git來管理工程版本了安全
可是若是咱們想對一個未啓用git的工程加入git的功能怎麼作呢?咱們可使用命令行來開啓此功能,新建一個工程,不勾選Create git repository on,此時咱們沒有開啓Source Control,而後咱們手動建立git管理,以下圖所示:app
1ssh 2編輯器 3ide |
YiBantekiiMac-3:UseGit YiBan$ cd /Users/YiBan/Documents/iOS_Dev/ManualGitDemo YiBantekiiMac-3:ManualGitDemo YiBan$ git init Initialized empty Git repository in /Users/YiBan/Documents/iOS_Dev/ManualGitDemo/.git/ |
使用
來初始化一個空的git倉庫,如今使用ls-la命令查看目錄下的全部文件(包含隱藏文件)
1 2 3 4 5 6 7 |
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_Store drwxr-xr-x 9 YiBan staff 306 5 12 16:06 .git drwxr-xr-x 12 YiBan staff 408 5 12 16:06 ManualGitDemo drwxr-xr-x 5 YiBan staff 170 5 12 16:06 ManualGitDemo.xcodeproj drwxr-xr-x 5 YiBan staff 170 5 12 16:06 ManualGitDemoTests |
此時咱們看到除了三個文件以外還有兩個隱藏文件,.DS_Store和.git,.DS_Store是由OS X生成的文件,包含了文件夾中的位置屬性,.git則是啓用了Source Control自動生成的目錄,而後使用git status查看當前狀態:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
YiBantekiiMac-3:ManualGitDemo YiBan$ git status On branch master Initial commit Untracked 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,而後使用文本編輯器輸入如下信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Xcode
<br>.DS_Store
<br>*/build/*<br>
*.pbxuser ! default .pbxuser *.mode1v3 ! default .mode1v3 *.mode2v3 ! default .mode2v3 *.perspectivev3 ! default .perspectivev3 xcuserdata profile <br>*.moved-aside DerivedData .idea/ *.hmap |
保存至工程文件夾中,這樣咱們目錄中就多出一個.gitignore文件了,這時咱們再用git status命令查看當前狀態:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
YiBantekiiMac-3:ManualGitDemo YiBan$ git status On branch master Initial commit Untracked 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忽略了。如今能夠提交了,使用
此命令先將文件添加至暫存區域,但尚未提交,查看下狀態:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
YiBantekiiMac-3:ManualGitDemo YiBan$ git status On branch master Initial commit Changes 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"命令,引號內的內容是提交的註釋,隨便寫什麼均可以:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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 |
再查看下狀態:
1 2 3 |
YiBantekiiMac-3:ManualGitDemo YiBan$ git status On branch master nothing 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"這個錯誤信息,此時你能夠經過命令生成出來:
1 |
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主頁,你的工程已經添加成功了~!
因爲本身也遇到了一樣問題,找到的解決辦法怕本身忘啦,特複製的解決辦法