How to Merge One Local Branch Into Another in Git

Avatar

By squashlabs, Last Updated: Oct. 28, 2023

How to Merge One Local Branch Into Another in Git

Step 1: Checkout the branch you want to merge into

To merge one local branch into another in Git, you first need to checkout the branch that you want to merge into. This can be done using the following command:

git checkout <destination_branch>

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

Related Article: How to Stash Untracked Files in Git

Step 2: Merge the source branch into the destination branch

Once you have checked out the destination branch, you can proceed with merging the source branch into it. This can be done using the following command:

git merge <source_branch>

Replace <source_branch> with the name of the branch you want to merge into the destination branch.

Step 3: Resolve any conflicts

If there are any conflicts between the source branch and the destination branch, Git will pause the merge process and notify you about the conflicts. You can use a Git merge tool or manually edit the conflicting files to resolve the conflicts.

After resolving the conflicts, you need to mark the conflicts as resolved by using the following command:

git add <conflicted_file>

Replace <conflicted_file> with the path to the conflicted file.

Once all conflicts are resolved and marked as resolved, you can continue the merge by running the following command:

git merge --continue

Step 4: Push the merged branch

After successfully merging the source branch into the destination branch, you can push the merged branch to the remote repository using the following command:

git push origin <destination_branch>

Replace <destination_branch> with the name of the branch you merged into.

It is important to note that if you are merging branches that have been pushed to a remote repository, you might need to perform additional steps to synchronize your local repository with the remote repository.

Related Article: How to Set the Upstream Origin Branch in Git

Alternative: Using Git GUI tools

If you prefer a graphical interface, there are several Git GUI tools available that can help you merge one local branch into another. Some popular options include:

- GitKraken: A cross-platform Git GUI client with built-in merge and conflict resolution tools. You can easily select the source and destination branches and initiate the merge process.

- Sourcetree: A free Git GUI client for Windows and Mac that provides a visual representation of your Git repositories. It allows you to easily perform branch merges and resolve conflicts.

Using a Git GUI tool can provide a more user-friendly experience, especially for beginners or those who prefer visual interfaces.

Best practices

When merging one local branch into another in Git, it is important to follow some best practices to ensure a smooth and efficient merge process:

1. Always update your branches: Before merging, make sure to fetch the latest changes from the remote repository and update your local branches. This helps prevent conflicts and ensures you are working with the latest code.

2. Test your changes: Before merging, it is a good practice to test your changes on the source branch to ensure they work as expected. This can help identify any issues or conflicts that need to be resolved before merging.

3. Resolve conflicts promptly: If conflicts occur during the merge process, it is important to resolve them promptly. Conflicts can arise when the same lines of code have been modified in both the source and destination branches. Take the time to carefully review and resolve conflicts to ensure the integrity of your codebase.

4. Review the merge result: After merging, review the resulting code to make sure everything has been merged correctly and there are no unexpected changes or issues. This can help catch any mistakes or inconsistencies that may have occurred during the merge process.

More Articles from the Git Tutorial: From Basics to Advanced Concepts series:

How to Undo Pushed Commits Using Git

This article provides a detailed guide on how to use Git to undo pushed commits. It covers two options: reverting the commit and resetting the branch… read more

How To Modify Unpushed Commit Messages

Modifying unpushed commit messages in Git is a simple task using the git commit amend command. In this article, you will learn how to identify the co… read more

Tutorial: HEAD in Git

A brief guide on using 'Head' in Git for version control. This article covers key aspects such as viewing the commit pointed by 'Head', moving 'Head'… read more

How To Use Git Pull Rebase

Git pull rebase is a useful tool in your Git workflow for merging code efficiently and avoiding unnecessary merge commits. This article explores why … read more

How to Git Pull from a Specific Branch

Executing a git pull from a specific branch is a fundamental skill for any developer working with Git. This article provides a concise guide on how t… read more

How to Force Overwrite During Git Merge

This article provides a step-by-step guide on how to force overwrite during a Git merge operation. It covers two methods: using the --strategy-option… read more

How to Discard All Local Changes in a Git Project

Guide on reverting all local changes in a Git-managed project to its previous state. This article provides step-by-step instructions on how to discar… read more

How to Fix Git Error: Could Not Read From Remote Repository

A simple guide to resolve the common Git error 'Could Not Read From Remote Repository.' Learn how to verify the remote repository URL, check authenti… read more

How To Use Git Reset Hard Head To Revert To A Previous Commit

Reverting to a previous commit in your Git repositories can be a simple and process using the git reset --hard HEAD command. This article will guide … read more

How to Throw Away Local Commits in Git

Removing local commits in Git can be a simple process with the right methods. This article provides two methods, using git reset and git revert, to h… read more