Search code examples
linuxgitgitignore

How to add multiple files into a .gitignore file in the proper way?


Here, I'm using a linux terminal. Assume, I have created a new directory

mkdir project_new
cd project_new
git init

And I've created two new files

touch a.txt b.txt

Now I'm staging and committing

git add .
git commit -m 'Add two new files a.txt b.txt'

Creating a .gitignore and made it as a hidden file

touch .gitignore
echo .DS_Store >.gitignore

Now can you help me out in the next steps

how to add both 'a.txt' & 'b.txt' inside the .gitignore to make them as untracked files?


Solution

  • Since you have already committed your files. They fall into the category of tracked files and are rightfully being tracked by git. You need to remove the files first from the index.

    git rm --cached <path-to-files>

    Since your a.txt and b.txt are sitting under the root of project, you just need to run,

    git rm --cached a.txt b.txt

    Add the files to .gitignore as follows,

    /a.txt
    /b.txt
    

    Commit your changes and then going forward changes to a.txt and b.txt will be ignored by git.