How to Use Git Properly with Xcode

How to use Git properly with Xcode?

I have worked on iPhone applications full time since the SDK launch, most of that time spent working on teams with multiple developers.

The truth is that it's way more harmful to disallow merging of that .pbxproj file than it is helpful. As you say, when you add a file unless other people get that file, they have to also add it to their project - in an application of any size, that sucks and it also takes away a huge benefit of source code control in that you cannot really revert to a complete earlier project state just through git.

The .pbxproj file is simply a property list (similar to XML). From experience, just about the ONLY merge conflict you were ever get is if two people have added files at the same time. The solution in 99% of the merge conflict cases is to keep both sides of the merge, which for git at least simply involves removing any >>>>, <<<<, and ==== lines. In fact this is so common that I have created a simple shell script to fix a .pbxproj file in a merge state from git, I run this from within the project directory (at the Classes level):

#!/bin/sh

projectfile=`find -d . -name 'project.pbxproj'`
projectdir=`echo *.xcodeproj`
projectfile="${projectdir}/project.pbxproj"
tempfile="${projectdir}/project.pbxproj.out"
savefile="${projectdir}/project.pbxproj.mergesave"

cat $projectfile | grep -v "<<<<<<< HEAD" | grep -v "=======" | grep -v "^>>>>>>> " > $tempfile
cp $projectfile $savefile
mv $tempfile $projectfile

Worst case if it fails (you ask XCode to load the project and it fails to load), you simply delete the .pbxproj file, check out the master from git, and re-add your files. But I've never had that happen in many months of use with this script, again working full time on iPhone applications with several other developers.

Another option (pointed out in comments below) that you can try using in place of the script, is to add this line to a .gitattributes file:

*.pbxproj text -crlf -diff -merge=union

Then git will always take both sides of a merge for the .pbxproject files, having the same effect as the script I provided only without any extra work.

Lastly, here is my complete .gitignore file, showing what I do have it set to ignore as there are a few things you don't want - in my case really just emacs remnants and the whole build directory:

# xcode noise
build/*
*.pbxuser
*.mode1v3
*~

# old skool
.svn

# osx noise
.DS_Store
profile

Issues Using GIT with Xcode projects

The first thing I always do is set up the .gitignore file before adding any other files from the project. Problems can occur when loading project settings for two different machines that may have the same User account name. You have to make sure some settings aren't added to the repo whilst others are.

I set up my .gitignore by adding the following:

.DS_Store
*/.DS_Store
*.swp
*~.nib

build/

*.pbxuser
*.perspective
*.perspectivev3

*.mode1v3
*.mode2v3

Documentation/Generated
doxygenWarnings.txt
*.xcodeproj/xcuserdata/*.xcuserdatad
*.xcodeproj/project.xcworkspace/xcuserdata/*.xcuserdatad
*/*.xcodeproj/xcuserdata/*.xcuserdatad
*/*.xcodeproj/project.xcworkspace/xcuserdata/*.xcuserdatad

If you create the repos from scratch or run git rm --cached <filethatshouldbeignore> to each file that should be ignored and then commit, see if the problem still occurs.

Problems will occur in the .pbxproj file, so make sure all conflicts are corrected too.

Regarding files that cannot be found, remember to import the libraries etc. into the project folders and reference them from there. Apple's own iOS libraries should be fine, but others won't be. For example, in your project folder, copy your images from there and reference them only within the project folder.

Can git be integrated with Xcode?

Xcode 4 supports git natively (Developer Tools State of the Union Address at WWDC 2010)

Learn more here: What's new in Xcode 4

The documentation from Apple is lengthy, but a good read.

How do I start using git on an existing Xcode project that didn't initially use git when it was created?

In Xcode choose Source Control > New Git Repositories to create a git repository for an existing Xcode project.

Quick guide to get started using Git + GitX with Xcode projects on the mac?

Git is absolutely enormous, and you could certainly spend that month learning its processes, but you can stick to some basic concepts and end up with a really great workflow. I use the command line, as it allows you stick to these basics, and expand out if you need to. These basic commands are "pull", "push", "init", "commit -am "message"". Later, you can read about branches and rebasing at gitref.org.

As a mac Xcode + git user; I definitely recommend DTerm to make life easy. One key command brings up a floating terminal window, CDed to the directory of the file that's currently active. In XCode, this means that you'll be in a git-controlled directory immediately.

So, my workflow --

  1. Use "git init" in the terminal to create a repository
  2. Create github repository
  3. follow github instructions to associate the two
  4. When working in my project, press Shift-Command-Enter to bring up a floating terminal window
  5. type "git commit -am "commit message" to commit all current changes
  6. Same key combo plus "git pull" or "git push" for pulling in changes from code repository or pushing changes to code repository, respectively

I find that the command line allows a much easier working relationship with git than GitX, especially if you're using something like DTerm.

For a great reference, check out gitref.org. Good luck!

Add Xcode project to github repo?

The best way really is to follow the instructions GitHub gives you:

First, go the correct directory

cd <directory of your Xcode project>

It sound like you've already got a local Git project from Xcode. If that's true, skip this code block.

git init .
git add .
git commit -s
<type in a commit message>

Last, push into your repository. The following is copied directly from my own github account, after I created a new "test" repo. Change the "dhalperi/test" part.

Push an existing repository from the command line

git remote add origin git@github.com:dhalperi/test.git
git push -u origin master

How to capitalize Xcode, Git, git, and HEAD

Xcode has always been “Xcode” with a capital “X” and a lower-case “c”. Here's the press release dated June 23, 2003 announcing the first release of Xcode. Here's a video of Steve Jobs introducing Xcode 1.0 at WWDC 2003, and it says “Xcode” on the slide. (Youtube took down the video.)

“Git”, when referring to the system, is a proper noun and is therefore, by English language convention, spelled “Git” with a capital “G”. Example sentence from the Git User Manual:

It will be useful to have a Git repository to experiment with as you read this manual.

The command-line program that provides access to the system is “git” with a lower-case “g”, because all-lowercase is the historic convention for Unix commands. If your filesystem is case-insensitive (which is the default on OS X), you can sometimes get away with capitalizing some or all of its letters, but it's a bad idea to make that a habit.

The currently-checked out commit in Git is spelled “HEAD” in all capitals, because that string is embedded in the git source code in many places. Example:

if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)

You can sometimes get away with not capitalizing some or all of the letters if your filesystem is case-insensitive (which is the default on OS X), but it's a bad idea to make that a habit.



Related Topics



Leave a reply



Submit