When working with a larger team, it's possible to have multiple merge conflicts; both multiple conflicts in a single file, and conflicts in multiple files. When working on large projects, with many files, it's common for a single branch to alter dozens of files. When this happens, you may face merge conflicts that are tricky to resolve.
Although we won't be using them here, this is where git's graphical merge tools can be useful. To use them, you'll need to run the git mergetoolcommand. You'll also generally want to specify the --tool
option flag to specify which graphical tool to use. You can see a full list of available tools on your system by running git mergetool --tool-help
.
A graphical merge tool will show you the branches side by side, and highlight differences visually, like this:
In this particular tool, the HEAD
branch is on the right, and called the REMOTE
branch, the branch we're merging is on the left and called the LOCAL
branch, and the final version is in the center. We need to edit the center version to get the result we want, then save it.
In the absence of a graphical tool, it's still possible to sort through multiple merge conflicts, it's just a bit more work. All of the merge conflict markup must be removed before committing if there are multiple conflicts.
Instructions
- Switch into the
/home/dq/chatbot
repo. - Switch to the
master
branch. - Create a branch that inserts print statements into
bot.py
.- Create a branch called
feature/more-printing
in the repochatbot
. - Switch to the branch
feature/more-printing
. - Edit
bot.py
and add multiple lines that print whatever text you want. - Commit your changes.
- Create a branch called
- Create another branch that inserts print statements into
bot.py
.- Switch to the master branch.
- Create a branch called
feature/more-printing-2
in the repochatbot
. - Switch to the branch
feature/more-printing-2
. - Edit
bot.py
and add different print statements to the same lines you edited infeature/more-printing
. - Commit your changes.
- Create a merge conflict.
- Merge
feature/more-printing
intomaster
. - Try merging
feature/more-printing2
into master. - At this point, you should trigger multiple conflicts.
- Merge
- Resolve the merge conflict.
- Resolve the conflicts by editing
bot.py
and keeping whatever lines you want. - Commit
bot.py
with the messageResolved multiple conflicts
.
- Resolve the conflicts by editing
- Push the
master
branch ofchatbot
to the remote repo
~$ cd ~
~$ cd chatbot
~$ git checkout -b feature/more-printing
~$ printf "\nmessages=['I am great', 'You are great', 'I need more outside time']\nimport random\nmsg=random.choice(messages)\nprint(msg)" >> bot.py
~$ git add .
~$ git commit -m "Add more printing"
~$ git checkout master
~$ git checkout -b feature/more-printing-2
~$ printf "\nimport random\nd1=random.randint(1,6)\nd2=random.randint(1,6)\nprint('D1: {0} D2: {1}'.format(d1, d2))" >> bot.py ~$ git add .
~$ git commit -m "Add even more printing"
~$ git checkout master
~$ git merge feature/more-printing
~$ git merge feature/more-printing-2
~$ printf "\nimport random\nd1=random.randint(1,6)\nd2=random.randint(1,6)\nprint('D1: {0} D2: {1}'.format(d1, d2))" > bot.py
~$ git add .
~$ git commit -m "Resolved multiple conflicts"
~$ git push origin master