Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git showing entire file is modified instead of showing modified small part of code

Tags:

git

I searched for an answer to this in Google but came up with nothing.

In Windows, I configured git via eclipse. When I try to commit a modified file to my branch via Team --> commit, it shows only the modified lines as changed, but whenever my lead tries to merge the change to the branch it shows the whole file as changed

Can anyone suggest to me what is going to wrong?

Thanks in advance.

like image 527
Anil Meenugu Avatar asked Jun 10 '15 09:06

Anil Meenugu


People also ask

Why git is showing all the files as modified?

As you edit files, Git sees them as modified, because you've changed them since your last commit. As you work, you selectively stage these modified files and then commit all those staged changes, and the cycle repeats.

How do I see exact changes in git?

Find what file changed in a commit To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

How does git know a file is modified?

For every tracked file, Git records information such as its size, creation time and last modification time in a file known as the index. To determine whether a file has changed, Git compares its current stats with those cached in the index. If they match, then Git can skip reading the file again.

How do I update a modified file in git?

Enter git add --all at the command line prompt in your local project directory to add the files or changes to the repository. Enter git status to see the changes to be committed. Enter git commit -m '<commit_message>' at the command line to commit new files/changes to the local repository.


2 Answers

This is most likely a line ending issue. Windows ends lines with CRLF, whereas other operating systems end them with LF. When files in a repository are changed by people using both types of operating systems, the fact that their respective editors change every line of a file to their particular flavor of line endings causes the behavior you are seeing.

Even if everyone on the team is using Windows, Git's attempts to "fix" this issue can also cause this issue to come up. This is because one way to tell Git to fix this issue is to set core.autocrlf to true. Doing this will cause Git to convert every CRLF to a LF on commit, and every LF to a CRLF on checkout. The problem is when different developers of the project have their core.autocrlf set to different values. So let's say you have core.autocrlf set to true, and another developer has it set to false. When you commit a file, Git will do the line ending conversion, and the repo will contain LF line endings. When the other developer checks out the file, no conversion will be done, and therefore every one of the lines in their local file (which have CRLF) will be different than in the repository.

The solution to this is to not rely on core.autocrlf to determine whether any of this normalization is done, since that can vary by machine. Instead, you want to have a .gitattributes file in your repo's root directory that determines whether normalization is done. What you want in there depends on if your whole team is using Windows or not.

If your whole team uses Windows: In this case, I would recommend disabling line ending normalization, as there is no need for it. To do this, put * -text in your .gitattributes file.

If your team uses other OSs in addition to Windows: In this case you do want line endings to be normalized. To make it consistent for everyone, put * text=auto in your .gitattributes file.

like image 159
David Deutsch Avatar answered Nov 15 '22 08:11

David Deutsch


Following has fixed the problem for me in Eclipse

  1. Window -> Preferences -> Team -> Git -> Configuration
  2. Click Add Entry button
  3. Key = core.autocrlf, Value = false
  4. Restart eclipse
  5. Reset the problematic file to previous version
  6. Merge the changes to file again
  7. Commit.
like image 33
Usef Avatar answered Nov 15 '22 07:11

Usef