Generating merge conflicts and learning to solve them
Navigate to the location where you want to create your project:
cd path/to/your/folderIn that folder, create a file called notes.txt and write a simple list in it, for example:
GROCERY LIST
banana
mango
papaya
cereal
Start version control in the current folder:
git initStage and commit your initial list:
git add .
git commit -m "initial commit with grocery list"Create and switch to a new branch:
git checkout -b testIn the test branch, edit notes.txt. For example, delete an item or rename one:
- Rename
cerealtogranola - Or remove
banana
git add .
git commit -m "update branch test"git checkout mainNow make a different change in notes.txt, ideally to the same lines (e.g., rename papaya to pineapple).
git add .
git commit -m "update on main branch"git merge test💥 You’ll likely see a merge conflict. Open notes.txt and resolve the conflict manually.
Conflict markers will look like this:
<<<<<<< HEAD
pineapple
=======
papaya
>>>>>>> test
Decide whether to keep:
- The change from
main(HEAD) - The change from
test(incoming) - Or combine both
Then save the file.
After resolving the conflict, stage and commit the final version:
git add .
git commit -m "resolved merge conflict"You've now created and resolved a Git merge conflict.
Great job — you're one step closer to being a Git pro! 🚀