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.
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
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
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"
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