How to ignore previously committed files and folders
Home » BLOG » Web development » How to ignore previously committed files and folders

How to ignore previously committed files and folders

category:  Web development

Situation I have with CraftCMS3

Recently I am working on a project using CraftCMS 3. I work with another backend developer and we develop on our own branch. When we both make the changes, we will commit the changes to our own branch, and later we merge the changes from two branches into the development branch which will deploy to the staging.

The problem we have with CraftCMS is the project config conflict. It causes to have the duplicate fields, sections, and the image transforms on the staging. For assets, we store on Amazon S3. In CraftCMS 3, the project config file is a YAML file. It stores all the settings of CrafrCMS (custom fields, section, image transforms, assets, and more). The project config is stored in the database as well. So the project config files and the database will be the same. In CraftCMS3, you can rebuild the project config from the database, reapply the changes manually in the project config or reapply everything from the project config files.

The solution we come up with is to ignore the project config files. All settings of CraftCMS will come from the stating site only. Meaning, we, developers have to sync(import/export DB) the database from the staging to our database and rebuild the project config every time we merge the changes to the staging site. It is not practical but it will help us to continue to focus on developing rather than fixing the project config conflict.

Ignore the files or folders that never committed

If you never committed the files or folders before, you can simply add the files and folders into the .gitignore file. In VSCode, the text color of the ignored files and folders will gray out. If not, you have to reopen the VSCode to see the changes.

Below is how to ignore the files and folders if none of them are committed to git before.

git init

Run git init in the terminal if your working directory never had git yet.

Create a .gitignore file in your working directory.

Then add the files or folders you want to ignore in the .gitignore file.

/foo/bar/
requirement.txt

In VSCode, the “foo/bar” folder and files under the bar folder will gray out as well as requirement.txt after you add these files and folders into the .gitignore file.

Ignore the previously committed

You will need this command.

git rm -r --cached <folder>
git rm --cached <file>

“-r” option is recursive. It will use for removing the folder from the remote repository.

By running the command above, the folders and files you want to ignore will be deleted from the remote repository but these ignored files and folders will still store in your working directory(localhost).

Here are the commands I run.

git commit -m "commit all changes I have in my dev local"
git rm -r --cached config/project/
git rm -r --cached web/vendor/
git rm -r --cached storage/
git commit -m "start ignoring config/project, storage,web/vendor"
git push origin <my dev brach>

To explain, I commit the changes that I currently have on my dev local. Then I remove the folders I want to ignore by running “git rm -r –cached <folder>”. This removal won’t affect your working directory (localhost). Next, I commit to my git (local). Finally, I push the commit to the remote repository.

Next, create the .gitgnore file and add the files and folders that you just committed into the .gitignore file. Mine looks like this.

/web/vendor/
/config/project/
/storage/

Then commit the .gitignore file to git by running the commands below.

git add .gitignore
git commit -m "your comment"
git push origin <my dev branch>

How to check the ignored files and folders

You can run the command below in order to see the ignored files and folders.

git status --ignored

And that’s how to ignore the files and folders. Hope it is useful.