A Git merge conflict happens when two branches edit the same lines of a file and Git throws up its hands. It cannot guess which version you want, so it stops and asks for human judgment. This is not dramatic. It is the repo being careful. You will see conflicts when you run git merge branch-name
or when git pull
brings in remote work that overlaps your edits.
Run git status
to get a tidy list of files that need attention. Conflicted files are marked as both modified and unmerged. Open any one of those files and look for the classic markers that scream for your intervention.
<<<<<<< HEAD
your changes
=======
the other branch changes
>>>>>>> branch-name
Edit between the markers to produce the content you actually want in the final file. Remove all the marker lines, save, then stage and commit the result. The commands you will use most are below.
git add path/to/file
git commit
git status
to see conflicted files.git add
and git commit
.git merge --abort
to return to the pre merge state.git mergetool
or a GUI client for side by side views and less soul crushing choices.Common causes are overlapping edits in the same lines long lived branches and poor synchronization. Big refactors and formatting churn make conflicts much nastier. If everyone rewrites the same lines at once expect problems.
Prevention is mostly about being boring and predictable. Keep changes small and focused. Pull from main often and rebase local work onto the latest main before opening a pull request. Communicate when you plan a large refactor. If you rebase small commits instead of merging big divergent branches you lower the chance of two developers clobbering each other.
git fetch
then rebase your feature branch on top of the latest mainRemember that a merge conflict is not an error. It is a polite request from the repository for human taste and intent. With a few commands and a little judgment you will resolve conflicts and get back to writing code that hopefully does not start another argument with Git.
Commands cheat sheet
git merge branch-name
git pull
git status
git add path/to/file
git commit
git merge --abort
git mergetool
git fetch
git rebase origin/main
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.