Git

How to Make git-diff Ignore ^M?

When working with Git on different operating systems, issues can arise with file separators. When users perform the “git diff” operation, some lines contain “^M” as a file separator. This is because the “git diff” consider the entire file as a single line. To avoid this issue, developers need to configure Git to convert the file separator and ignore the “^M” automatically.

This article will demonstrate the procedure to make “git-diff” ignore “^M”.

How to Make “git-diff” Ignore ^M?

To make git-diff ignore “^M”, follow the provided instructions:

    • Redirect to the local Git repository.
    • Update the auto CRLF value using the “git config –global core.autocrlf true” command.
    • Remove the desired file from the index through the “git rm –cached -r” command.
    • Add deleted files to the Index again.
    • Commit added changes.
    • Verify changes.

Step 1: Move to Desired Repository

First, execute the given-provided command and navigate to the local Git directory:

$ cd "C:\Git\Repos1

 
Step 2: View Commit Changes

Next, run the “git diff” command along with the desired commit id to view its changes:

$ git diff 3974733

 
It can be observed that the few lines of the file contain “^M” as a new line separator:


To make git-diff ignore “^M” (line separator), follow the below-provided steps.

Step 3: View Auto CRLF Default Value

Execute the following command to view the default value of Auto CRLF:

$ git config --global core.autocrlf

 
According to the below-provided screenshot, the Auto CRLF’s default value is “false”:


Step 4: Update Auto CRLF Default Value

Now, update the Auto CRLF value to “true”:

$ git config --global core.autocrlf true

 

Then, verify the new setting by running the below-provided command:

$ git config --global core.autocrlf

 

Step 5: Remove File From Index

Then, remove the desired file from the Git staging area. Here, the “–cached” option removes a file from the Git repository:

$ git rm --cached -r Demo_File.txt

 
It can be seen that the file has been removed from the Git Index:


Step 6: Add Deleted Files to Index

Now, run the below-listed command to add the deleted files to the index again:

$ git diff --cached --name-only -z | xargs -0 git add

 

Step 7: Commit Changes

After that, commit newly added changes through the “git commit” command:

$ git commit -m "Fix CRLF problem"

 

Step 8: Verification

To ensure whether the git-diff is ignoring the “^M” or not, run the “git diff” command again:

$ git diff 3974733

 
Here, it can be observed that the lines of files do not contain “^M” anymore:


We have explained the method to make git-diff ignore ^M.

Conclusion

To make git-diff ignore ^M, first, redirect to the local Git repository. Then, execute the “git config –global core.autocrlf true” command to update the auto CRLF value to “true”. Next, remove the file from the index and again add the file to the index. Finally, commit the added changes. This article demonstrated the procedure to make “git-diff” ignore “^M”.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.