Why You Shouldn’t Ignore the .gitignore File: Best Practices for Git

Why You Shouldn’t Ignore the .gitignore File: Best Practices for Git

The .gitignore file is one of the most overlooked yet crucial components in version control with Git. Many developers, especially those new to Git, often underestimate its importance, thinking of it as just another file in their repository. However, the .gitignore file plays a vital role in maintaining a clean and efficient project environment.

By defining which files and directories should be excluded from version control, the .gitignore file helps prevent unnecessary or sensitive data from being tracked and shared. This can include build artifacts, environment-specific configurations, or temporary files that do not belong in the codebase. Ignoring the .gitignore can lead to cluttered repositories, potential security risks, and difficulties in collaboration.

So far so good right does it explained to you that why .gitignore is important? Not Yet ? No worries let us dive more.

I will be answering directly so as to not waste your time.

When you build a project using create-react-app or using directly webpack you often see a .gitignore file which have data like below image. Note-: Your file may or may not be exactly matching but more or less same.

So you see in above screenshot that we have something called build and .env these are most common files which every developer come across while doing a web development. So what does this do here? It simply avoids congestion in your git versioning control system, see below an example –
when build not in .gitignore vs when build is in .gitignore

Two ways to check the files changed in a project: –
1) When build is in .gitignore
Check using git command git status or check using graphical user interface (GUI) of VS code.
See below you can see two file changes only.

2) When build not in .gitignore

Now see below screenshot suddenly unwanted files started coming in git status which is showing lot of changes which are not meant to be pushed in git and remember these changes can be huge depending on your project code size as you might be using multiple packages from npm.
So why we do not push build to git? Because we do not need it simple, but why? Because anyone can clone your repository and do npm install and then npm run build in their system and build file is ready, no need to directly share it. This is your build file only which is then pushed to hosting servers and then you see your website and this file is very large in size you cannot share it over teams, whatsapp etc because it is actually a pain in ass waiting for it to get it sent.

Will explain in my upcoming blog how to push this build to your server to make it publically accessible So stay tuned !! Bbye

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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