Lesson 23 of 60 – Git Merge
38%

Git Merge

The git merge command is used to combine the changes from one branch into another branch. It is commonly used after completing a feature or bug fix on a separate branch and then bringing those changes into the main development branch.

Note: The branch you are currently on is the branch that receives the changes. Therefore, switch to the target branch before running git merge.

1. What is Git Merge?

git merge combines the history and changes of one branch into the currently checked-out branch.

git merge branch-name

For example:

git switch main
git merge feature-login

2. Why Use Git Merge?

Merge is useful when development work has been completed on a separate branch and needs to become part of another branch.

  • Combine feature work
  • Combine bug fixes
  • Integrate development branches
  • Bring completed work into main
  • Combine changes from multiple developers

3. Basic Merge Syntax

The basic syntax is:

git merge branch-name

The specified branch is merged into the branch that is currently checked out.

4. The Target Branch

The current branch is the target branch of the merge.

git switch main
git merge feature-login

Here, feature-login is merged into main.

5. Simple Branch Merge

main
  |
  A → B

feature-login
      |
      C → D

If you switch to main and merge feature-login, Git combines the histories.

git switch main
git merge feature-login

6. Creating a Feature Branch

git switch main
git switch -c feature-login

Now you can develop the feature independently.

7. Making Changes on a Feature Branch

git add .
git commit -m "Add login feature"

The feature branch now contains a commit that can later be merged into another branch.

8. Switching to Main Before Merge

Before merging the feature branch into main, switch to main:

git switch main

Verify the current branch:

git branch --show-current

9. Performing the Merge

Now merge the feature branch:

git merge feature-login

Git combines the changes from feature-login into main.

10. Fast-Forward Merge

A fast-forward merge can occur when the target branch has not moved forward since the feature branch was created.

A → B → C → D
         ↑
      feature

If main still points to C, Git can move the main pointer forward to D without creating a merge commit.

11. Merge Commit

If the branches have diverged, Git may create a merge commit.

        C → D
       /     \
A → B         M
       \     /
        E → F

The merge commit combines the two development histories.

12. Viewing the Merge History

After merging, use:

git log --oneline --graph --all

This can help you visualize branches and merge commits.

13. Checking Merge Status

Use:

git status

After a successful merge, Git may show that the working tree is clean if there are no additional changes.

14. Merging a Bug-Fix Branch

git switch fix-navbar

# Fix the problem
git add .
git commit -m "Fix navbar issue"

git switch main
git merge fix-navbar

The completed bug fix is now integrated into the main branch.

15. Merge Conflicts

A merge conflict can happen when Git cannot automatically combine changes from the branches.

This commonly happens when different branches modify the same parts of the same file.

git merge feature-login

Git may stop and ask you to resolve the conflicting files.

16. Checking Merge Conflicts

If a merge conflict occurs, use:

git status

Git will identify files that have unresolved conflicts.

17. Conflict Markers

A conflicted file can contain markers such as:

<<<<<<< HEAD
Your current branch changes
=======
Incoming branch changes
>>>>>>> feature-login

These markers help you identify the competing changes.

18. Resolving a Merge Conflict

To resolve a conflict:

  1. Open the conflicted file.
  2. Review both sets of changes.
  3. Remove the conflict markers.
  4. Keep the correct final content.
  5. Save the file.
  6. Stage the resolved file.
git add filename

19. Completing a Merge After Conflict Resolution

After resolving and staging all conflicts, complete the merge:

git commit

Git may prepare an appropriate merge commit message automatically.

20. Aborting a Merge

If you do not want to continue a merge that is currently in progress, you can abort it:

git merge --abort

This attempts to return the repository to the state it had before the merge started.

21. Merging Multiple Features

git switch main

git merge feature-login
git merge feature-dashboard
git merge feature-payment

Each branch is merged separately into the current branch.

22. No Fast-Forward Merge

The --no-ff option can be used to create a merge commit even when Git could perform a fast-forward merge.

git merge --no-ff feature-login

This can preserve an explicit merge point in the project history.

23. Merge Without Automatic Commit

The --no-commit option can stop before creating the merge commit when a merge commit would otherwise be created.

git merge --no-commit feature-login

This gives you an opportunity to inspect the result before completing the merge commit.

24. Merge and Remote Repository

After merging locally, you can push the updated target branch to a remote repository when appropriate.

git switch main
git merge feature-login
git push

The merge itself happens locally; git push sends the resulting commits to the remote repository.

25. Merge vs Rebase

Merge Rebase
Combines branch histories Replays commits onto another base
Can create a merge commit Normally creates a different linear history
Preserves the existing branch relationships Rewrites commit history for the rebased commits

The appropriate workflow depends on the project's collaboration rules.

26. Common Git Merge Mistakes

  • Merging into the wrong branch.
  • Forgetting to check the current branch.
  • Ignoring uncommitted changes before merging.
  • Not resolving all merge conflicts.
  • Forgetting to stage resolved files.
  • Using the wrong source branch.
  • Pushing before reviewing the merge result.

27. Useful Git Merge Commands

git merge feature-login
git merge --no-ff feature-login
git merge --no-commit feature-login
git merge --abort
git status
git add filename
git commit
git log --oneline --graph --all

28. Complete Feature Merge Workflow

# Create feature branch
git switch -c feature-login

# Make changes
git add .
git commit -m "Add login feature"

# Switch to main
git switch main

# Merge feature
git merge feature-login

# Check result
git status

# View history
git log --oneline --graph --all

29. Practical Merge Example

git init

git add .
git commit -m "Initial project"

git switch -c feature-about

# Create about page

git add .
git commit -m "Add about page"

git switch main

git merge feature-about

git log --oneline --graph --all

The feature-about branch is merged into main, bringing the About page changes into the main development line.

30. Summary of Git Merge

The git merge command combines changes from one branch into the current branch.

git switch main
git merge feature-login

Git may perform a fast-forward merge or create a merge commit, depending on the branch history.

If conflicts occur, resolve them, stage the resolved files, and complete the merge. If you do not want to continue, use git merge --abort while the merge is in progress.

📌 Key Points

  • git merge combines changes from one branch into another.
  • The current branch receives the merged changes.
  • Use git switch to move to the target branch first.
  • Use git merge branch-name to merge a branch.
  • Git can perform a fast-forward merge.
  • Git can create a merge commit when histories have diverged.
  • Merge conflicts can happen when changes cannot be combined automatically.
  • Use git status to identify unresolved conflicts.
  • Use git add after resolving conflicted files.
  • Use git merge --abort to cancel a merge in progress.
  • git merge --no-ff can force a merge commit.
  • After a local merge, git push can share the result with a remote repository.

🧠 Quick Quiz

Question: Which branch receives the changes when you run git merge feature-login?