Test Non-Conflicted Files
In the previous steps, we identified and examined a file with a merge conflict (message.txt
). However, during a merge, there might also be files that were changed in both branches but without conflicts. Git automatically merges these files.
In this step, we will create a new file in one of the branches and see how Git handles it during the merge process. This will help us understand that conflicts only occur when changes overlap in the same file.
Make sure you are still in the ~/project/my-time-machine
directory and on the master
branch (where the merge conflict occurred).
Let's create a new file called notes.txt
in the master
branch:
echo "Important notes for the project." > notes.txt
git add notes.txt
git commit -m "Add project notes"
Now, let's switch back to the feature/greeting
branch:
git checkout feature/greeting
In this branch, the notes.txt
file does not exist yet. Let's create a different file here, for example, todo.txt
:
echo "Things to do: finish the lab." > todo.txt
git add todo.txt
git commit -m "Add a todo list"
Now, let's switch back to the master
branch and attempt the merge again. Even though we have already resolved the conflict in message.txt
, the merge process needs to be completed.
git checkout master
git merge feature/greeting
This time, since we have already resolved the conflict in message.txt
and added it to the staging area (though we didn't explicitly show that step after editing, Git often stages the file after manual conflict resolution), Git should be able to complete the merge. You might see output indicating that the merge is complete.
Let's check the status again:
git status
The output should now show that you are "On branch master" and that the working tree is clean, meaning there are no pending changes or unmerged paths.
On branch master
nothing to commit, working tree clean
Now, let's check if the files from both branches are present in the master
branch:
ls
You should see both message.txt
, notes.txt
(from the master
branch), and todo.txt
(from the feature/greeting
branch) listed.
message.txt notes.txt todo.txt
This demonstrates that Git successfully merged the changes from feature/greeting
, including the new todo.txt
file, without any conflicts because todo.txt
did not exist in the master
branch. Conflicts only arise when the same file has overlapping changes in the branches being merged.
Understanding how Git handles both conflicted and non-conflicted files during a merge is essential for managing your project's history effectively.