Version control and git are very important tools when it comes to developing software. You may have been wondering what exactly is the .gitignore file and how it can make your version a little bit easier.

.gitignore usage

In this article, I already assume that you have at least a basic knowledge of what version control and git are. If you’re not aware of what those are then, please make sure that you familiarise yourself before you get even more confused!

In short, .gitignore lists all of the files that we want to be ignored by git.

If you’ve used the create-react-app to set up your React project then this file is already provided for you and looks like below.

# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

Comments in this file start with the # symbol and are ignored by the software. All the other entries are file patterns to be ignored by git. You should provide one configuration per line. Take a look at git documentation to see how those should be written.

Example

Let’s now take a look at a small example. I have uncommitted BUT tracked files in my project.

Let’s now try to ignore all of the files located in the src folder. To do that I need to append a new line in .gitignore

/src

The next step is to remove. Let’s now make git forget about the src folder by running

git rm -r --cached src/.

And that’s all that has to be done. Git will not be picking up any changes made to any of the files in src from now on. Let’s add a dummy file to make sure that this is indeed the case.

And after running git status we can see that no changes in src are being picked up by git.

Let’s now undo this behaviour by removing

/src

from .gitignore

We can now start tracking those files again. Including our newly created dummy.txt.

πŸ‘‹ Hey, I'm Dawid Budaszewski
Hello! I'm Dawid. I'm a full-stack developer with a couple of years of experience under my belt. I spent most of my career working with React. Now it's time to pass the knowledge onto somebody else!

πŸ’¬ Leave a comment

Your email address will not be published.

We will never share your email with anyone else.