FIX: Files in my Git gitignore are still available in the repository

Why are the files still available in my Azure DevOps repository that I added to my gitignore?

Most times this occurs, because files that have already been added to a repository and committed will not be ignored by gitignore rules.

PS: For most PowerShellers among us who are not used to the difference between a small and a large letter in programming. It can also be because you have a capital letter somewhere that should be a small letter!


Are you sure you didn’t use a capital letter where a lowercase letter should be, or vice versa?

Let’s verify if you haven’t made a mistake with small and large letters.
For the PowerShellers among us, we are not used to a small and a large letter impacting your changes that much.
We love cascading everything, but files with the names like this:

  • JustAName.txt
  • jUSTanAME.TXT

Are two different files in git.


Removing files from Azure DevOps repository despite being added to gitignore: A step-by-step solution using git rm --cached command

A possible solution to this problem is to use the git rm --cached command to remove the file from the repository’s index and stop tracking it, while still keeping the file in your local file system.

To mimic the below I created a file called AFileIWantToIgnore.txt and placed it in the main branch. I then added the file to my .gitignore.

Here is a step-by-step explanation of how to do this:

  • Check the status of your repository by running the command git status --ignored. This will show you a list of all the files that are currently untracked by git because of your .gitignore.
git status --ignored

On branch master
Your branch is up to date with 'origin/master'.

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)
        .vscode/
        Ignore/

nothing to commit, working tree clean

As you can see, the file name is not listed, although it is actually listed in the main branch:

FIX: Files in my Git .gitignore are still available in the repository
FIX: Files in my Git .gitignore are still available in the repository
  • Let’s check via the command line to make sure that the filename is actually in the .gitignore.
    Check the contents of your .gitignore file by running the command cat .gitignore.
cat .gitignore

Ignore
workspace.code-workspace
.vscode
AFileIWantToIgnore.txt

Make sure that the file or folder you want to ignore is listed in this file. If it’s not, add it to the file and save it.

Remove the file from the repository’s index by running the command git rm --cached [file name]. This will stop git from tracking the file, but it will still exist in your local file system.

git rm --cached AFileIWantToIgnore.txt

rm 'AFileIWantToIgnore.txt'

If you want to stop tracking a directory and all its content, you can use the command git rm -r --cached [directory name] instead of git rm --cached [file name].

  • Check the status of your repository by running the command git status.
    You should now see that the file is deleted from git.
git status

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    AFileIWantToIgnore.txt
  • Commit the changes by running the command git commit -m "COMMIT MESSAGE". This will save the changes to your local repository and upload them to your remote repository.
git commit -m "Removed AFileIWantToIgnore.txt from repository"

[master fb97316] Removed AFileIWantToIgnore.txt from repository
 1 file changed, 1 deletion(-)
 delete mode 100644 AFileIWantToIgnore.txt

And do not forget to push this to your remote repository as well:

git push

Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
...
  • Verify that the file is no longer present in your remote repository by navigating to the repository in the web interface.
    The file should no longer be present in the repository.

Hopefully, these steps will get your files back into the .gitignore without seeing them in your remote branch.
Have you found another solution? Then I’d love to hear it in the comments

Published by

Bas Wijdenes

My name is Bas Wijdenes and I work as a PowerShell DevOps Engineer. In my spare time I write about interesting stuff that I encounter during my work.

Leave a Reply

Your email address will not be published. Required fields are marked *