Managing Files with .gitignore in Git

In the previous blog, we learned how to use patterns with the git add command. But here comes a new challenge: what if you don’t want some files to ever enter the staging area at all? That’s where the .gitignore file comes in.


Problem Statement

Let’s create a few files first:

touch application.c module.pyc output.exe application.log output.out

If we run git status, Git shows all of them:

gaurav@learning-ocean:~/git-tutorials$ git status
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        application.c
        module.pyc
        output.exe
        application.log
        output.out

Now, if we run:

git add .

👉 The problem is that all files get staged — including log files, compiled outputs, and temporary binaries. But we don’t want that. We only want application.c to go in staging.


Creating a .gitignore File

To solve this, create a .gitignore file:

vi .gitignore

Add the file patterns you want Git to ignore:

# Ignore compiled Python files
*.pyc

# Ignore log files
*.log

# Ignore executables
*.exe
*.out

Save and exit.


Checking Status Again

Now run:

git status

Output:

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        application.c

🎉 Only application.c and .gitignore remain. All unwanted files (module.pyc, output.exe, application.log, output.out) are ignored.

If we now add files:

git add .
git status

Output:

Changes to be committed:
        new file:   .gitignore
        new file:   application.c

Perfect ✅.


Using Patterns in .gitignore

Suppose we create more files:

touch service1.log service2.log service3.log service1.c

Check status:

Untracked files:
        service1.c
        service1.log
        service2.log
        service3.log

We never want .log files to enter Git. Instead of listing each file, we use a pattern:

Edit .gitignore:

*.log

Now git status shows only:

Untracked files:
        service1.c

All .log files are ignored automatically.


Summary

  • .gitignore tells Git which files not to track.
  • You can ignore by file name (application.log) or by extension (*.log).
  • This keeps your repo clean from unwanted binaries, logs, and temp files.

In the next blog, we’ll learn advanced .gitignore usage — including how to ignore some files but keep exceptions.


📺 Watch the Video

👉 Watch this blog in video format on YouTube


🚀 Don’t Miss Out!

Now you know how to use .gitignore to filter out unnecessary files. In the next blog, we’ll dive deeper into exceptions and path-specific ignores.

👉 Subscribe to Learning Ocean – Subscribers get coupon codes, early access to blogs/courses, and exclusive YouTube content.

👉 My YouTube Channel

👉 Watch this video explanation

Stay curious, keep coding, and let’s master Git together! 🎉