Monday, July 18, 2016

Brief Introduction to iOS Development - Part 7 - Using Github and Creating Repository with Xcode

Todays session, we will not be looking into the iOS framework. Instead, we will be looking at using Github with Xcode.

First, we create a new Single View Application project called "Intro7".


Make sure you entered the product name "Intro7".


Click Next, make sure the option "Create Git repository on My Mac" was checked.


Click Create and a project will be created.

Next we go to our Github site. Click on "New repository".


Under the page "Create a new repository". Enter the name of the repository and description. Please take note: DO NOT CHECK "Initialize this repository with a README". What we want is a complete empty repository.


Once you clicked on Create repository, the system will show a page with some quick setup instruction. Please take note of the address or click on the copy button on the right.


Now, we go to the Xcode. Under Source Control, select Intro7 - master -> Configure Intro7.


Click to configure Intro7. On the configuration page, click on the middle tab "Remote". 


We should not have anything in the remote yet. Click Add remote...


On the setup page, under address, paste the Github address we have copied for Intro7.


The remote site will be registered.


Next we need to activate Push under Source Control.


Click Push. The push command is to copy everything from your local Git repository to the remote site (Github).


We will be asked for login credential of our Github site.


Enter our login credential from Github and the system will push everything we have created to Github. If we encountered any error such as "there are uncommitted files", we need to commit the files first. If we are not sure what we have changed, we can click Discard all changes and click push again.

The Github page will show new update similar to the screenshot below.


Now, we can create a README file. There are 2 ways to create README file. We can create a text file named as README and place it in our project folder and push the file to Github. Alternatively, we can create the README file from Github page. On the previous screen shot, click on the green button Add a README.

A new README file will be create with the repository description. We need to add additional description into the README file.


After adding new description, we can commit the file by adding comment and click the green button "Commit new file".


Once the file is committed, the copy of the project in the Github is the latest. In order to update the project file from Github to our local repository, we need to perform a pull. A pull command is to copy everything from the remote site to our local site.

Go to Source Control, select Pull.


Leave the selection as origin/master and click Pull.


Once it is done, everything from Github will be updated to our local repository.

While using Pull or Push, there is one simple rule; we should not have changes in the remote site and our local site at the same time. Pull or push will not work if there are changes to both the Github copy and the local copy. We also cannot perform any push or pull if there are any uncommitted files on either the remote site or local site.

If we really need changes from Github and our local copy, we need to perform a merge instead.

Under Source Control, the most common command are commit, push, pull and discard all changes.


The good thing about git is that we can make changes and if we don't like our changes we can always click discard all changes. It is an undo button for programming. Once we are sure about our changes, we can click Commit to confirm the changes. After the commit, we can choose to update the remote site by performing a push. However, usually we perform commit and a push at the same time, we can do this by checking "Push to remote" at the commit page as follow. We also need to add comment. The system would not allow us to perform any commit without any comment.

Before we perform any commit we must write some program. For simplicity, we choose to use the textfield program in the previous lesson. Please check the lesson here. Once we completed the program and tested the program, we can perform a commit as shown below.


Now lets try other collaborative features. Under Source Control -> Intro7 - master, select New Branch.


Enter the name of new branch and click Create.


A copy of the similar program will be created under the branch named Test1. Now change the background color of the label. We can perform a commit to the branch as shown below.


Now, we can choose to merge the changes in Test1 to the master copy. Select Merge into Branch as follows.


Select master and click Merge.


The system will show the changes we want in the following screen shot. Click Merge.


Now we can switch to master Branch by selecting Switch to Branch...



Select the master copy at the local site and click Switch.


Now we are in the master branch, please check the color of your label. Any changes made in the Test1 branch will be reflected here.

Now in our master copy, we can make addition changes such as adding a button to the storyboard.

Remember that previously, we only merge the changes into local master copy. To update the Github copy we must commit additional changes and push the changes to Github. Select Commit, checked "Push to remote" and add comment. Click Commit...


Now if we check our Github page the latest changes are reflected as shown below.


A copy of this project is stored at Github https://github.com/SwiftiCode/Intro7


*** End ***

No comments:

Post a Comment