DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. A Tutorial on Git Merge

A Tutorial on Git Merge

When you're working in a git branch you'll eventually have to integrate that code with the rest of your app. Learn how to use git merge to make this possible.

Kristina Garcia Francisco user avatar by
Kristina Garcia Francisco
·
May. 01, 18 · Tutorial
Like (15)
Save
Tweet
Share
22.67K Views

Join the DZone community and get the full member experience.

Join For Free

Isolating features into different branches is a crucial practice for any serious developer. By separating each feature, bugfix, or working experiment, you will avoid a lot of problems and keep your development branches clean.

At some point, a piece of code will reach a state where you'll want to integrate it with the rest of the project. This is where the git merge command comes in.

Preparing to Merge

Let's assume that we want to merge branch hotfix into your master branch.

Before we start, how do you make sure that you are ready to merge your changes?

  1. Check if your local repository is up to date with the latest changes from your remote server with a git fetch.
  2. Once the fetch is completed, use the git checkout master command.
  3. Ensure the master branch has the latest updates by executing git pull.
  4. Checkout to the branch that should receive the changes, in our case that is master.

Merging

Once the preparations are completed, you can start the merge with the git merge hotfix command.

Fast Forward Merge

A fast-forward merge can occur when there is a linear path between branches that we want to merge. If a master has not diverged, instead of creating a new commit, it will just point the master to the latest commit of the hotfix branch. All commits from the hotfix branch are now available in the master branch.

However, a fast-forward merge is not possible if the branches have diverged. In this case, you want to use a Three-way merge.

Three-Way Merge

When there is not a linear path to the target branch, Git has no choice but to combine them via a three-way merge. This merge uses an extra commit to tie together the two branches.

Test this out! Create your own project with an RSpec test branch and at the same time edit the Controller tests in master. Now, try to merge.

How to Deal With Merge Conflicts

A merge conflict occurs when two branches you're trying to merge both changed the same part of the same file. When this happens, Git won't be able to figure out which version to use.

For example, if the file example.rb was edited on the same lines in different branches of the same Git repository or if the file was deleted, you will get a merge conflict error when you try to merge these branches. Before you can continue, the merge conflict has to be resolved with a new commit.

Merge conflicts will only occur in the event of a 3-way merge.

  1. Generate a list of the files which need to be resolved: git status
  2. # On branch master
    # You have unmerged paths.
    #   (fix conflicts and run "git commit")
    # Unmerged paths:
    #   (use "git add ..." to mark resolution)
    # both modified: example.rb
    # no changes added to commit (use "git add" and/or "git commit -a")
  3. When the conflicted line is encountered, Git will edit the content of the affected files with visual indicators that mark both sides of the conflicting content. These visual markers are:
    • <<<<<<<- Conflict marker, the conflict starts after this line.
    • ======= - Divides your changes from the changes in the other branch.
    • >>>>>>> - End of the conflicted lines.
  4. <<<<<<< HEAD(master)
    conflicted text from HEAD(master)
    =======
    conflicted text from hotfix
    >>>>>>> hotfix
  5. Decide if you want to keep only your hotfix or master changes, or write a completely new code. Delete the conflict markers before merging your changes.
  6. When you're ready to merge, all you have to do is run the git addcommand on the conflicted files to tell Git they're resolved.
  7. Commit your changes with git commit to generate the merge commit.

Hope this helped you get a better understanding how to merge your branches and deal with conflicts.

Git Branch (computer science)

Published at DZone with permission of Kristina Garcia Francisco, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Quick Pattern-Matching Queries in PostgreSQL and YugabyteDB
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid
  • Event Driven 2.0
  • Real-Time Stream Processing With Hazelcast and StreamNative

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: