Using Git with Terminal | CodePath iOS Cliffnotes (2024)

Git is an essential tool in the developers toolkit. Git stores your files like a stream of snapshots, allowing you and other collaborators to make changes to projects and keep everything in sync. This Guide introduces using Git with Terminal. You can click the video link in each section for a video walkthrough or watch the video playlist now.

Basic Terminal Navigation

The Terminal is much like the finder, but less pretty. There are just a few basic commands needed to navigate around in Terminal. Using Terminal, we often refer to folders as directories. Check out this Terminal Cheat Sheet for Mac for a more complete list of commands.

  • Open up terminal, cmd + space "terminal".
  • pwd Figure out which directory you are currently in by "Printing the Working Directory".
  • ls List the directories and files inside the current directory.
    • ls -a include hidden files in the list of directories and files. This is helpful when trying to find hidden files like .git or .gitignore
  • cd [folder] Go into the folder. e.g. cd Desktop/Developer
    • cd This by itself will navigate all the way out to your Home Directory
    • cd .. Back out to the parent directory of the current directory.
  • open [file] Same as "double-clicking" a file or folder in finder.
    • open . Opens the current folder or file in finder or it's default application.
  • clear When you need a fresh Terminal window

Hint If you can't find the path to a file or folder through terminal, search for it using Finder, then drag the file or folder right into your terminal window! If you are trying to change to that directory, you will need to type cd before dragging in the folder.

Using Git with Terminal | CodePath iOS Cliffnotes (1)

Creating a Local Git Repository

Using Git with Terminal | CodePath iOS Cliffnotes (2)

Local Git repositories are created and managed locally on your computer.

  • Xcode will automatically create a local Git repository for your project if you select, "Create Git repository on... My Mac", when you first create your project. You should Always select this when creating a new Xcode project.

    Using Git with Terminal | CodePath iOS Cliffnotes (3)

If you already have a project, where a Git repository was not created when you made the project, you can create a local Git repository using terminal.

  • Navigate to your Xcode Project folder in Terminal.

    git init

Creating a .gitignore File

Using Git with Terminal | CodePath iOS Cliffnotes (4)

When collaborating using Git, you will inevitably run into "merge conflicts". However, you will save yourself from a lot of extraneous conflicts by adding a .gitignore file with the proper content.

Add Files and Directories to .gitignore

Create and Configure .gitattributes File

Using Git with Terminal | CodePath iOS Cliffnotes (5)

