Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions site/posts/understanding-gitignore/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
title: "Understanding gitignore: A Simple Guide"
author: "rajwanur"
date: "2025-09-04"
categories: [git, tools, version-control]
toc: TRUE
toc-title: "Table of contents"
toc-depth: 5
---

# Understanding gitignore: A Simple Guide

If you are working with Git but find yourself dealing with unnecessary files cluttering your repository, **.gitignore** is a tool that can help. Let's explore what it does in plain terms.

## What is gitignore?

.gitignore is simply a text file that tells **Git** which files or folders to ignore in your project. It works like an instruction list for version control - when Git sees something mentioned in this file, it pretends that file doesn't exist and will not include it in tracking changes.

Think of it as creating a "do not pack" list before the travel. .gitignore helps programmers avoid committing files they don't want to track. It also helps programmers exclude the files that don't need to be versioned.

## The Problem It Solves

When working with code - especially languages like SAS, R, or Python - we often generate temporary files:

- Log files showing execution results
- Temporary output files
- Large datasets created during processing
- Configuration files specific to the local machine
- Compiled binaries and dependencies

These files can clutter the repository, making it harder to see actual code changes. They also unnecessarily increase repository size, which can slow down operations.

.gitignore solves this by automatically excluding these unwanted files from version control without someone having to manually specify each time.

## How It Works

The .gitignore file uses simple patterns to match filenames:

- `*.log` - ignores all files ending with .log
- `temp/` - ignores any folder named temp
- `/build/` - ignores a build folder in the root directory
- `*.tmp` - ignores all temporary files with extension `.tmp`
- `!*.sas` - **do not ignore** all files ending with .sas
- `# This is a comment` - Anything after a # is a comment and is ignored

For complete syntax please refer to the [official documentation](https://git-scm.com/docs/gitignore).

These rules are applied whenever Git checks for changes, so you only see relevant modifications.

## Different Ways to Use gitignore

### Basic Setup

For most projects, create a .gitignore file in your project's root directory with patterns specific to your language or tools:

```
# Ignore log files
*.log
*.tmp

# Ignore compiled output
/bin/
/dist/

# Ignore IDE configuration files
.idea/
.vscode/
```

### Project-Specific Rules

Different programming languages often have different temporary files:

- For SAS programs:
```
# To exclude SAS log, lst, and sas7bdat files
*.log
*.lst
*.sas7bdat
```

- For R projects:
```
# To exclude R temporary files
.RData
.Rhistory
.Rproj.user
*.Rproj
```

- For Python projects:
```
__pycache__/
*.pyc
.env
.pytest_cache
.venv/
```

### Global Ignore Patterns

We can set up global ignore patterns that apply to all our repositories:

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

Then add common patterns in .gitignore_global file located at ~/ (home directory) that should be ignored across all projects.

## Best Practices

1. **Create early**: Add .gitignore at the beginning of a project
2. **Commit it**: Make sure .gitignore itself is version controlled
3. **Share with team**: Everyone working on a project should use the same rules
4. **Review occasionally**: As your project evolves, update your ignore patterns
5. **Exclude all/include some**: To avoid new file types from being tracked, exclude all and include what is expected

## Resources

- [.gitignore syntax](https://git-scm.com/docs/gitignore)
- [Understanding .gitignore](https://www.atlassian.com/git/tutorials/saving-changes/gitignore)
- [Automatically Generate gitignore Files](https://www.gitignore.io/)


## Conclusion

.gitignore is a simple but powerful tool that helps maintain clean repositories by excluding unnecessary files. It's not magical - just practical configuration that saves time and reduces clutter in version control systems.

If you haven't used .gitignore before, give it a try on your next project. You'll likely find yourself wondering how you ever worked without it!