DEV Community

Ankit malik
Ankit malik

Posted on • Updated on

What is .gitignore file in github, gitlab

Introduction

When working on a project with Git, there are often files or directories that you don't want to track, such as temporary files, compiled code, or sensitive data. To exclude these files from being committed to your repository, you can use a .gitignore file.

What is .gitignore file

A .gitignore file is a simple text file that contains a list of patterns for Git to ignore when it performs operations like staging, committing, or pushing. Git will not add files or directories that match these patterns to the repository, nor will it show them as untracked files when you run git status.

Here is a basic .gitignore template to get started:



# Ignore files generated by the IDE
.idea/

# Ignore compiled code
*.class
*.pyc

# Ignore sensitive data
secrets.txt

# Ignore build output
build/
dist/



Enter fullscreen mode Exit fullscreen mode

In this example, we are ignoring the .idea directory, which is generated by some IDEs, as well as any compiled code files (.class and .pyc), which are generated by some programming languages. We're also ignoring a file called secrets.txt, which contains sensitive data, and the build/ and dist/ directories, which contain build output.

You can customize the .gitignore file to your specific project needs. Here are some tips for creating an effective .gitignore file:

Be specific: Use exact filenames or directory names to exclude only what you want to exclude.
Use wildcards: Use * to match any characters, and ? to match a single character. For example, *.log will ignore all files with the .log extension.
Comment your patterns: Use # to add comments to your .gitignore file explaining why certain files or directories are being ignored.
It's important to note that a .gitignore file only affects the local repository where it is located. If you have multiple developers working on the same project, each person should create their own .gitignore file with patterns specific to their local environment.

What is Global .gitignore file

You can also create a global .gitignore file that applies to all of your Git repositories. To do this, create a file called .gitignore_global in your home directory, and add patterns to it just like you would with a regular .gitignore file. Then, run the following command to tell Git to use the global .gitignore file:



git config --global core.excludesfile ~/.gitignore_global

Enter fullscreen mode Exit fullscreen mode




Benefits of .gitignore file

  • Preventing accidentally committed files: .gitignore helps to prevent accidental commits of files that shouldn't be tracked in your Git repository. For example, temporary files, log files, and other files that are generated during the development process but should not be part of the final product.
  • Keeping the repository clean: Ignoring unnecessary files and directories can help keep your repository clean and organized, making it easier to navigate and manage.
  • Saving disk space: Ignoring large files or directories that aren't needed can help save disk space in your repository, which can be especially important when working with large files or when disk space is limited.
  • Improving performance: By ignoring unnecessary files and directories, Git can perform more efficiently, reducing the time it takes to clone, checkout, and perform other operations on the repository.
  • Collaborating with others: A .gitignore file can help ensure that everyone working on the repository is on the same page about which files and directories should be tracked and which ones shouldn't.

How to use pre-existing .gitignore template

Most of time people use pre-existing templates available.

You can create a pre-existing template while you are creating the repository based on your language because most of languages has some pattern of files or folder structure which can be ignored.

Choosing gitignore template from github

Github also published an open-source repo for .gitignore files for different languages.
https://github.com/github/gitignore

Conclusion

In conclusion, using a .gitignore file is a simple and effective way to exclude files and directories from your Git repository. By following the tips above, you can create a customized .gitignore file that meets the needs of your specific project.

Top comments (1)

Collapse
 
datamuc profile image
datamuc
git config --global core.excludesfile ~/.gitignore_global
Enter fullscreen mode Exit fullscreen mode

That's not needed. There is a default global ignore file: ~/.config/git/ignore