DEV Community

Cover image for Git basics: Ignore files from being committed
Chris Bongers
Chris Bongers

Posted on • Edited on • Originally published at daily-dev-tips.com

Git basics: Ignore files from being committed

There are some files you don't want to commit to Git, simply because they would be too big and don't have any use.

Some examples of these files:

  • node_modules: Super big and changes every install
  • .env often contains secrets and keys. Make sure to ignore this file
  • Operating system files like: Thumbs.db, .DS_Store
  • .log files
  • Any cache directory

Luckily there is a simple way always to ignore these from your git repo.

Introducing the .gitignore file

As the name suggests, the .gitignore file will ignore specific files you marked inside it.

To use one, create a file called .gitignore at the root of your project.

Let's already add our operating files like so:

# Ignore platform files
Thumbs.db
.DS_Store
Enter fullscreen mode Exit fullscreen mode

But let's put it to the actual test and initialize npm in our testing repo.

npm init -y
Enter fullscreen mode Exit fullscreen mode

Now let's add a random package. I choose fastify for testing purposes.

npm i fastify
Enter fullscreen mode Exit fullscreen mode

If we then look at our Git-changed files, we'll see a massive list of files.

Git open changes for node_modules

That's not really what we want as this contains all the node_modules files.

Let's modify our .gitignore file to ignore these like so:

# Ignore platform files
Thumbs.db
.DS_Store

# Packages
node_modules
Enter fullscreen mode Exit fullscreen mode

If we take another look, we'll see there are only three files ready to be committed.

Git commit with gitignore in place

Way better!

And there you go, you can add any files you want to this .gitignore file but be aware of what you'll don't want to be committed.

I've pushed this to GitHub in case you want to have a look.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (3)

Collapse
 
gitkat profile image
GitKat

Thanks. but I have a question. There is a situation where ive tried and failed before that sometimes I commit the whole project which is fine but during the development process I want to add some files in the .gitignore which does not work. I searched around google and found that I have to go to repository and delete that file from there first and then add it to the .gitignore. and also if I discard the file in the "changes" it discards my file which is frustrating. so I keep it unchecked and keep it in the "changes" list. I work on home pc, office pc and server pc so 3 of them have 3 config files which cause conflict.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Got you, it happens and is very annoying indeed.
Got a article coming up about removing files from git in a couple of days.

You'll have to use the git rm command to remove files after adding them to the gitignore file is what it comes down to :)

You can always hit me up on DM if you need assistance, happy to help 🎉

Collapse
 
gitkat profile image
GitKat

I use GitHub Desktop