Branching and Merging
Overview
A branch is an independent line of development. You can create a branch, make changes on it, and then merge it back into main when your work is complete and tested.
By the end of this task, you will be able to:
- Create a new branch
- Switch between branches
- Make commits on a feature branch
- Pull the latest changes from the remote repository
- Merge a branch into
main - Delete a branch after merging
Creating a New Branch
Before making changes, create a new branch for your work. Branch names should be short and descriptive of what you are working on.
-
Run the following command to create and switch to a new branch:
This command does two things at once: it creates a new branch called
feature/add-contact-pageand switches you to that branch immediately.
You should see:
Branch Naming Conventions
Good branch names describe what you are working on. Common prefixes include:
feature/- for new features (e.g.,feature/add-login-page)bugfix/- for bug fixes (e.g.,bugfix/fix-nav-links)hotfix/- for urgent fixes (e.g.,hotfix/security-patch)
Avoid spaces in branch names. Use hyphens (-) or slashes (/) instead.
Verifying Your Current Branch
To confirm which branch you are on, check the branch list.
-
Run the following command:
You should see a list of branches with an asterisk (*) next to the active one:
Success
If you see the asterisk next to your new branch name, you are on the correct branch and ready to start making changes.
Switching Between Branches
To switch back to main (or any other branch) at any time, run:
Make sure you commit or stash your changes before switching branches, otherwise Git may not let you switch.
Making Changes on Your Branch
Now you can work on your feature. Any changes you commit here will only exist on this branch and will not affect main.
- Create or modify files for your feature (e.g., create a
contact.htmlfile). -
Stage your changes:
-
Commit your changes:
You can make multiple commits on your branch as you work. Each commit captures a step in your progress.
Pulling the Latest Changes from Main
Before merging your branch into main, it is important to make sure your local main branch is up to date with the remote repository. This is especially important when working with a team, as others may have pushed changes while you were working.
-
Switch back to the
mainbranch: -
Pull the latest changes from GitHub:
You should see output similar to:
Or, if there are new changes:
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
Updating c3d4e5f..f6g7h8i
Fast-forward
index.html | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
Do Not Skip This Step
If you skip pulling before merging, you risk creating merge conflicts (covered in Task 4). Always pull the latest changes from main before merging your feature branch.
Merging Your Branch into Main
Now that main is up to date, you can merge your feature branch into it.
-
Ensure you are on the
mainbranch (the branch you want to merge into): -
Run the merge command:
You should see output like:
Updating f6g7h8i..j9k0l1m
Fast-forward
contact.html | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
create mode 100644 contact.html
Success
If the merge completes without errors, your feature branch changes are now part of main!
What is Fast-Forward?
A fast-forward merge happens when main has not changed since you created your branch. Git simply moves the main pointer forward to include your new commits. If main has changed, Git will create a merge commit instead, which combines the histories of both branches.
Pushing the Merged Changes
After merging locally, push the updated main branch to GitHub.
-
Run the following command:
Deleting the Feature Branch
Once your feature branch has been merged, you no longer need it. Cleaning up old branches keeps your repository organized.
-
Delete the local branch:
You should see:
-
Delete the remote branch (if you pushed it to GitHub earlier):
Why Delete Branches?
Over time, old branches pile up and make it harder to see what is actively being worked on. Deleting merged branches is a common housekeeping practice in professional development.
Do Not Use -D Unless You Absolutely Need Too
The uppercase -D flag (git branch -D branch-name) force-deletes a branch even if it has not been merged. Only use this if you are completely sure you want to discard the work done on that branch.
Conclusion
In this task, you learned how to:
- Create a new branch with
git checkout -b - Verify and switch between branches with
git branchandgit checkout - Make commits on a feature branch
- Pull the latest changes from the remote with
git pull - Merge a branch into
mainwithgit merge - Delete branches after merging with
git branch -d
Branching lets you work on features, fixes, and experiments without risking the stability of your main codebase. In the next task, you will learn how to handle merge conflicts, which is what happens when two branches change the same lines of code.