A merge in Git occurs when two branches are combined in the repository.
Let's say a programmer wants to merge a branch called
main. To accomplish this:
The programmer switches to the branch they want to merge into. In this case:
$ git checkout main
The programmer then runs the merge command:
$ git merge test
When successful, the code in the
test branch is pulled into
This process is often seamless. In the example in the previous section, a programmer created a branch to change the HTML and the other programmer did the same to change the CSS. Because the two programmers changed different files, the merge of the updated HTML and updated CSS won't create a conflict.
A merge conflict occurs when a change was made to the same line of code on both branches. Git doesn't know which change to accept, so it is up to the programmers to resolve it. Merge conflicts are minor on small applications, but can cause issues with large enterprise applications.
Even though the thought of ruining software can be scary, every programmer deals with a merge conflict during their career. The best way to deal with a merge conflict is to face it head on and rely on teammates for support!
Even though merge conflicts are normal in Git, it is also normal for programmers to want to do everything they can to avoid them. Here are some tips on how to avoid a merge conflict:
git merge --no-commit --no-ff <branch>. The
--no-ffsyntax tells Git to run the merge without committing the result to the repository.
git stashcommand, the uncommitted work is saved in the stash, and the repository is returned to the state at the last commit. If the programmer wants to retrieve stashed work later, they can do so with the command
git stash pop.
If a programmer is on the branch
test and wants to merge a branch called
main, what steps should they take?