I'll go straight to the point!
To merge a branch into the main branch in Git, simply follow these steps:
- Checkout Main Branch: First, make sure you are on the main branch typing the following command:
git checkout main
- 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
- Merge Branch: Now, you can merge the feature (or any other) branch into the main branch:
git merge <branch-name>
<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.
- 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
- 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"
- Push Changes: Finally, push the changes to the remote repository:
git push origin main
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.