intro-frontend-course

Git Branching and Merging: Enhancing Collaboration

Objective: By the end of this lesson, you will understand the concepts of branching and merging in Git, enabling you to manage separate lines of development and integrate changes efficiently.

The Power of Branching in Git

Branching in Git allows developers to create separate lines of development that do not affect the main codebase. It’s useful for developing new features or fixing bugs without disrupting the main project.

Creating a New Branch: To create a new branch named ‘feature-branch’, use:

git branch feature-branch

Switching Between Branches: After creating a new branch, switch to it using:

git checkout feature-branch

Listing Branches: To see all the branches in your repository, including the active one, use:

git branch

The Art of Merging in Git

Merging is the process of integrating changes from one branch into another. This is crucial when a feature is complete or a bug is fixed and needs to be integrated into the main codebase.

Merging Changes: To merge changes from ‘feature-branch’ into the ‘master’ branch, first switch to the ‘master’ branch:

git checkout master

Then merge ‘feature-branch’:

git merge feature-branch

Resolving Merge Conflicts

Merge conflicts occur when Git can’t automatically reconcile changes in two branches. You’ll need to manually resolve these conflicts by editing the conflicted files, then stage and commit the resolved files.

Identifying Conflicts: Git will notify you of conflicts during the merge process.

Resolving Conflicts: Open the conflicted files, modify them to resolve the conflicts, then save.

Finalizing the Merge: After resolving conflicts, stage and commit:

git add .
git commit -m "Resolved merge conflicts"

Managing Branches

Good branch management ensures a clean and navigable project. After merging a feature branch, it’s good practice to delete it to reduce clutter.

Deleting a Branch: To delete ‘feature-branch’ after merging, use:

git branch -d feature-branch

Key Lesson Concepts: