How to Create a .Gitignore File

Better Stack Team
Updated on August 12, 2024

Creating a .gitignore file is an essential step in managing which files and directories Git should ignore in your repository. Here’s a detailed guide on how to create and configure a .gitignore file:

Steps to Create a .gitignore File

  1. Create the .gitignore File

    You can create a .gitignore file using any text editor or from the command line.

    Using Command Line:

     
    touch .gitignore
    

    This command will create an empty .gitignore file in the current directory.

    Using Text Editor:

 
- Open your preferred text editor (e.g., Notepad, VS Code, Sublime Text).
- Create a new file.
- Save the file as `.gitignore` in the root directory of your Git repository.
  1. Add Patterns to the .gitignore File

    Open the .gitignore file in your text editor and add the file patterns you want Git to ignore. Each pattern should be on a new line.

    Basic Patterns:

 
- Ignore all files with a specific extension:

    ```
    *.log
    ```

- Ignore a specific file:

    ```
    secret.txt
    ```

- Ignore a specific directory:

    ```
    temp/
    ```


**Advanced Patterns**:

- Ignore all files in a directory but not a specific file within that directory:

    ```
    temp/*
    !temp/important.txt
    ```

- Ignore files and directories at any level:

    ```
    **/logs/
    ```

- Ignore files with specific names in any subdirectory:

    ```
    **/debug.log
    ```
  1. Save and Add the .gitignore File to Your Repository

    Once you’ve added the necessary patterns, save the .gitignore file. Then, add it to your Git repository and commit the changes.

     
    git add .gitignore
    git commit -m "Add .gitignore file"
    
  2. Check the Status

    Verify that the files specified in the .gitignore file are not being tracked:

     
    git status
    

    Files listed in .gitignore should not appear in the output if they are not being tracked.

Example .gitignore File

Here’s an example .gitignore file that ignores log files, temporary directories, and specific files:

 
# Ignore log files
*.log

# Ignore temporary directories
temp/
cache/

# Ignore OS-specific files
.DS_Store
Thumbs.db

# Ignore compiled files
*.o
*.pyc
*.class

# Ignore specific files
secret.txt

Special Considerations

  • Already Tracked Files: If a file was already tracked by Git before adding it to .gitignore, you need to remove it from the index. Use:

     
    git rm --cached <file>
    

    For example:

     
    git rm --cached temp/important.txt
    
  • Global .gitignore: You can also create a global .gitignore file to apply ignore rules across all repositories for your user. This is often used for IDE or OS-specific files:

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

    Then, create ~/.gitignore_global and add patterns there.

Summary

To create a .gitignore file:

  1. Create the .gitignore file in your repository’s root directory.
  2. Add patterns to the file to specify which files and directories Git should ignore.
  3. Save the file, add it to your repository, and commit the changes.
  4. Verify that the files are correctly ignored using git status.

This helps keep your repository clean by ensuring that unnecessary files are not tracked by Git.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github