Back to List

Git Branch and Merge β€” The Basics of Collaboration

This explains the concept of Git branches, creation and switching, merging methods, conflict resolution, and a PR-based collaboration workflow.

Beginner
|
5min
|
Verified (2026-07)
Progress0/55 (0%)

Git Branches and Merges β€” The Basics of Collaboration

After completing this topic, you will:

Understand why branches are necessary, how to create and merge them, and how to resolve conflicts.


What if there were no branches?

Three team members are simultaneously modifying the same code. A creates a login, B creates a payment, and C fixes a bug. Every time they merge, conflicts arise, and it becomes difficult to track who changed what.

A branch is an independent workspace for code. Each person works on their own branch and merges it when finished.


Creating and switching branches

bash
# Check the list of branches
git branch
# Create a new branch
git branch feature/login
# Switch to the branch
git checkout feature/login
# Create and switch at the same time
git checkout -b feature/login

main (or master) is the default branch. When creating new features, branch off and merge into main when finished.


Working and committing

bash
# Work on the feature/login branch
git add login.js
git commit -m "feat: add login form"
git add auth.js
git commit -m "feat: add authentication logic"

These commits only exist in the feature/login branch. They do not affect the main branch.


Merge: Combining

bash
# Go back to main
git checkout main
# Merge feature/login into main
git merge feature/login

The commits from feature/login are merged into main. If there are no conflicts, it completes automatically.


Resolving conflicts

If the same part of the same file has been modified in two branches, a conflict occurs:

text
<<<<<<< HEAD
const greeting = "Hello";
=======
const greeting = "Hi";
>>>>>>> feature/login

Check the content between <<<<<<< and >>>>>>> and keep only the desired code:

javascript
const greeting = "Hi";

After modifying, commit:

bash
git add greeting.js
git commit -m "fix: resolve merge conflict"

Pull Request (PR)

In practice, git merge is not done directly in the local environment. It is merged through a Pull Request.

  1. Work on a branch and push
  2. Create a PR on GitHub/GitLab
  3. Team members review the code
  4. If approved, merge

A PR is a request to merge the code into main. Because it goes through a code review, bugs are reduced, and team members can understand the code changes.


Branch naming conventions

text
feature/login        β€” New feature
fix/cart-bug         β€” Bug fix
refactor/auth        β€” Refactoring
docs/readme-update   β€” Documentation

The type/description format is common. Each team may have different rules, so follow the rules of the project.


Key takeaways

Branches are independent workspaces for code, and are merged when finished. If the same part is modified, a conflict occurs, and it must be resolved manually. In practice, code is merged through a Pull Request after a code review.

πŸ’¬ Questions & Comments

0 comments

You can post without signing in. Guest comments cannot be edited or deleted by their author.

0/2000

Loading...