Merging branches
With our styling work completed on the style
branch, it's time to merge those changes back to the main
branch.
First, switch back to the main
branch.
Switch to the main
branch using the git switch
command.
Tip: Use and arrow keys on your keyboard to navigate through the command history in the terminal. It's quicker than typing the command all over again.
Merging branches is a very common task when working with Git. When one branch is merged into another, it's not just the actual changes that are copied over, but also the commit history. Git carries over all the extra commits from the source branch to the target branch.
To merge the style
branch into main
, we'll use the git merge command. You pass the name of the source branch as an argument. All the changes from that branch are merged into the current branch.
git merge style
Merge the style
branch into the main
branch using the git merge
command.
If everything went well, we should see our style.css
file in the project directory. Let's check that.
Awesome, the style.css
file is now in the project directory. This means the merge was successful.
Let's also check the commit history with git log
.
Check whether the commits from the style
branch are now part of the main
history using git log
.
Tip: Remember that you can scroll through the output of git log
using the and arrow keys on your keyboard. To exit pager mode, press q.
We can also delete the style
branch now that we've merged it into main
. You can do that with the git branch command with the -d
option (shorthand for --delete
):
git branch -d style
Delete the style
branch using the command above.
Great, the style
branch is now deleted. This is a good practice to remove branches that are no longer needed to keep your repository tidy.
Here we tried only the most straightforward case of merging branches. But sometimes, you might run into nasty conflicts when merging branches. This happens when the same part of a file has been changed in both branches.
For example, if you changed the background color of a button in one branch and your colleague changed the text color of the same button in another branch, Git will not be able to automatically resolve these conflicts. You will have to fix them manually. If you'd like to learn more about resolving conflicts, stick around for Part II of the course.
Hi! I'm Alex, creator of GitByBit.
This page is a part of the interactive course about Git version control.
It's a one-of-a-kind course that is integrated into the VS Code code editor. Learning directly in VS Code lets you operate Git exactly as you would in real life, doing real jobs, writing real code.
In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.