How to remove files from repository listed in .gitignore without changing whitespace

Viewed 623

I have read about how to remove files that are in the ignore file from a repository using git commands here: Remove directory from remote repository after adding them to .gitignore.

I am using Visual studio 2019 with a repository in Azure DevOps. I have used these steps:

  1. From the Git Explorer Menu, "Open in Command Prompt"
  2. git rm -r --cached . This tells git to stop tracking everything and now I have all my files showing as deleted in "Staged Changes" and a smaller number of files showing as added in "Changes". This looks good. I assume ignored files are not added back again, so when I commit those additions all the files that should have been ignored in the first place will be deleted on the server.
  3. Stage. Now it looks like there's a problem. The added files are showing as modified.
  4. Push to see what they look like when I view the changes in the browser: "Only showing the first 999 changes. Click here to load more". And every file I can see has a warning: "The file differs only in whitespace"

I'm guessing it's about line endings but it makes it nearly impossible to review the changes. Is there a way to stop the whitespace changes occurring when I use this process? Or to reverse the whitespace changes? Or tell git to only check in the deletes?

TLDR Answer (from VonC):

git rm -r --cached .
git config --global core.autocrlf false
git add --renormalize .
git add .
git commit
2 Answers

Stage. Now it looks like there's a problem. The added files are showing as modified.

Repeat the same process, but before step 3 (staging), add:

git config --global core.autocrlf false

That will make sure there is automatic eol (end of line) transformation.
See if a git add . (or git add --renormalize . with Git 2.16+) followed by a git status still show everything as modified.

The OP Colin confirms in the comments the following working:

git config --global core.autocrlf false
git add --renormalize .
git add .

I think you are making this way more complicated than necessary. To remove files from git, you just do the following:

git rm <files>
git add .
git commit

Replace <files> with the names of the files you want to remove. You can use standard glob syntax to match more than one file.

This will remove files that have previously been committed. There is no reason to use --cached or anything with autocrlf. These just complicate the issue.

Related