Learn Version Control with Git

git merge

The purpose of a branch is that it allows making modifications to your project without affecting the master branch.If you end up liking those modifications, you can combine those changes into another branch. This process is known as merging.

From the above diagram, both new-feature and master merged to create a commit that is a combination of both. Notice how the latest commit tracks both the new-feature commits and master commits.

you can also still add commits to the new-feature branch that won't be included in the master branch like so:

Notice how the merge commit 6 does not track the latest commit 7

How to merge

Before merging, it is important to be in the branch that you would like to merge changes to. So if you would like to merge the changes from new-feature into the master branch. Then it's important for you to be in the master branch.

Check which branch you're in with git branch, if you're not in the master branch, then:

git checkout master

let's merge new-feature and master:

git merge new-feature
>> Auto-merging project-1.txt
   CONFLICT (content): Merge conflict in project-1.txt
   Automatic merge failed; fix conflicts and then commit the result.

We're encountering our first merge issue, it looks like there is a conflict in our project-1.txt.

let's open the file and see what's inside:

Merge Conflicts

This is our first merge and we already have a conflict. Conflicts don't happen all the time, the reason we have a conflict here is that some content in new-feature and some content in master are sharing the same line in the editor. Git doesn't really know what to do here.

The editor shows the following:

  • <<<<<<< HEAD: anything below this, until ======, tells us what's conflicting in the current branch.

  • >>>>>>>> new-feature : anything above this, until ======, tells us what's conflicting from the new-feature branch.

We have two choices, we can remove one of the conflicts or keep both. we are going to delete the indicators and keep both. your editor should look something like this:

Now that our conflict has been resolved we need to create a commit:

git add .
git commit -m "merged new features"

Deleting our branch

Now that our feature has been added, we don't need the new-feature branch anymore, we can simply delete it:

git branch -d new-feature

let's check:

git branch

>> * master

Only our master branch shows, we successfully deleted our new-feature branch

Feedback Summary
3 students





Written Reviews
There are no written reviews yet.