Staging, Committing, and Ignoring Changes

Explore the staging area (`git add`), committing changes with meaningful messages (`git commit`), and using `.gitignore` to exclude files from version control.


Learn Git and GitHub: Ignoring Files (.gitignore)

Ignoring Files (.gitignore)

When working on a project with Git, you often have files and directories that you don't want to track. These might include temporary files, build artifacts, sensitive information (like API keys), or operating system-specific files.

That's where the .gitignore file comes in. It's a powerful tool that tells Git which files and directories to deliberately ignore – meaning they won't be tracked or included in your commits.

Discover how to use the .gitignore file

Creating a .gitignore file

To create a .gitignore file, simply create a text file named .gitignore in the root directory of your Git repository. Make sure the filename starts with a dot (.) – this makes it a hidden file on Unix-like systems.

Adding Rules to .gitignore

The .gitignore file consists of a list of patterns, one per line, that specify which files and directories to ignore. Here's a breakdown of common pattern syntax:

  • Exact Filenames: To ignore a specific file, simply add its name to the .gitignore file.
    my_secret_file.txt
  • Wildcards (*): The asterisk (*) matches zero or more characters.
    *.log  # Ignores all files with the .log extension
  • Specific Directory: To ignore a specific directory:
    build/  # Ignores the entire 'build' directory
  • Directories and Files inside: To ignore all files within a directory recursively
    /node_modules # Ignores the node_modules directory in the root of the repo
    node_modules/ #Also Ignores the node_modules directory in the root of the repo
  • Double Asterisk (**): The double asterisk (**) matches directories recursively.
    **/temp  # Ignores any directory named 'temp' at any depth
    /docs/**/*.pdf #Ignores all .pdf files within the docs directory and all of its subdirectories
  • Negation (!): To *unignore* a file or directory that was previously ignored, use the exclamation mark (!). This can be useful for exceptions. Important: the negation rule must come *after* the rule that initially ignores the file.
    *.log
    !important.log  # This will include important.log, even though *.log is ignored
  • Comments: You can add comments to your .gitignore file by starting a line with a hash symbol (#).
    # Ignore build artifacts
    build/

Examples

Here's a more comprehensive example .gitignore file:

# Compiled source #
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
# it's better to unpack these files and commit the code
# rather than committing the package files themselves
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
*.log
*.sql
*.sqlite
*.sqlite3

# OS generated files #
.DS_Store
Thumbs.db

#IDE/Text Editor
.idea/
*.swp
*~ 

Important Considerations

  • Already Tracked Files: The .gitignore file only prevents files from being tracked *in the future*. If a file is already being tracked by Git, adding it to .gitignore will *not* automatically untrack it. To stop tracking a file that's currently being tracked, you need to use the command: git rm --cached <file>. Then commit the changes.
  • Ordering Matters: The order of rules in your .gitignore file can be important, especially when using negation.
  • Commit the .gitignore file: The .gitignore file should be committed to your repository so that everyone working on the project uses the same ignore rules.

Best Practices

  • Start Early: Create your .gitignore file at the very beginning of your project.
  • Global Ignores: You can configure Git to ignore certain files globally on your system, regardless of the repository. This is useful for things like editor backup files. Look into git config --global core.excludesfile.
  • Be Specific: Avoid overly broad ignore rules that might accidentally exclude files you *do* want to track.