bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Git/Git Tutorial
Git•Git Tutorial

Git Branch Merge

Concept visual

Git Branch Merge

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

Change Platform:

GitHub

Bitbucket

GitLab

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 git merge

Formula

- 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'

Formula

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

Example git merge emergency-fix

Updating 09f4acd..dfa79db

Formula

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.

Formula

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.

Formula

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:

Formula

Happens when no new commits diverged - Git just moves the branch pointer forward.

No-fast-forward merge:

Formula

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

Formula

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.

Formula

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

Formula

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>

Formula

< title > Hello World!</title >

<link rel="stylesheet" href="bluestyle.css"> </head> <body>

Formula

< h1 > Hello world!</h1 >
<div><img src="img_hello_world.jpg" alt="Hello World from Space" style="width:100%;max-width:960px"></div>

Formula

< 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"

Formula

[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.

Formula

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

Formula

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:

Previous

Git Branch

Next

Git Workflow