git - CRLF issue in windows + linux dual boot - linux

I'm going to answer my question with the fix what solved my problem.
Note for downvoters: I understand that the root cause is discussed in various other threads (that is how I solved my problem). This post is more about how having a dual boot system can lead you to this issue. So no, this question/answer is not a duplicate, but a particular instance of a general class of problems, adding more cases to SO's repository on this issue.
At home: I code in Linux. LF used as line ending
At the office: I code in windows. CRLF used as line endings.
By default, git's autocrlf feature (https://stackoverflow.com/a/20653073/2715083) keeps things happy.
However, if you run a dual boot system with Linux and Windows, you can mess yourself up in the following way:
git pull some files you worked on in a linux environment in a windows environment, in a location that can be accessed from the dual-booted linux environment. This modifies the files to contain CRLF endings.
Then when you open up the file in linux, where the default is only LF, git diff will say entire file is modified, because each LF got changed to CRLF at every single line. (I was warned by Atom, which has this diff count inbuilt)

you are talking about text=auto part? I wasn't sure If I need to include that in a new .gitattributes file or not, because when I did, ATOM still showed the files as modified.
Yes, it does.
To force Git to apply .gitattributes directives, see "Dealing with line endings".
I would first make sure core.autocrlf is set to false.
git config --global core.autocrlf false
Then:
git add . -u
git commit -m "Saving files before refreshing line endings"
rm .git/index
git reset
git status
git add -u
git add .gitattributes
git commit -m "Normalize all the line endings"
You can also use, to force the index re-normalization:
git rm --cached -r .
git reset --hard
See "Force LF eol in git repo and working copy"
* text=auto eol=lf

The FIX
Delete/move to another location the problem files/folders
do git checkout <hash> <your/files/location>
where <hash> is for the last good commit, and your/files/location being the location of the files you want to cure from the CRLF problem. This will basically restore the older versions from your local .git repository.
Worked for me.
If you know something i missed, or explained incorrectly, do let me know

Related

There are some changes opening in the linux of a git repo. But the repo is latest in windows

My pc is a dual system with Linux Mint and Windows 11.
The git repo always has some changes in Linux.
Every time I need to git stash and git stash drop.
I don't know how to descript this situation with google.
Help pls. Thanks.
The problem is that you have different line ending settings between Linux and Windows. What you're seeing in Linux is that your files have carriage returns on the end of them, which is the Windows line ending.
The easiest way to solve this is to add this to .gitattributes:
* text=auto
Then, on an otherwise empty working tree, add that file, run git add --renormalize ., and commit. That will cause the files to be renormalized as having LF line endings in the index.
If you're going to be working across OSes, then you'd also do well to add eol=lf to the end of that line in .gitattributes so that files are always handled with LF endings. You'll also need to configure your tools on Windows to emit LF endings instead of CRLF endings. That will prevent line ending differences in the working tree across systems.

Added file to .gitignore (to not ignore) but git status doesn't see it

For my project I'm using by .gitignore as a 'don't ignore' file by telling it to ignore everything in the first few lines then giving it a list of file to include with the ! character. I added a file to my list and copied the file to my working directory and did a "git status" but it's not showing my new file as something to be added. Even "git status -u" doesn't show anything but says my working directory is clean.
This is on a Raspberry Pi with Debian Wheezy and git version 1.7.10.4, which should be the latest version for Wheezy. I can do a build of git from the source if that is the fix.
Just remove it from .gitignore and you will see it, there is no point of putting it there if you want it to be committed.

Git ignore and changing the history (on Windows)

I've already read several posts about this here (like Git ignore & changing the past, How to remove files that are listed in the .gitignore but still on the repository?, and Applying .gitignore to committed files), but they have several problems:
Commands that only work on Linux.
Incomplete commands (like the first post I've linked to).
Only for one file.
I have pretty much no experience with Git so I was hoping for some help here.
What I'm basically trying to do is rescue one of my projects history. It's currently Hg and I converted it to Git with Hg-Git (all very easy) and it includes the history (great!). However, I also added a .gitignore file and added several new files & folders that I want completely gone from the history (like the bin and obj folders, but also files from ReSharper). So I'm looking for a way to apply the .gitignore file to all of my history. The commands should work on Windows as I have no intention of installing Linux for this.
No need to add the .gitignore in the history (there is no added value to do it), just add it for your future commits.
For the remove of files and directories in your history, use bfg-repo-cleaner which is fast, easy and works very well on Windows (done in scala).
It will do the job for you!
This is working for me:
Install hg-git.
cd HgFolder
hg bookmark -r default master
mkdir ../GitFolder
cd ../GitFolder
git init --bare
cd ../HgFolder
hg push ../GitFolder
Move all files from GitFolder to a '.git' folder (in this GitFolder) and set this folder to hidden (not the subfolders and files).
cd ../GitFolder
git init
git remote add origin https://url.git
Copy all current content (including .gitignore) to GitFolder.
git add .
git commit -m "Added existing content and .gitignore".
git filter-branch --index-filter "git rm --cache d -r --ignore-unmatch 'LINES' 'FROM' 'GITIGNORE'" --prune-empty --tag-name-filter cat -- --all
git rm -r --cached .
git add .
git gc --prune=now --aggressive
git push origin master --force
There is probably an easier way to do this and it might not be perfect but this had the result I wanted.

Git problems with line endings differences between linux and windows

I'm working on a project on Windows and some project files seems to be changed locally but I didn't. Was ignoring them but now I cannot push because are unstaged files. When I look into this files differences, there isn't, only this line ending thing (CRLF, LF).
Tried to revert this files, reset --hard too.
Why can't I reset this files to repository HEAD?
Tried to do this too:
git config --global core.autocrlf false
If I do git reset --hard and git status later I get message that have many files with changes not staged.
Sorry but I don't know git and this linux/windows line ending differences very well.
Maybe someone else working on the project introduced bad line endings. two things 2 do:
fix bad line endings already committed.
align the team about the line endings policy.
See: Dealing with line endings.

What is the best git config set up when you are using Linux and Windows?

I am a bit confused with core.eol, core.autocrlf, core.safecrlf for git config.
http://git-scm.com/docs/git-config
I am using Ubuntu and Widows.
I had ^M, and other issues before.
Could anyone suggest the best git config setups for this problem?
Thanks in advance.
As detailled in Git on Windows (msysgit) - Unix or DOS line termination, I would use:
git config --system core.autocrlf false
That would avoid any automatic eol transformation (See "Git on Windows: What do the crlf settings mean?" for the exact meaning of this option value).
You can leave it to true, as explained in git replacing LF with CRLF, but I prefer setting core.eol + some gitattribute files in order to fine tune some of the files I want eol changes on.
See this answer for more details.

Resources