๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Merging Changes in Git Repository - git merge


Introduction

  • git merge command is used to update your current local branch with changes from the remote branch (like origin/main).
  • Here, you are using this command after running git fetch, which downloads the latest changes from the remote server.
  • This method gives you more control than git pull, because you can see the changes first before merging them into your branch.

How to use git merge ?

  1. Fetch Changes :-
    • In previous git fetch tutorial, we have retrieved the latest changes from the GitHub repository.
    • Below are the commands with output:
      Git Fetch Command
  2. Merge Changes in Another Local Repository :-
    • Now, in another local directory where we have cloned the repository, we will merge the latest changes from the original location.
    • Syntax :
      • git merge FETCH_HEAD
    • The command will show the following output:
      Git Merge Command
      This command merge the latest changes from the specified repository.

Different ways to use git merge ?

  1. Merging Changes Locally:
    • Syntax :
      • git merge FETCH_HEAD
    • Example:
      • git merge FETCH_HEAD
    • Merges fetched changes from another local repository after using git fetch.
  2. 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.
  3. Abort a Merge in Case of Conflict:
    • Syntax :
      • git merge --abort
    • If conflicts occur during merging, this command cancels the merge process.
  4. 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.
  5. 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.
  6. 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.