40

I've got a website that has a git repo. I cloned the repo so that I could develop in one directory and then push to the repo, and then pull in the live/prod directory (would be interested in suggestions for a better way to do this if there is one, but that's outside the scope of this question).

I did the following in the live directory to push all my latest changes:

git add .
git commit -a // added a message
git push

I then did the following in the dev directory:

git clone git@bitbucket.org:user/repo.git

I then opened two files, prod/root/test.php and dev/root/test.php, and they looked identical. However, when I did the following diff command, it outputted the entire file:

diff prod/root/test.php dev/root/test.php

I am so confused as to why diff would output the entire file if they're identical... I also tried googling this and can't find anyone else with this problem. Maybe it's a line endings issue or a character encoding issue where they look the same but they are actually different and git/bitbucket converts it when you push to their repo? That's the only thing I can think of... Either that or I'm missing something really obvious.

Here's the output:

1,3c1,3
< <?
< echo '<p>Hello world!</p>';
< ?>
---
> <?
> echo '<p>Hello world!</p>';
> ?>
Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81

6 Answers6

48

This seems like a whitespace issue, in order to avoid them in the future, you can setup Git to normalize them.

Windows and UNIX system don't use same line-ending, to prevent conflict from happening based on these, you should setup you git config this way:

  • Windows : git config --global core.autocrlf true
  • Unix : git config --global core.autocrlf input

Next, to make sure we only commit with ideal whitespace rules, you can set this config option:

git config --global core.whitespace trailing-space,space-before-tab,indent-with-non-tab
Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
20

Most likely it's line termination. Try git diff --ignore-space-at-eol. And for plain (not git) diff it's diff -b.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
2

That usually means the line endings are different. Most diffin programs allow you to ignore differences in line endings. Does yours allow you to do so?

Patrick
  • 13,872
  • 5
  • 35
  • 53
1

I was just having this issue in a JetBrains IDE and it was because when I saved the file, the IDE was changing the line endings. I just needed to go to the bottom-right corner and switch the line endings back to their original setting to make the diff look right.

enter image description here

Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95
0

Not the solution, will help this to you.

git diff --no-ext-diff --ignore-space-change -- <path> -> You can view only the code changes in git bash.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Yuresh Karunanayake
  • 519
  • 1
  • 4
  • 10
0

Expanding on @SimonBoudrias's excellent answer, git config --global core.autocrlf true ask's git to convert line endings from LF to CRLF on checkout. Very useful for developers working on Windows. This command will add the following to %HOMEPATH%\.gitconfig:

[core]
    autocrlf = true

Running the command from Git Bash is best, but the above lines can be manually added into .gitconfig if you find that easier.

AlainD
  • 5,413
  • 6
  • 45
  • 99