Common
Imal Perera  

Git Still Tracks Files in the .gitignore File

Spread the love

This is a very common problem for the git beginners, at some stage of your development you will notice even though you have added files folders into .gitignore the are still keep tracked by git or you will come across a situation where you need to untrack files which are previously tracked ( previously pushed to the repo )

Here is how you do that 

lets say we have a folder called test which needed to be untracked from git 

  1. you need to first add that folder to the .gitignore so if you don’t have a .gitignore file create a file named .gitignore within your root of your folder structure
  2. you now need to add the files or folder to that file to do that specify the file or folder path relative to the .gitignore file ( see sample below )
    test/
    .idea/
    src/resource/image.jpg
    
  3. now you just said do not track these files, but if it has already tracking those files first you will have to remove them from the index, lets do that by running this command
    git rm -r --cached .
    

    for my ease without removing files one by one i’ll do  . remove all from the index 

  4. now add them back ( at this point git will only add the files/ folders that are not listed in .gitignore )
    git add .
    
  5. now commit and push you are done

In rare cases

in very rare cases even if you do all above you will still see that files that are in .gitignore is being tracked and you are confused why,  in this case it is mostly an encoding issue of the .gitignore file git expect it to be UTF-8 but your files encoding is different.

  • if you have got Notepad++ you can easily open the file and change the encoding to UTF using the encoding menu
  • or if you are a hard core terminal guy you can use the below shell commands to check the file encoding and change it to UTF-8
    1. check file encoding

    file -i .gitignore 
    

    2. change encoding

    iconv -t UTF-8 .gitignore
    
  • You are done make sure to add the .gitignore file and run the first set of steps again

 

Leave A Comment