KoderSolution Logo
HomeArticlesTutorialsForumAI LabRun Code
KoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Engineering Newsletter

Join 100,000+ engineers receiving curated high-signal content weekly.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
📦

Git

Topic Hub & Articles

Git HOME

10 min

Git Intro

10 min

Git Install

10 min

Git Config

10 min

Git Get Started

10 min

Git New Files

10 min

Git Staging

10 min

Git Commit

10 min

Git Tagging

10 min

Git Stash

10 min

Git History

10 min

Git Help

10 min

Git Branch

10 min

Git Merge

10 min

Git Workflow

10 min

Git Best Practices

10 min

Git Glossary

10 min

GitHub Get Started

10 min

Git What is SSH?

10 min

GitHub Add SSH

10 min

GitHub Set Remote

10 min

GitHub Edit Code

10 min

Pull from GitHub

10 min

Push to GitHub

10 min

GitHub Branch

10 min

Pull Branch from GitHub

10 min

Push Branch to GitHub

10 min

GitHub Flow

10 min

GitHub Pages

10 min

Git GUI Clients

10 min

GitHub Fork

10 min

Git Clone

10 min

GitHub Pull Request

10 min

Git Revert

10 min

Git Reset

10 min

Git Amend

10 min

Git Rebase

10 min

Git Reflog

10 min

Git Recovery

10 min

Git .gitignore

10 min

Git .gitattributes

10 min

Git Large File Storage

10 min

Git Signing

10 min

Git Cherry Pick

10 min

Git Merge Conflicts

10 min

Git CI/CD

10 min

Git Hooks

10 min

Git Submodules

10 min

Git Remote Advanced

10 min

Git Exercises

10 min

Git Quiz

10 min

Git Syllabus

10 min

Git Study Plan

10 min

Progress
0%

0 / 53 Lessons

GitGit Tutorial
Lesson

Git Merge

10 min reading
Free Course

Git Merge

Merging combines commit histories from separate branches, integrating feature updates back into your primary branch.

Fast-Forward vs 3-Way Merge

flowchart TD
    subgraph FastForward ["Fast-Forward Merge"]
        A1["main: C1"] --> A2["main: C2"] --> A3["feature: C3"]
    end
    subgraph ThreeWay ["3-Way Merge Commit"]
        B1["Base: C1"] --> B2["main: C2"]
        B1 --> B3["feature: C3"]
        B2 --> M["Merge Commit: M1"]
        B3 --> M
    end

Core Concepts & Explanation


Common git merge Options

  • git merge - Merge a branch into your current branch
  • git merge --no-ff - Always create a merge commit
  • git merge --squash - Combine changes into a single commit
  • git merge --abort - Abort a merge in progress

Merging Branches (git merge)

To combine the changes from one branch into another, use git merge.

Usually, you first switch to the branch you want to merge into (often main or master), then run the merge command with the branch name you want to combine in.

First, we need to change to the master branch:

Example
git checkout master
Switched to branch 'master'

Now we merge the current branch (master) with emergency-fix:

Example
git merge emergency-fix
Updating 09f4acd..dfa79db
Fast-forward
 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Since the emergency-fix branch came directly from master, and no other changes had been made to master while we were working, Git sees this as a continuation of master.

So it can "Fast-forward", just pointing both master and emergency-fix to the same commit.

Best Practices for Merging Branches

  • Always commit or stash your changes before starting a merge.
  • Regularly merge from the main branch into your feature branch to minimize conflicts.
  • Read and resolve conflicts carefully-don't just accept all changes blindly.
  • Write clear and descriptive merge commit messages.

Practical Examples

  • Abort a merge: git merge --abort
  • Check status during a merge: git status
  • Resolve a conflict and complete the merge: Edit the conflicted file(s), then git add file and git commit
  • Fast-forward merge: Happens when no new commits diverged-Git just moves the branch pointer forward.
  • No-fast-forward merge: Use git merge --no-ff branch to always create a merge commit, preserving branch history.

As master and emergency-fix are essentially the same now, we can delete emergency-fix, as it is no longer needed:

Example
git branch -d emergency-fix
Deleted branch emergency-fix (was dfa79db).


Non-Fast-Forward Merge (git merge --no-ff)

By default, if your branch can be merged with a fast-forward (no new commits on the base), Git just moves the branch pointer forward.

If you want to always create a merge commit (to keep history clearer), use git merge --no-ff branchname.

Example
git merge --no-ff feature-branch
Merge made by the 'recursive' strategy.
 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Squash Merge (git merge --squash)

If you want to combine all the changes from a branch into a single commit (instead of keeping every commit), use git merge --squash branchname.

This is useful for cleaning up commit history before merging.

Example
git merge --squash feature-branch
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested

Aborting a Merge (git merge --abort)

If you run into trouble during a merge (like a conflict you don't want to resolve), you can cancel the merge and go back to how things were before with git merge --abort.

Example
git merge --abort

What is a Merge Conflict?

A merge conflict happens when changes in two branches touch the same part of a file and Git doesn't know which version to keep.

Think of it like two people editing the same sentence in a document in different ways-Git needs your help to decide which version to use.


How to Resolve a Merge Conflict

Git will mark the conflict in your file.

You need to open the file, look for lines like <<<<<<< HEAD and =======, and decide what the final version should be.

Then, stage and commit your changes.


Troubleshooting & Tips

  • If you want to cancel a merge, use git merge --abort.
  • Always commit or stash your changes before starting a merge.
  • Read the conflict markers carefully and remove them after you've resolved the issue.
  • Use git status to see what files need your attention.
  • If you're unsure, ask a teammate or look up the error message.

Merge Conflict Example

Now we can move over to hello-world-images from last chapter, and keep working.

Add another image file (img_hello_git.jpg) and change index.html, so it shows it:

Example
git checkout hello-world-images
Switched to branch 'hello-world-images'

Example

  <!DOCTYPE html><html><head><title>Hello World!</title>
  <link rel="stylesheet" href="bluestyle.css"></head><body>
  <h1>Hello world!</h1><div><img src="img_hello_world.jpg" alt="Hello World 
  from Space" style="width:100%;max-width:960px"></div><p>This is the first 
  file in my new Git Repo.</p><p>A new line in our file!</p><div><img 
  src="img_hello_git.jpg" alt="Hello Git" 
  style="width:100%;max-width:640px"></div></body></html>

Now, we are done with our work here and can stage and commit for this branch:

Example
git add --all
git commit -m "added new image"
[hello-world-images 1f1584e] added new image
 2 files changed, 1 insertion(+)
 create mode 100644 img_hello_git.jpg

We see that index.html has been changed in both branches.

Now we are ready to merge hello-world-images into master.

But what will happen to the changes we recently made in master?

Example
git checkout master
git merge hello-world-images
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

The merge failed, as there is conflict between the versions for index.html.

Let us check the status:

Example
git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Changes to be committed:
        new file:   img_hello_git.jpg
        new file:   img_hello_world.jpg

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   index.html

This confirms there is a conflict in index.html, but the image files are ready and staged to be committed.

So we need to fix that conflict. Open the file in our editor:

Example

  <!DOCTYPE html><html><head><title>Hello World!</title><link 
  rel="stylesheet" href="bluestyle.css"></head><body><h1>Hello 
  world!</h1><div><img src="img_hello_world.jpg" alt="Hello World from 
  Space" style="width:100%;max-width:960px"></div><p>This is the first file 
  in my new Git Repo.</p><<<<<<< HEAD<p>This line is here to show how 
  merging works.</p>=======<p>A new line in our file!</p><div><img 
  src="img_hello_git.jpg" alt="Hello Git" 
  style="width:100%;max-width:640px"></div>>>>>>>> hello-world-images
</body></html>

We can see the differences between the versions and edit it like we want:

Example

  <!DOCTYPE html><html><head><title>Hello World!</title><link 
  rel="stylesheet" href="bluestyle.css"></head><body><h1>Hello 
  world!</h1><div><img src="img_hello_world.jpg" alt="Hello World from 
  Space" style="width:100%;max-width:960px"></div><p>This is the first file 
  in my new Git Repo.</p><p>This line is here to show how 
  merging works.</p><div><img 
  src="img_hello_git.jpg" alt="Hello Git" 
  style="width:100%;max-width:640px"></div>
</body></html>

Now we can stage index.html and check the status:

Example
git add index.html
git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
        new file:   img_hello_git.jpg
        new file:   img_hello_world.jpg
        modified:   index.html

The conflict has been fixed, and we can use commit to conclude the merge:

Example
git commit -m "merged with hello-world-images after fixing conflicts"
[master e0b6038] merged with hello-world-images after fixing conflicts

And delete the hello-world-images branch:

Example
git branch -d hello-world-images
Deleted branch hello-world-images (was 1f1584e).

Now you have a better understanding of how branches and merging works.

Time to start working with a remote repository!



Practical Terminal Workflow

Execute the following terminal commands to work with this concept in your workspace:

# Switch to main target branch
git switch main

# Merge feature branch into main
git merge feature/login-page

Best Practices & Pro-Tips

  • Atomic Commits: Keep each commit small, focused, and dedicated to a single logical feature or bug fix.
  • Imperative Commit Messages: Write commit titles in imperative mood (e.g., Fix CORS bug rather than Fixed CORS bug).
  • Review Diff Before Staging: Run git diff to review unstaged modifications before adding files to the index.

Common Gotchas & Troubleshooting

  • Detached HEAD State: Triggered when checking out a specific commit hash directly rather than a branch. Create a new branch with git switch -c <branch-name> to preserve edits.
  • Accidentally Staged Binary Files: Run git restore --staged <filename> to remove the file from the index without altering your local workspace copy.

Self-Check Challenge

Open your terminal in a test project directory, execute git status, and verify your current working tree state. Practice running git log --oneline to review commit history.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Recommended Courses & Books

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum