Topic 3
Merge
Merging brings work from one branch into another branch, usually from a task branch back into main.
What a merge means
A merge combines the commit history from one branch into another branch. In a common workflow, you create a separate branch for a task, finish the work there, and then merge that branch back into main.
Merging does not mean copying files by hand. Git compares the branch histories and brings the selected changes into the branch you are currently using.
Use a merge when:
- the branch work is finished
- you want the changes to become part of
main - you are working alone or the team has agreed that the branch is ready
Prepare the branches
Before merging, commit or stash your current changes. The merge should start from a clean working state.
Open a new terminal with Ctrl + Shift + ` on Windows or Cmd + Shift + ` on macOS. Then move to the branch that should receive the finished work:
git switch main
Then download the latest version of main:
git pull
Merge into main
Merge the finished branch into the branch you are currently using:
git merge draft-section
In this example, draft-section is the branch that contains the finished work. main is the branch receiving that work.
If Git can combine the changes automatically, the merge will finish immediately. If the same part of a file changed on both branches, Git may stop and ask you to resolve a conflict.
Push the merge
After the merge succeeds, upload the updated main branch to GitHub:
git push origin main
If the merged branch is no longer needed, you can delete it after confirming that the important work is now on main.
Pull from different branches
Sometimes you are working on one branch, such as new-contents, but you need to bring in the latest work from main. In that case, first make sure you are on the branch that should receive the changes.
For example, while you are on new-contents, run:
git pull origin main
This pulls the latest main branch from GitHub and merges it into the branch you are currently using.
If the two branches changed the same file, Git may stop and ask you to resolve a conflict. VS Code then shows the merge state in Source Control.
After you resolve the conflict and stage the files, click Continue to create the merge commit.
VS Code prepares a merge commit message. You can keep the default message or replace it with a short message that explains the merge.
Then sync or push the branch so GitHub receives the merge commit.
VS Code may warn that the action will pull and push commits for the current branch. Confirm only if you are ready to update that branch on GitHub.
After the sync finishes, Git Graph shows the merge commit on the current branch.
Remember that plain git pull pulls from the upstream branch of your current branch. To pull from main while staying on another branch, specify origin main.
Merge only when you know which branch should receive the changes. If other people need to review the work before it becomes part of main, use a Pull Request instead of merging directly.