DEV Community

Rita Kairu
Rita Kairu

Posted on

Merging Your Git Branch to Main: A Simple Step-by-step Guide

I'll go straight to the point!

To merge a branch into the main branch in Git, simply follow these steps:

  1. Checkout Main Branch: First, make sure you are on the main branch typing the following command:
   git checkout main
Enter fullscreen mode Exit fullscreen mode
  1. Pull Latest Changes: It's a good practice to ensure your main branch is up-to-date with the remote repository before merging any changes. Use:
   git pull origin main
Enter fullscreen mode Exit fullscreen mode
  1. Merge Branch: Now, you can merge the feature (or any other) branch into the main branch:
   git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

Replace <branch-name> with the name of the branch you want to merge into the main branch.

If there are no conflicts, Git will automatically perform the merge. If there are conflicts, Git will pause the merge and let you resolve the conflicts manually.

  1. Resolve Conflicts (if any): If there are conflicts, Git will mark the conflicted files. Open those files, and Git will show you the conflicting sections. Manually resolve the conflicts, save the files, and then continue the merge:
   git add <conflicted-file1> <conflicted-file2>  # Add resolved files
   git merge --continue
Enter fullscreen mode Exit fullscreen mode
  1. Commit the Merge: After resolving conflicts (or if there were none), you need to commit the changes:
   git commit -m "Merge branch-name into main"
Enter fullscreen mode Exit fullscreen mode
  1. Push Changes: Finally, push the changes to the remote repository:
   git push origin main
Enter fullscreen mode Exit fullscreen mode

That's it! You've successfully merged the changes from your branch into the main branch.

P.S. It's good practice to merge branches with a clean working directory. So consider committing or stashing any local changes before starting the merge process.

Top comments (0)