How to ignore files in git using .gitignore file


None of us will like to commit and add intermediate files to git repositories such as log and temp files, in order to not track specific files in your git repository you can make use of the gitignore.

Git •gitignore file example

.gitignore - Git will not track files that are specified in this file.

How to create a .gitignore file

Within your git project repository create a file named .gitignore

% touch .gitignore

Add file names/extensions/regex to remove/ignore files

Any line that starts with as # hash or pound sign is a command and will be ignored!

Example: For Java Project:
# Compiled class file
*.class

# Log file
*.log

# Package Files
*.jar
*.war
*.ear
*.zip
*.tar.gz
Example:
% ls -a
.gitignore
2022070101_Local.log
release_1.2.0.jar
src
index.html

As you may see when I do a git status the log and jar files are not shown

% git status
.gitignore
src
index.html

This is not an AI-generated article but is demonstrated by a human on an M1 Mac running macOS Ventura 13.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap