Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Git Tutorial

Git HOME Git Intro Git Install Git Config Git Get Started Git New Files Git Staging Git Commit Git Remove and Move Git Tagging Git Stash Git History Git Diff Git Blame Git Help Git Branch Git Merge Git Workflow Git Best Practices

Git and GitHub

GitHub Get Started Git What is SSH? GitHub Add SSH GitHub Set Remote GitHub Edit Code Pull from GitHub Push to GitHub GitHub Branch Pull Branch from GitHub Push Branch to GitHub GitHub Flow GitHub Pages Git GUI Clients

Git Contribute

GitHub Fork Git Clone from GitHub GitHub Send Pull Request

Git Undo

Git Revert Git Reset Git Clean Git Amend Git Rebase Git Reflog Git Recovery

Git Advanced

Git .gitignore Git .gitattributes Git Large File Storage (LFS) Git Signing Commits/Tags Git Cherrypick & Patch Git Bisect Git Merge Conflicts Git CI/CD Git Hooks Git Submodules Git Worktree Git Remote Advanced

Git and Bitbucket

Bitbucket Get Started Bitbucket Add SSH Bitbucket Set Remote Bitbucket Edit Code Bitbucket Branch Bitbucket Pages Bitbucket Fork Git Clone from Bitbucket Bitbucket Send Pull Request

Git and GitLab

GitLab Get Started GitLab Add SSH GitLab Set Remote GitLab Edit Code GitLab Branch GitLab Pages GitLab Fork Git Clone from GitLab GitLab Send Pull Request

Git Cert

Git Certificate

Git Reference

Git Glossary

Git Exercises

Git Exercises Git Challenges Git Quiz Git Syllabus Git Study Plan

Git Branch Merge


Share

What is Merging in Git?

Merging in Git means combining the changes from one branch into another.

This is how you bring your work together after working separately on different features or bug fixes.

Common git merge Options


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 main branch:

Example

git checkout main
Switched to branch 'main'
Try it Yourself »

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

Example

git merge emergency-fix
Updating f938b79..e7e2d06
Fast-forward
 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Try it Yourself »

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

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

As main 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 e7e2d06).
Try it Yourself »

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.


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 'ort' strategy.
 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Try it Yourself »

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
Automatic merge went well; stopped before committing as requested
Squash commit -- not updating HEAD
Try it Yourself »

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
Try it Yourself »

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'
Try it Yourself »

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 ff63d40] added new image
 2 files changed, 1 insertion(+)
 create mode 100644 img_hello_git.jpg
Try it Yourself »

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

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

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

Example

git checkout main
Switched to branch 'main'
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.
Try it Yourself »

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

Let us check the status:

Example

git status
On branch main
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 &lt;file&gt;..." to mark resolution)
        both modified:   index.html
Try it Yourself »

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 main
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
Try it Yourself »

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"
[main 00f1c9e] merged with hello-world-images after fixing conflicts
Try it Yourself »

And delete the hello-world-images branch:

Example

git branch -d hello-world-images
Deleted branch hello-world-images (was ff63d40).
Try it Yourself »

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

Time to start working with a remote repository!




Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.