8Ignoring Files

Software projects generate a lot of cruft. Some of it you don’t need to commit. For example, I do a lot of work in Python, which leaves a ton of .pyc files laying around, and I edit using MacVim, which creates a swap file for each file that you’re editing. We don’t need or want these files cluttering up our repository or showing up in git status. That’s where the .gitignore and friends comes in.

Each line of the .gitignore is scanned, and any matches it finds are ignored by Git. Your .gitignore file is inside your repository, so you can track it like any other file. You can put it at the top level of your repository, and in that case the rules cascade through all subdirectories. You can also use subdirectory-specific .gitignore, and those rules will only apply to files and directories inside that subdirectory.

Sometimes you don’t want to commit your .gitignore file to your repository. Maybe you’re contributing to an open source project—there’s no need to add your *.swp to the project-wide .gitignore. You have two options in this case: use the .git/info/excludes file or add the ignore cases to your global excludesfile.

The .git/info/excludes is the same as a .gitignore file, except it’s not tracked by Git since it’s inside the .git directory. It’s useful for excluding files that are specific to a project without adding a .gitignore file to the repository.

For files that you want to ignore in every repository on your computer, you can set the core.excludesfile configuration value to point to a file that contains your global ignore rules. It follows the same format as the .gitignore and .git/info/excludes files.

What To Do...
  • Ignore a specific file and/or path called cache.

    Add the following to .gitignore:

     
    cache
  • Ignore all .swp files from Vim.

    Add the following to .gitignore:

     
    *.swp
  • Set up a global excludes file.

    Your excludes file can exist anywhere you want on your computer. The following example puts it in your home directory in the .gitignore file:

     
    prompt>​ git config --global core.excludesfile
     
    ~/.gitignore

    I have the following my ~/.gitignore since I’m on a Mac and use Vim:

     
    .DS_Store
     
    *.swp
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.137.58