很多Git命令都是在命令行下運行的,命令行下管理Git有很多優點不用多說。但最大的缺點是要求用戶記住這些命令。因此Git圖形界面還是很受一些用戶歡迎的,其中Xcode作為集成開發環境工具,也提供了一定Git圖形界面功能。但是要想在Xcode中使用Git管理工程代碼還想需要進行一些配置,然後才能使用。
如果我們是使用Xcode 4創建的一個iOS工程,在終端的命令行中提交代碼時候,可能會出現下面的部分信息:
[cpp]
create mode 100644 HelloWorld/HelloWorld.xcodeproj/project.xcworkspace/contents.xcworkspacedata
create mode 100644 HelloWorld/HelloWorld.xcodeproj/project.xcworkspace/xcuserdata/tonyguan.xcuserdatad/UserInterfaceState.xcuserstate
create mode 100644 HelloWorld/HelloWorld.xcodeproj/xcuserdata/tonyguan.xcuserdatad/xcschemes/HelloWorld.xcscheme
create mode 100644 HelloWorld/HelloWorld.xcodeproj/xcuserdata/tonyguan.xcuserdatad/xcschemes/xcschememanagement.plist
rewrite HelloWorld.xcodeproj/project.xcworkspace/xcuserdata/tonyguan.xcuserdatad/UserInterfaceState.xcuserstate (83%)
create mode 100644 HelloWorld/HelloWorld.xcodeproj/project.xcworkspace/contents.xcworkspacedata
create mode 100644 HelloWorld/HelloWorld.xcodeproj/project.xcworkspace/xcuserdata/tonyguan.xcuserdatad/UserInterfaceState.xcuserstate
create mode 100644 HelloWorld/HelloWorld.xcodeproj/xcuserdata/tonyguan.xcuserdatad/xcschemes/HelloWorld.xcscheme
create mode 100644 HelloWorld/HelloWorld.xcodeproj/xcuserdata/tonyguan.xcuserdatad/xcschemes/xcschememanagement.plist
rewrite HelloWorld.xcodeproj/project.xcworkspace/xcuserdata/tonyguan.xcuserdatad/UserInterfaceState.xcuserstate (83%)
事實上是能夠列入到代碼版本控制的文件是有規定的,不能是編寫的二進制文件、臨時文件和用戶特有的文件等。下面是Xcode 4創建的HelloWorld工程的目錄結果:
HelloWorld
[cpp]
├── HelloWorld
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── HelloWorld-Info.plist
│ ├── HelloWorld-Prefix.pch
│ ├── ViewController.h
│ ├── ViewController.m
│ ├── en.lproj
│ │ ├── InfoPlist.strings
│ │ └── ViewController.xib
│ └── main.m
└── HelloWorld.xcodeproj
├── project.pbxproj
├── project.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcuserdata
│ └── tonyguan.xcuserdatad
│ └── UserInterfaceState.xcuserstate
└── xcuserdata
└── tonyguan.xcuserdatad
├── xcdebugger
│ └── Breakpoints.xcbkptlist
└── xcschemes
├── HelloWorld.xcscheme
└── xcschememanagement.plist
├── HelloWorld
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── HelloWorld-Info.plist
│ ├── HelloWorld-Prefix.pch
│ ├── ViewController.h
│ ├── ViewController.m
│ ├── en.lproj
│ │ ├── InfoPlist.strings
│ │ └── ViewController.xib
│ └── main.m
└── HelloWorld.xcodeproj
├── project.pbxproj
├── project.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcuserdata
│ └── tonyguan.xcuserdatad
│ └── UserInterfaceState.xcuserstate
└── xcuserdata
└── tonyguan.xcuserdatad
├── xcdebugger
│ └── Breakpoints.xcbkptlist
└── xcschemes
├── HelloWorld.xcscheme
└── xcschememanagement.plist
其中HelloWorld.xcodeproj屬於包文件,它內部的很多東西是不能提交的,包括:project.xcworkspace和xcuserdata,它們是與用戶有關的。Git中有一個.gitignore配置文件,在這個文件中可以設置被忽略的文件。下面的內容是一個.gitignore配置文件:
[cpp]
# Exclude the build directory
build/*
# Exclude temp nibs and swap files
*~.nib
*.swp
# Exclude OS X folder attributes
.DS_Store
# Exclude user-specific XCode 3 and 4 files
*.mode1
*.mode1v3
*.mode2v3
*.perspective
*.perspectivev3
*.pbxuser
*.xcworkspace
xcuserdata
# Exclude the build directory
build/*
# Exclude temp nibs and swap files
*~.nib
*.swp
# Exclude OS X folder attributes
.DS_Store
# Exclude user-specific XCode 3 and 4 files
*.mode1
*.mode1v3
*.mode2v3
*.perspective
*.perspectivev3
*.pbxuser
*.xcworkspace
xcuserdata
文件中#號是注釋,可以使用正則表達式,文件考慮到了X code 3和4差別。這個文件創建之後,應該放在什麼地方?如果只考慮對一個特定工程忽略,.gitignore文件應該放在代碼庫目錄下面,目錄結構如下所示:
<代碼庫目錄>
[cpp]
└── HelloWorld
├── HelloWorld
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├──
[email protected]
│ ├── Default.png
│ ├──
[email protected]
│ ├── ViewController.h
│ ├── ViewController.m
│ ├── HelloWorld-Info.plist
│ ├── HelloWorld-Prefix.pch
│ ├── en.lproj
│ │ ├── InfoPlist.strings
│ │ └── MainStoryboard.storyboard
│ └── main.m
└── HelloWorld.xcodeproj
│
└── .gitignore
└── HelloWorld
├── HelloWorld
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├──
[email protected]
│ ├── Default.png
│ ├──
[email protected]
│ ├── ViewController.h
│ ├── ViewController.m
│ ├── HelloWorld-Info.plist
│ ├── HelloWorld-Prefix.pch
│ ├── en.lproj
│ │ ├── InfoPlist.strings
│ │ └── MainStoryboard.storyboard
│ └── main.m
└── HelloWorld.xcodeproj
│
└── .gitignore
如果考慮適用於所有的Xcode工程,則需要使用git config命令配置git,在終端中執行git config命令:
$ git config –global core.excludesfile ~/.gitignore
該命令會將配置信息寫入到~/.gitconfig文件中,–global參數是配置全局信息,~/.gitignore說明文件是放置於當前用戶目錄下。