Basic Merging
Basic merging combines the history and changes from one Git branch into another. Before you run a merge command, always remember the golden rule of Git merging: Move to the branch that you want to receive the changes.
Here is the step-by-step workflow for a basic merge.
The Standard Merge Workflow
Assume you want to merge a feature branch named
feature-login into your
main branch.
1. Switch to the Receiving Branch
Move to the branch you want to update, usually
main or master.
git switch main
2. Pull the Latest Updates
Update your local branch with the latest changes from the remote repository before merging.
git pull origin main
3. Merge the Target Branch
Bring the changes from
feature-login into your currently
active main branch.
git merge feature-login
4. Push the Merged Changes
Upload the merged changes to the remote repository.
git push origin main
The Two Types of Merges
When you run git merge, Git determines
how to combine the branches based on their commit history.
| Merge Type | What It Means | What Happens |
|---|---|---|
| Fast-Forward | The receiving branch has no new commits since the feature branch was created. | Git moves the receiving branch pointer forward. No new merge commit is created. |
| Three-Way Merge | Both branches have independent commits after they diverged. | Git combines the histories using their common ancestor. A merge commit is normally created when the merge succeeds and is not fast-forwarded. |
Cleaning Up After the Merge
After the feature has been successfully merged and pushed, delete the old feature branch if it is no longer needed.
1. Delete the Local Branch
git branch -d feature-login
Deletes the local feature branch after Git confirms its changes have been merged appropriately.
2. Delete the Remote Branch
If the feature branch was pushed to GitHub, you can also delete it from the remote repository.
git push origin --delete feature-login