Basic Git commands every iOS dev should know

Nicolle Policiano
Policiano
Published in
4 min readNov 4, 2023

As an iOS developer, you’re going to need to collaborate with others, maintain different versions of your code, and track your changes over time. Git, a distributed version control system, is an invaluable tool for these tasks. It helps you manage your project’s history, collaborate with your team, and synchronize your local and remote repositories. Here’s a beginner’s guide to some fundamental Git commands that every iOS developer should know.

Understanding Repositories

First, let’s clarify the two types of repositories:

  • Local Repository: This is stored on your machine and is integrated with your file system. Here, you make changes, commit them, create branches, and perform other local Git operations.
  • Remote Repository: Hosted on a server, like GitHub, this is where your project’s history is preserved. It’s a collaborative space where multiple developers can work on the same project simultaneously.

Fundamental Git commands

  • Clone an existing project (git clone)
  • Create and switch to a new branch (git checkout -b <new-branch-name>)
  • Stage changes (git add)
  • Commit changes (git commit)
  • Pull updates from the remote repository (git pull)
  • Push changes to a remote repository (git push)

git clone

Use git clone to copy an existing remote repository onto your local machine. It initializes a .git directory, pulls down all the data from the remote repository, and checks out the main branch.

git branch

Branches allow you to diverge from the main line of development and work independently without affecting the “main” branch. To create a branch, use git branch <branch-name>.

git checkout

The git checkout command is used to switch between different branches in a Git repository. When you want to work on a new feature or fix a bug, you create or switch to a new branch using this command. It ensures that your changes are kept separate from the main codebase until they are ready to be merged.

To switch to an existing branch:

git checkout <branch-name>

To create and switch to a new branch:

git checkout -b <new-branch-name>

Additionally, if you want to discard changes in your working directory, you can use the following command:

git checkout -- .

This will revert all changes made to files in the current directory to their last committed state, effectively cleaning your working area by removing all non-committed changes.

git status

This command is your window into the current state of your local repository. It shows what has changed and what’s currently staged for the next commit. This command can produce different outputs, as shown in the examples below.

Example 1: Clean working tree

On branch main
nothing to commit, working tree clean

Example 2: Changes not staged for commit

On branch main
Changes to be committed:
(use "git restore - staged <file>…" to unstage)
modified: ContentView.swift
Changes not staged for commit:
(use "git add <file>…" to update what will be committed)
(use "git restore <file>…" to discard changes in working directory)
modified: GitAppExampleApp.swift

git add

The git add command is your first step in the staging process, indicating to Git which changes in the working directory should be included for the next commit. This command doesn't affect the repository until you commit the changes.

Here’s how you can use it for individual files:

git add <file>

If you want to stage all the changes you’ve made, including new, modified, and deleted files, you may want to usegit add -A:

git add -A

git commit

When you’ve staged your changes, you use git commit to actually commit those changes to your local repository’s history. It’s like saving a checkpoint in your project’s development.

To create a commit with a message, you can use the -m flag followed by your commit message in quotes. This message should be a brief description of the changes you've made:

git commit -m "Enter a commit message here"

git pull

git pull updates your local branch with the latest changes from the remote repository. It’s a combination of git fetch and git merge.

It is considered best practice to pull the latest updates from the remote repository before starting work and before pushing new commits to keep your local branch up-to-date.

git push

Finally, git push uploads your local branch commits to the remote repository branch. This shares your changes with the team and integrates them into the main project.

Conclusion

Mastering these basic Git commands lays the groundwork for efficient and collaborative iOS development. As a passionate beginner iOS developer, this is my very first blog post. My journey into the world of development is just beginning, and I’m excited to share what I learn along the way. I hope this guide serves as a helpful starting point for other beginner developers like myself. Let’s embrace the learning process together and build amazing things. Happy coding!

--

--