- Published on
Part 2: Branching and Merging
- Authors
- Name
- Diego Herrera Redondo
- @diegxherrera
Understanding Branching
Branching is one of the most powerful features of Git and is crucial for modern development workflows. A branch in Git represents an independent line of development. By default, Git creates a branch called main
(or master
in older versions), which serves as the primary branch.
Branches enable developers to:
- Work on new features or bug fixes in isolation.
- Experiment without affecting the main codebase.
- Collaborate on different features simultaneously.
Why Use Branches?
Using branches ensures that the main
branch remains stable and deployable. Developers can create separate branches to develop features, fix bugs, or test new ideas. Once the changes are complete and tested, the branch can be merged back into main
.
Creating a Branch
Creating a new branch in Git is straightforward. The following command creates a new branch and switches to it:
# Create a new branch
$ git branch feature-branch
# Switch to the new branch
$ git checkout feature-branch
You can combine these steps using:
$ git checkout -b feature-branch
Listing Branches
To see all branches in your repository:
$ git branch
The current branch is marked with an asterisk (*).
Working with Branches
Once on a branch, any changes you make will be isolated to that branch until you decide to merge it with another branch.
- Modify or create files as needed.
- Stage and commit changes:
$ git add .
$ git commit -m "Add new feature implementation"
Merging Branches
After you have completed your work on a branch, you’ll want to merge it back into the main branch. This can be done using:
- Switch to the main branch:
$ git checkout main
- Merge the feature branch:
$ git merge feature-branch
Handling Merge Conflicts
Merge conflicts occur when changes in two branches overlap, and Git is unable to automatically resolve them. Here’s how to handle them:
- Identify the conflict by running:
$ git status
- Edit the conflicted file(s) to manually resolve the conflicts.
- Mark the conflict as resolved:
$ git add resolved-file.txt
- Complete the merge:
$ git commit
Deleting a Branch
Once a branch is merged and no longer needed, you can delete it:
$ git branch -d feature-branch
If the branch has not been merged and you still want to delete it, use:
$ git branch -D feature-branch
Practical Example: Feature Branch Workflow
# Create and switch to a new branch
$ git checkout -b add-login-feature
# Make changes and commit them
$ git add login-module.js
$ git commit -m "Implement login feature"
# Switch back to main and merge
$ git checkout main
$ git merge add-login-feature
# Delete the feature branch
$ git branch -d add-login-feature
Recap
- Branches are essential for parallel development.
- Merging incorporates changes from one branch into another.
- Merge conflicts require manual resolution when automatic merging fails.
- Deleting branches helps keep your repository clean and organized.
Next Steps
In the next part, we’ll explore Collaboration with GitHub, where we’ll discuss how to push branches to remote repositories, create pull requests, and collaborate with others.
Stay tuned for Part 3: "Collaboration with GitHub."