In this lesson, you will learn how to integrate your finished work from a feature branch back into your main branch to consolidate your changes into the project's primary history.
Why Merge?
When you create a branch, you are effectively working in a parallel universe. You can experiment, build new features, or fix bugs without disturbing the stable code in main. Once your work is polished and tested, you need a way to bring those improvements into the primary project line. This process is called "merging."
The Merging Workflow
Merging is a two-step process that ensures you are incorporating your changes into the correct destination.
- Switch to the target branch: You must first tell Git where the changes should land. In most cases, this is your
mainbranch. - Execute the merge: You instruct Git to pull the history from your feature branch into your current branch.
Step-by-Step Example
Imagine you just finished a new feature on a branch named user-auth. To bring that work into main, run these commands in your terminal:
# 1. Switch to the main branch
git checkout main
# 2. Merge the feature branch into main
git merge user-auth
When you run git merge, Git looks at the commit history. If main has not changed since you started your feature branch, Git performs a "fast-forward" merge, simply pointing the main branch label to the latest commit of your feature branch. If other work has happened on main in the meantime, Git will create a new "merge commit" that ties the two histories together.
After the merge is successful, your main branch now contains all the snapshots you captured while working on your feature. You can then safely delete your feature branch if you no longer need it to keep your project tidy.
Key Takeaway: Merging updates your primary branch by pulling in the unique commit history from a feature branch, effectively unifying your work into a single timeline.