DEV Community

Amulya Magadi
Amulya Magadi

Posted on

Day 02 : Mastering Branching and Merging in Git

Today’s learning focused on one of the most powerful features of Git — branching and merging. This concept is essential for real-world development, especially when working in teams or managing multiple features simultaneously.


🚀 What is Branching?

A branch in Git is essentially a separate line of development. It allows you to work on new features, bug fixes, or experiments without affecting the main codebase.

By default, every repository starts with a branch called:

  • main (or sometimes master)

🌿 Why Use Branches?

  • Develop features independently
  • Fix bugs without disturbing stable code
  • Collaborate with team members efficiently
  • Maintain clean project history

🛠️ Important Branching Commands

1. Check existing branches

git branch
Enter fullscreen mode Exit fullscreen mode

2. Create a new branch

git branch feature1
Enter fullscreen mode Exit fullscreen mode

3. Switch to a branch

git checkout feature1
Enter fullscreen mode Exit fullscreen mode

👉 Modern alternative (recommended):

git switch feature1
Enter fullscreen mode Exit fullscreen mode

4. Create and switch in one step

git checkout -b feature2
Enter fullscreen mode Exit fullscreen mode

👉 Modern alternative:

git switch -c feature2
Enter fullscreen mode Exit fullscreen mode

5. Delete a branch

git branch -d feature1
Enter fullscreen mode Exit fullscreen mode

👉 Force delete (if not merged):

git branch -D feature1
Enter fullscreen mode Exit fullscreen mode

🔀 What is Merging?

Merging is the process of combining changes from one branch into another.

Typically:

  • Work is done in a feature branch
  • Then merged into main after completion

🔧 Important Merging Commands

1. Switch to target branch (usually main)

git checkout main
Enter fullscreen mode Exit fullscreen mode

2. Merge another branch into current branch

git merge feature2
Enter fullscreen mode Exit fullscreen mode

⚠️ Merge Conflicts

Sometimes, Git cannot automatically merge changes. This happens when:

  • Same file
  • Same lines modified in different branches

In such cases:

  1. Git marks conflict in files
  2. You manually resolve it
  3. Then run:
git add .
git commit -m "Resolved merge conflict"
Enter fullscreen mode Exit fullscreen mode

📊 Useful Commands for Workflow

Check branch status

git status
Enter fullscreen mode Exit fullscreen mode

View commit history

git log --oneline --graph --all
Enter fullscreen mode Exit fullscreen mode

Push branch to GitHub

git push origin feature2
Enter fullscreen mode Exit fullscreen mode

Pull latest changes before merging

git pull origin main
Enter fullscreen mode Exit fullscreen mode

🧠 Key Takeaways

  • Branching allows safe and parallel development
  • Always create a branch before starting new work
  • Merge carefully and handle conflicts properly
  • Keep your branches updated with main regularly

📌 Final Thought

Branching and merging are the backbone of collaborative development in Git. Once mastered, they make your workflow cleaner, safer, and much more professional.

Today was a major step forward in understanding how real-world development actually happens!


Top comments (0)