git : nothing changed but mark every line "changed" - eclipse

IDE : Eclipse & VS code
Git tool : source tree, VS code
Action : Nothing changed in my code. But when pressing ctrl+s, it's able to save and suorceTree will show every line has been edited. The only change I found (show by sourceTree) :
form:
import org.apache.poi.ss.usermodel.Cell;␍import org.apache.poi.ss.usermodel.CellType;
to this:
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType;
I'm quite sure that I'm not using any "format code on save" settings, is it a git bug in this version?
trying : ctrl + s in vs code, eclipse, wordPad
excepting : to edit a line (not changing the whold file when saving) and push to git branch.

By default Git uses LF (Linux) Line Endings, Windows on the other hand pushes CRLF Line Endings.
Git tries makes the assumption that if you are using Windows, you want CRLF, this is good for normal users because programs generally come pre-configured to use CRLF.
If you are using Windows and you are getting false changes reports, it is likely that Git is ignoring the line endings, so when you clone or pull code, it will be in LF. Then when you save your progress in your CRLF-configured editors, each line ending is being changed back to CRLF, modifying the whole file.
This behavior can be altered during the Windows installation of Git, pay close attention to it next time you install Git.
Solution 1: Configure Git
You can configure Git to *replace LF to CRLF when checking out (cloning, pulling...) so you can work in CRLF, and then replace CRLF back to LF when you commit so that you don't commit whole practically unchanged files. The option you want is core.autocrlf, you can do it in your terminal:
git config --global core.autocrlf true
The --global parameter makes this change for your entire user (which you probably want to)
Solution 2: Start using LF
You could configure your Text Editors and IDEs to use LF by default, that way when you clone or pull in LF, you will keep working and saving your work using LF, retaining Git's default line endings.
You can usually find this option refeered to as EOL (End Of Line).

Related

End-of-line characters flip flop in Eclipse settings

Eclipse stores some project settings in the file .settings/org.eclipse.wst.common.project.facet.core.xml changing end-of-line characters and this is creating unnecessary commits and polluting the repository. Here's the file contents:
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project> (this EOL flip-flops)
<installed facet="jst.utility" version="1.0"/> (this EOL flip-flops)
<installed facet="java" version="11"/>
</faceted-project>
Steps to reproduce:
Developer #1 opens the project in Windows, and Eclipse changes the end-of-line chars to \r\n; then this developer commits to the repository.
Then, developer #2 opens the project in Linux, and Eclipse changes the end-of-line chars to \n; then this developer commits to the repository.
Developer #1 pulls the changes in Windows, and EOL flip-flops again. Rinse and repeat.
This is happening with four settings files (so far), so they are committed to the git repository pretty much on every single commit; and this is polluting the repository.
Is there a way of telling Eclipse to use either \r\n or \n for good? Workspace and project settings don't seem to affect "settings files", but only "source code" files.
There are two possible approaches here. One is not to store editor configuration in the repository, which is generally the recommended approach. Different people use different editors on different systems, and anything other than a generic editor configuration can contain settings that aren't applicable to all systems.
The other way, if you want to store them anyway, is to use a .gitattributes file in your repository, and then add a line like the following:
.settings/*.xml text
Then run git add --renormalize .. That will force the line endings in the repository to LF and tell Git, by default, to check out the native line endings. Of course, the user can configure Git to do something different if it's appropriate to their system.
Then when the user modifies the file, the endings will be appropriate for their platform and it won't cause Git to show things that are modified. If the user does attempt to commit it, the file will be normalized, and no change will occur.
You can also add * text=auto to the .gitattributes file, which will let Git determine which files should have their line endings normalized based on whether they're detected as binary or text. You can see more about how to adjust these settings in the gitattributes(5) man page.

Eclipse eGit on Linux screwing line endings

Because of the changes in line endings, commits looks a lot bigger than they should be.
I have egit 4.6.1.201703071140
Before using egit, the command
git ls-files --eol
shows
i/mixed w/mixed attr/ somefile
then, after commit in Eclipse via git staging view, it is:
i/lf w/mixed attr/ somefile
Another example:
i/crlf w/crlf attr/ somefile2
becomes:
i/lf w/crlf attr/ somefile2
You can see egit is changing the first column to i/lf.
In GitHub you can use ?w=1 to dampen the noise, but how do I fix the problem itself?
git config
core.autocrlf=input
core.repositoryformatversion=0
core.filemode=true
core.logallrefupdates=true
remote.origin.url=https://github.com/plutext/docx4j.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.master.rebase=false
According to https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration:
You can tell Git to convert CRLF to LF on commit but not the other way around by setting core.autocrlf to input.
You probably want to use false instead.

diff after EGit commit changing whole file, but ok in command line

I don't understand why a commit in EGit is changing the whole file.
Yes, I'm on Windows, and I have core.autocrlf=true set globally in /etc/gitconfig.
Here's what I don't understand:
the working copy starts off with DOS line endings both before and after edit
When I edit the file in Eclipse and look at git diff through the command line, I only see the lines that I changed in the diff.
If I git add and git commit through the command line as well, the git diff HEAD^ shows only the lines I changed.
When I do the commit through Eclipse/EGit instead, git diff HEAD^ shows the entire file as changed, as if the Windows line endings were just added -- when my working copy already had DOS newlines before I touched it.
Any idea why the different behavior in EGit vs. git command line? Where else could Eclipse/EGit be mangling line endings?

not showing correct updates while commit with github for windows

I have installed the github for windows http://windows.github.com/.
Also already clone a repository into my local drive. I created a branch, and make some update on one file, e.g: index.html. When I commit with github for windows. Github seems to treat it as whole file update (whole line becomes red), instead of showing the modification which I've already made. But this will not happen while I commit with command line.
Does anyone seems ever to have same problem with me? Please tell me what should I do to make the git didn't treat my files as a whole update?
Thanks.
When you write files in Windows, they have a CRLF line endings. Lines with \n and \r\n line endings are different for Git. Please check your editor's and Github for Windows preferences. They can convert CRLF<->LF automatically.
Based on Rustam Safin about the CRLF line endings, recently I found this solution was helpful.
https://help.github.com/articles/dealing-with-line-endings
I have tried it, and it's resolve my problem.

Git shows files changed, but EGit does not

Git noob here. Having a hard time figuring out why git shows some files changed, while Eclipse EGit does not.
When I use EGit within Eclipse and view a project, it shows no files changed. There is no little caret next to each file. When I use Git for Windows, or go to the command line and type "git status", it shows that all the files have been modified. When I type "git diff" it shows two different versions of a file, first red, then green, and there appears to be some whitespace differences, but I can't be sure, and I can't figure out how the whitespace changed in every file in the project. (Something here doesn't add up.) "git diff -w" returns nothing. "git config --global apply.whitespace nowarn" does nothing.
I might be having a basic conceptual problem with git.
In any case, why do EGit and the git command line show different results?
EGit understands the notion of Derived resources (e.g. used for generated .class files in JDT). In other words, files in derived resources are not added to version control by default in EGit. However, the command line git client does not know these markers, but relies on .gitignore files to avoid checking in generated files.
To check whether your problematic files are derived, open the file properties dialog (right click on the file in the explorer, and select Properties...), and on the Resources page check for the Derived checkbox (it should be around the middle of the dialog).
It turns out that git is playing games with newlines. It's inexplicable. Bottom line: EGit and the git command line cannot both be used on a Windows box. To get consistency, you have to use one or the other.