Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 888
Colin Avatar asked Aug 03 '21 19:08

Colin


People also ask

How do you remove files that are listed in the .gitignore but still on the repository?

As the files in . gitignore are not being tracked, you can use the git clean command to recursively remove files that are not under version control. Use git clean -xdn to perform a dry run and see what will be removed. Then use git clean -xdf to execute it.

Does git clean remove ignored files?

The git clean command also allows removing ignored files and directories.

How do I remove files from remote repository?

The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted. Note that by using the “git rm” command, the file will also be deleted from the filesystem.

How to remove files in gitignore from remote repository?

How to remove git files, directories in .gitignore from a remote repository. 1 Step 1: Unstage the files/directory. Mark the files for unstaging using the git rm -r --cached command. Replace buckup/ with your directory or file. 2 Step 2: Make new commit. 3 Step 3: Push changes to remote git repository. 4 Your support is our everlasting motivation,

How do I remove files in Git ignore?

As the files in .gitignore are not being tracked, you can use the git clean command to recursively remove files that are not under version control. Use git clean -xdn to perform a dry run and see what will be removed. Then use git clean -xdf to execute it. Basically, git clean -h or man git-clean(in unix) will give you help.

What happens when you edit the gitignore file?

When you edit the .gitignore file, the rules added only apply to the untracked files. For the files already committed to a remote git repository, you’ll need to unstage them, create a new commit and push to your remote git repository.

How to remove all uncommitted files in a git repository?

1 Be sure to have committed all changes You shouldn’t have any uncommitted files in your working directory. ... 2 Remove everything from the repository To clear your repo, use: git rm -r --cached . ... 3 Add everything again That’s the easy part: git add . 4 Commit and party


1 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 .
like image 187
VonC Avatar answered Oct 28 '22 20:10

VonC