Test Text vs Binary Files
In this step, we will see how Git handles changes in text files versus binary files. This will highlight why Git's diffing capabilities are primarily designed for text.
First, ensure you are in your project directory:
cd ~/project/my-time-machine
We already have our message.txt
(text file) and binary_file
. Let's make another change to message.txt
:
echo "Another line for the future" >> message.txt
Now, let's add both files to the staging area and commit them. First, add the files:
git add message.txt binary_file
Check the status to confirm both files are staged:
git status
You should see both files listed under "Changes to be committed":
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: message.txt
new file: binary_file
Now, let's commit these changes:
git commit -m "Add binary file and update message"
You will see output confirming the commit, including changes to both files:
[master ...] Add binary file and update message
2 files changed, 2 insertions(+)
create mode 100644 binary_file
Now, let's make a small change to the binary_file
. We can append a single byte to it:
echo -n "a" >> binary_file
The -n
flag prevents echo
from adding a newline character.
Check the status again:
git status
Git will show that binary_file
has been modified:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: binary_file
no changes added to commit but untracked files present (use "git add" to track)
Now, let's try to see the difference using git diff
:
git diff
Instead of showing line-by-line changes, Git will likely tell you that the binary file differs:
warning: LF will be replaced by CRLF in binary_file.
The file has no newline at the end of the file.
diff --git a/binary_file b/binary_file
index ... ...
Binary files a/binary_file and b/binary_file differ
This output clearly shows that Git doesn't attempt to show the detailed changes within the binary file. It simply states that the files are different. This is a key difference in how Git handles text versus binary files. For text files, Git can show you exactly which lines were added, removed, or modified. For binary files, it can only tell you that a change occurred.
This step demonstrates why Git's powerful diffing and merging tools are most effective with text-based content, which is common in source code and configuration files.