How do I merge git branches?

I had multiple branches in git and I have multiple commits from each branches. Now I wanted to merge all the branches to main branch. can you help me on this.

>Solution :

Merging Branches in a Local Repository
To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git mergeand specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch. Note that this is a fast-forward merge.

git checkout main
git merge jeff/feature1

Work continues on the main and other branches, so they no longer share a common commit history. Now a developer wants to merge the jeff/feature2 branch into the main branch. Instead, Git performs a three-way (or recursive) merge commit.

git checkout main
git merge jeff/feature2

Merging Branches to Remote Repository
If you create a branch in your local repository, the remote repository is not aware of the branch’s existence. Before you can push the branch code in the remote repository, you set the remote repository as the upstream branch using the git pushcommand. This command simultaneously sets the upstream branch and pushes the branch contents to the remote repository.

git push --set-upstream origin <branch name>

Merging Main into a Branch
While you are working on your branch, other developers may update the main branch with their branch. This action means your branch is now out of date of the main branch and missing content. You can merge the main branch into your branch by checking out your branch and using the same git merge command.

git checkout <branch name>
git merge main

For more detail answer please follow this url
https://www.varonis.com/blog/git-branching#:~:text=To%20merge%20branches%20locally%2C%20use,branch%20into%20the%20main%20branch.

Leave a Reply