Merging GitHub Branches

How to merge GitHub Branches

When you create a branch for a new feature with Git, you are ostensibly making a separate file (forking) that will have no larger effect on the main project file until you are ready to merge that branch. Merging is a means of putting forked files, or branches, back together. When you merge a branch into your main project file, you are fusing the two files and all their working code back into a single branch.

All feature branches have a commit in common with the main branch in their history.

“git merge”

Using the command git merge takes all commits from a secondary or tertiary feature branch and integrates it with the main working branch to form a consolidated history of commits. Git merge does this by identifying a common ancestor in the commit history of both branches (as shown above). This common ancestor commit is used by git to create a new merge commit that will fuse the differences of each branch into a singular sequence.

You must be on the receiving branch before merging. To merge secondary branches into your main branch you must be working on the main branch in your code editor. You can use the git status terminal command to see what branch you are currently on. If necessary, use terminal command git checkout main to switch to that branch.

Make sure that all branches are up to date by using terminal command git fetch on both main and feature branches, and then execute git pull on your main branch.

Once all of these steps are complete and you are on the main branch in your code editor, use git merge <feature-branch-name> to merge the given feature branch into your main branch.

I hope this was helpful and thanks for reading!

Further Reading and Sources



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sam Hall

Sam Hall

Full stack developer and stand-up comic.