🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

Merging Branches in Git Repository - git merge


Introduction

  • git merge command is used to combine the work from one branch into another, typically during development.
  • The most common use case is merging a feature branch (like feature-xyz) into a main or development branch.
  • It’s commonly used in collaborative environments when feature branches are finished and ready to be integrated into the main branch.
  • Merging keeps the full history of changes from both branches.
  • There are two types of merges:
    1. Fast-forward merge: Happens when the current branch has not diverged from the target branch.
    2. Three-way merge: Happens when both branches have diverged — Git creates a new merge commit.

How to use git merge ?

  1. Open the Git terminal (or command prompt) and navigate to the project folder where your Git repository is initialized.
  2. Create and Commit on a Feature Branch :-
    • Let’s assume you're on the default branch (main) and want to work on a new feature.
      • Create a new branch (if not created):
        • git branch feature-1
      • Switch to the new branch:
        • git switch feature-1
      • Make changes to your files, then add and commit them:
        • git add .
        • git commit -m "Implemented feature-1"
    • Below are the commands with output:
      Git Branch Command
  3. Switch Back to the Main Branch :-
    • Once your work on feature-1 is complete, switch back to the main branch:
      • git switch main
  4. Merge the Feature Branch into Main
    • Now, merge the feature-1 branch into main
      • git merge feature-1
        • The git merge command always merges the specified branch (i.e. feature-1) into your current branch (i.e. main).
        • If there are no conflicts, Git will create a merge commit automatically.
        • If there are conflicts, resolve them manually in the files. Then:
          • git add .
          • git commit
  5. (Optional) Delete the Feature Branch
    • After successful merging, you can delete the feature branch:
    • git branch -d feature-1
  6. Verify the Merge
    • Use this to check the branch list and confirm you’re on main
      • git branch
    • And view the merge history:
      • git log --oneline
  • Below is the output of the above commands:
    • Git Merge Branch Command

Different ways to use git merge ?

  1. Merging a Local Branch into the Current Branch:
    • Syntax :
      • git merge <branch-name>
    • Example:
      • git merge feature-branch
    • Merges feature-branch into the currently checked-out branch.
  2. Abort a Merge in Case of Conflict:
    • Syntax :
      • git merge --abort
    • If conflicts occur during merging, this command cancels the merge process.
  3. Merging After Fetching from a Remote Repository (Most Common):
    • Syntax :
      • git merge origin/<branch-name>
    • Example :
      • git merge origin/main
    • Merges the latest changes from the remote main branch into your local branch after fetching updates.
  4. Fast-Forward Merge (If No Diverging Changes Exist) (Commonly Used) :
    • Syntax :
      • git merge --ff-only <branch-name>
    • Exmaple :
      • git merge --ff-only origin/main
    • Merges the remote branch only if no conflicting changes exist.
  5. Squash Merge (Combine Multiple Commits into One Before Merging) (Commonly Used for Clean History) :
    • Syntax :
      • git merge --squash <branch-name>
    • Example :
      • git merge --squash feature-branch
    • Combines all commits from feature-branch into a single commit before merging.