Phase 1: Mobile Fundamentals

Merge conflict resolution & pull request workflows

Beginner ~4 min read
Think of it this way A friendly analogy. Read this if the technical version feels dense. Show Hide

Imagine you and your friends are building the most amazing, gigantic LEGO castle together. It's super fun, but if everyone just started grabbing pieces and sticking them onto the main castle at the same time, it could get really messy! Someone might accidentally knock down a tower, or you could end up with two windows trying to fit in the same spot. To make sure your awesome castle stays perfect and doesn't fall apart, you need a smart way to add new things.

That's where something called a Pull Request comes in. Instead of adding your brilliant idea for a new secret dungeon or a tall wizard's tower directly to the main castle, you build it on your own separate table first. Think of this separate table as your personal workspace, where you can build and try out your new section without disturbing the main castle. Once you're really happy with your secret dungeon or wizard's tower, you don't just snap it on. You say, "Hey everyone, look at this cool new part I've built! What do you think?" This moment, when you show off your new creation and ask your friends to check it out, is exactly what a Pull Request is.

Your friends (who are your teammates) will then come over and look at your amazing new castle piece. They might say, "Wow, that's incredible!" or they might offer suggestions like, "Maybe the dungeon entrance could be a bit bigger?" or "Does the wizard's tower fit neatly with the main castle walls?" They give you ideas and feedback to make your part even better. Once everyone agrees your new section is absolutely perfect and fits just right, someone (maybe the person who’s really good at connecting everything) will carefully help attach your finished piece to the main, growing castle. This way, everyone’s ideas get added smoothly and safely.

So, when you build apps with other people, this means you can all work on different cool new features without breaking each other's work, and everyone gets a chance to make sure the app turns out awesome.

When you work on a mobile app with a team, you'll constantly be proposing changes to the codebase without directly altering the main, stable version. This is where Pull Requests (PRs) come in. A Pull Request is essentially your way of saying, "Hey team, I've made some changes on my separate branch, please review them!" You typically start by creating a new branch for your feature or bug fix, make your commits there, and then push that branch to the shared repository. Once pushed, you open a PR from your feature branch to the main development branch (often main or develop). This process is crucial for maintaining code quality and collaborative teamwork in mobile development, allowing others to provide feedback before your code is integrated.

After you open a Pull Request, your teammates can review your code. They might leave comments, suggest improvements, or ask clarifying questions directly within the PR interface. This review phase is invaluable, as it helps catch potential bugs, ensures coding standards are met, and spreads knowledge across the team. Once your code passes review and all discussions are resolved, someone with permission (often a senior developer or lead) will approve and "merge" your Pull Request. Merging integrates your changes from your feature branch into the main development branch, making your new code part of the official project.

Sometimes, when you try to merge your branch (either directly or via a PR), Git might encounter a merge conflict. This happens when two or more developers have changed the exact same lines in the same file independently, or one developer moved a file while another edited it. Git, being a smart but not psychic tool, doesn't know which change to keep, so it pauses and asks you to decide. To resolve a conflict, you'll manually edit the conflicted file, choosing which changes you want to keep, discarding others, or combining parts of both. After editing, you stage the file (git add) and commit the resolution (git commit). Resolving conflicts is a fundamental skill for collaborative development, ensuring all team members' contributions can coexist smoothly in the project.

Key Takeaways

  • Pull Requests (PRs) are proposals for code changes, enabling team review and collaboration.
  • PRs help maintain code quality, consistency, and catch issues early in the development cycle.
  • Merge conflicts occur when Git can't automatically combine different changes made to the same code section.
  • Resolving conflicts involves manually editing files to decide which code to keep, discard, or combine.
  • This workflow promotes a safe and structured way to integrate new features and fixes into a mobile app.

Code Example

swift
// File: AppConstants.swift (Example of a conflicted file)

import Foundation

struct AppConstants {
    static let apiBaseURL = "https://api.myapp.com"
<<<<<<< HEAD
    static let appVersion = "1.0.1" // Your team's current production version
=======
    static let appVersion = "2.0.0-beta" // A teammate's new major release version
>>>>>>> feature/new-major-release
    static let buildNumber = 42
}

// To resolve this conflict, you would edit the file, for instance, by choosing to keep the new major release version:
// struct AppConstants {
//     static let apiBaseURL = "https://api.myapp.com"
//     static let appVersion = "2.0.0-beta"
//     static let buildNumber = 42
// }

// After editing, you would stage and commit the resolved file:
// git add AppConstants.swift
// git commit -m "fix: resolved merge conflict in AppConstants.swift"

How this code works

This Swift code demonstrates a common scenario in Git: a merge conflict within AppConstants.swift, a file typically holding crucial application settings like apiBaseURL and appVersion. The conflict is clearly marked by special Git indicators: <<<<<<< HEAD shows the version of the appVersion constant from the current branch, >>>>>>> feature/new-major-release shows the version from another branch named feature/new-major-release, and ======= separates these two conflicting changes. Notice how both branches tried to update the appVersion static constant, leading to Git needing human intervention to decide which one to keep.

To resolve this conflict, a developer must manually edit the file. The example shows the resolution by choosing to keep appVersion = "2.0.0-beta" from the feature/new-major-release branch, deleting all the conflict markers and the unwanted line. This manual decision-making is a critical step, as Git cannot inherently understand which version is semantically correct for the application. After editing, git add AppConstants.swift stages the now-resolved file, and a subsequent git commit command saves this resolution, allowing the merge process to complete.