Branches and merges
Merge a branch
Bring a completed branch into the current branch.
A merge brings another line of history into the current branch.
First, make sure the working tree is clean and switch to the branch that should receive the work:
git status
git switch main
git merge add-search
The direction matters. This command merges add-search into main because main is the current branch.
Git finds the common ancestor and compares both histories. If main has not moved, Git can fast-forward its reference. If both branches have new commits, Git usually creates a merge commit after combining compatible changes.
Verify and recover
Verify the result:
git status
git log --oneline --decorate --graph --all
After a successful merge, main reaches the feature commits. The add-search reference still exists until you delete it.
If Git reports a conflict, stop and inspect git status. You can resolve the files and continue, or return to the pre-merge state with git merge --abort before making unrelated changes.
My advice is to merge from the branch that should receive the work, not from the feature branch. Standing on main and running git merge add-search is the habit that keeps direction clear.
After the merge succeeds, run your tests. A clean merge only means Git could combine the files automatically. It does not guarantee the program still works.
You can delete the feature branch once main contains its commits and you no longer need the name. The commits stay in history either way.
If the feature branch grew large, read the graph after merging. A merge commit makes the join visible even when Git could have fast-forwarded.
Try this on your own project: merge a one-commit branch into an unchanged main. Draw the graph before and after, and explain why no merge commit was necessary.
Lesson completed