This file allows us to tell Git how we want certain files to be treated. We will add a file to the .gitattributes that tells Git to treat any file with .pbxproj as binary.

  • Navigate to your Xcode Project folder in Terminal.
  • Use ls -a to list all files including hidden files to check if a .gitattributes has already been created.
  • If there is not an existing `.gitattributes file, create one.

    touch .gitattributes
  • Open your .gitattributes file, open .gitattributes

  • Add the following code to the .gitattributes file.

    *.pbxproj binary merge=union

    Add and configure README

  • Add a README.md file to your repo

    touch README.md
  • Open and edit your README.md

    open README.md

Adding and Committing Changes to Local Repository

Using Git with Terminal | CodePath iOS Cliffnotes (6)

Now that your local Git is all setup from the last step, you can update your local Git repository with any changes you make to your project.

  • Navigate to your Xcode Project folder in Terminal.
  • Check for any changes that have been made to files since your last commit

    git status
  • Stage any files with changes you'd like to commit.

    git add [filepath] # This adds an individual filegit add . # This adds all files with changes
  • Check to see what was added.

    git status
  • Apply the changes to your local Git repository with a message briefly outlining the changes you made.

    git commit -m "Here is my commit message"

Linking to a Remote Repository

Using Git with Terminal | CodePath iOS Cliffnotes (7)

There are many remote repository options. In this guide we will be using GitHub. You will need a GitHub account if you don't have one already.

Repository URLs

There are two types of repository URLs, HTTPS and SSH:

  • HTTPS: https://github.com/homer/duff_project.git
  • SSH: [email protected]:homer/duff_project.git

The general workflow is the same for both URLs, but there are some differences in the specifics of commands.

SSH Setup

You'll first need to register your machine's SSH keys with your Github account. You only need to perform this step once for each machine you use.

Once you've added your SSH key, the SSH repository URL will look like[email protected]:myusername/reponame.git but for your username and project. You won't need to provide credentials (username/password) for any further git commands.

HTTPS Setup

There is no additional setup required for using HTTPS. However, you'll need to enter your username and password each time you run the git pull or git push commands:

git push origin master<enter your username at promt><enter your password at prompt>

Create a new Repository on GitHub

Using Git with Terminal | CodePath iOS Cliffnotes (8)

  • Navigate to your Xcode Project folder in Terminal.
  • Link to the remote repository by adding the following code in Terminal, provided from your GitHub repository. We want to "push an existing repository from the command line".

    • NOTE: Make sure you add YOUR GitHub remote address!

    Using Git with Terminal | CodePath iOS Cliffnotes (9)

    • NOTE: Decide if you want the remote to use SSH or HTTPS before setting your remote in the next step. I like ssh for the convenience of not always having to input my credentials.
    git remote add origin https://github.com/yourUserName/yourRepoName.gitgit push -u origin master

Syncing Local Repository With Your Remote Repository

When you have made changes to your project and used the git add ., git status and git commit -m "Message" commands to update your local Git repository, the next step is to sync with your remote repository. Luckily, keeping in sync can be done with only two additional steps, git push and git pull.

  • Navigate to your Xcode Project folder in Terminal.
  • Push changes you have made locally to update the remote repository.

    git push
  • Pull changes that have been added to the remote repository by a collaborator to update your local repository.

    git pull

Cloning a Remote Repository

Using Git with Terminal | CodePath iOS Cliffnotes (10)

If you are collaborating on a project, or just want access to your project from a different computer, you will need to clone from the remote repository.

  • Get the url of the remote repository. You can access this by going to the remote repository in GitHub and copying the clone link, bottom right.

Using Git with Terminal | CodePath iOS Cliffnotes (11)

  • In Terminal, navigate to the directory you want to clone the repository in.
  • Clone the repository...

    git clone https://yourRemoteRepositoryUrl

Dealing With Merge Conflicts

Using Git with Terminal | CodePath iOS Cliffnotes (12)

Inevitably, there will come a time when you AND a collaborator will make changes to the same file and both try to push to the remote repository. This will result in a Merge Conflict.

  • In the instance of a Merge Conflict, you will probably get an error in the terminal when attempting to push your local changes.

Using Git with Terminal | CodePath iOS Cliffnotes (13)

  • As, the error message suggests in the "Hint", the next step is to perform a git pull. The pull will likely result in a message similar to the following.
    • NOTE: The error message confirms we have a Merge Conflict in the Main.storyboard file and tells us that we need to fix the conflict and commit the changes.

Using Git with Terminal | CodePath iOS Cliffnotes (14)

  • Use git status to confirm which file(s) are causing the conflict.

Using Git with Terminal | CodePath iOS Cliffnotes (15)

  • Use open /yourfileHere/ to open up the file in conflict; this will open up the file in Xcode.
    • NOTE: In this particular example, the conflict is in the Main.storyboard file. We are usually looking at the Interface Builder view of this file, so to reveal the underlying code, right-click on the Main.storyboard file and choose, Open As -> Source Code.

Using Git with Terminal | CodePath iOS Cliffnotes (16)

  • Identify the conflicting lines of code; they will be surrounded by, <<<<<<< HEAD >>>>>>>bunchOfNumbersAndLetters and separated by =======.

  • Choose one to keep and one to delete. Do your best to figure out what the differences are; in this example, we see that the initialViewController is set to a different ID in each, "BYZ-38-t0r" vs. "NFr-Eg-PaI".

Using Git with Terminal | CodePath iOS Cliffnotes (17)

  • Clean your Project, cmd + shift + K

    • If your resolved conflict was in the Main.storyboard file, right + click on the storyboard file and choose Open As -> Interface Builder - Storyboard.
  • Finally, push your local changes to the remote repository.

Using Git with Terminal | CodePath iOS Cliffnotes (2024)
Top Articles
What are containers? | Google Cloud
Applying for a UK Visa from the United States
2018 Jeep Wrangler Unlimited All New for sale - Portland, OR - craigslist
Foxy Roxxie Coomer
Hotels Near 625 Smith Avenue Nashville Tn 37203
Lakers Game Summary
Davita Internet
Regal Amc Near Me
Danatar Gym
Midflorida Overnight Payoff Address
Academic Integrity
What Was D-Day Weegy
Comenity Credit Card Guide 2024: Things To Know And Alternatives
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Costco Gas Foster City
All Obituaries | Buie's Funeral Home | Raeford NC funeral home and cremation
Vigoro Mulch Safe For Dogs
Recap: Noah Syndergaard earns his first L.A. win as Dodgers sweep Cardinals
Ahrefs Koopje
Zillow Group Stock Price | ZG Stock Quote, News, and History | Markets Insider
At&T Outage Today 2022 Map
THE FINALS Best Settings and Options Guide
Anotherdeadfairy
Del Amo Fashion Center Map
Bidevv Evansville In Online Liquid
What Individuals Need to Know When Raising Money for a Charitable Cause
They Cloned Tyrone Showtimes Near Showbiz Cinemas - Kingwood
Srjc.book Store
Downloahub
Elanco Rebates.com 2022
Craigslistodessa
Red Sox Starting Pitcher Tonight
Egg Crutch Glove Envelope
Metro By T Mobile Sign In
Sedano's Supermarkets Expands to Orlando - Sedano's Supermarkets
Serenity Of Lathrop - Manteca Photos
How does paysafecard work? The only guide you need
Edward Walk In Clinic Plainfield Il
Seymour Johnson AFB | MilitaryINSTALLATIONS
Pillowtalk Podcast Interview Turns Into 3Some
The Blackening Showtimes Near Regal Edwards Santa Maria & Rpx
Hell's Kitchen Valley Center Photos Menu
Oriellys Tooele
Publictributes
Craigslist Pets Plattsburgh Ny
Kenner And Stevens Funeral Home
All Weapon Perks and Status Effects - Conan Exiles | Game...
Neil Young - Sugar Mountain (2008) - MusicMeter.nl
Amourdelavie
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 6498

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.