Once the work on a branch is complete, you usually want to bring those changes back into the main branch. This operation is called a merge.
Suppose we have main and feature/login. The feature branch adds commits D and E after main's A → B → C history:
We now want main to include the work from D and E.
Step 1: Switch to the Branch Receiving the Changes
A useful rule is:
First switch to the branch you want to merge into.
If we want to merge feature/login into main, start with:
git switch mainThen run:
git merge feature/loginYou can read this as:
I am currently on main
+
merge feature/login into itThe branch after git merge is the branch whose changes you want to bring into your current branch.
What Happens After a Successful Merge?
If Git can combine the branches automatically, the merge completes without any extra work.
Before merging, main remains at C while feature/login points to E. After merging, main includes the feature commits D and E, as shown on the right side of the diagram.
The exact shape of Git history can vary depending on the type of merge, but the important result is simple: the login changes are now part of main.
Branch merge visualizer
Switch to main, merge feature/login, and watch the branch pointers converge.
—
—
Switch to main, merge feature/login, and watch the branch pointers converge.Check the Project After Merging
After a merge, you can run:
git statusto check the repository state.
You can also inspect the commit history:
git log --onelineMore importantly, run the project or test the relevant feature. Git can combine code successfully, but that does not guarantee that the application behaves correctly.
Two branches can merge without a Git conflict and still introduce a bug when their changes interact. A quick test after merging is therefore a good habit.
Deleting a Finished Branch
Once feature/login has been successfully merged into main, you usually no longer need the local branch.
Delete it with:
git branch -d feature/loginThe -d means delete.
Then run:
git branchand you may see:
* mainDeleting the branch does not remove the code that was already merged into main. The branch name is simply no longer needed because its work has already become part of the main branch.
KNOWLEDGE QUIZ
feature/login into main?
A Complete Merge Example
Suppose you finish the login work on:
feature/loginand commit it:
git add .
git commit -m "Add login feature"Switch to main:
git switch mainMerge the feature branch:
git merge feature/loginCheck the result:
git statusIf everything is correct, delete the finished branch:
git branch -d feature/loginThe workflow is:
Finish feature/login → Commit → git switch main → git merge feature/login → Test → Delete feature/login
Do Teams Always Merge Locally?
No.
In team projects, a more common workflow is to push the branch to GitHub and open a Pull Request (PR). Other developers can review the code, CI checks can run automatically, and the branch can then be merged through GitHub.
That complete workflow is covered later with the collaboration material.
Even so, understanding git merge locally is still valuable because GitHub's merge process is based on the same branch concepts.
Practice
feature/profile is complete and committed. Write the commands to merge it into main, inspect the result, run tests, and delete the finished branch.