Is there an ignore command for git like there is for svn?

131,151

Solution 1

There is no special git ignore command.

Edit a .gitignore file located in the appropriate place within the working copy. You should then add this .gitignore and commit it. Everyone who clones that repo will than have those files ignored.

Note that only file names starting with / will be relative to the directory .gitignore resides in. Everything else will match files in whatever subdirectory.

You can also edit .git/info/exclude to ignore specific files just in that one working copy. The .git/info/exclude file will not be committed, and will thus only apply locally in this one working copy.

You can also set up a global file with patterns to ignore with git config --global core.excludesfile. This will locally apply to all git working copies on the same user's account.

Run git help gitignore and read the text for the details.

Solution 2

A very useful git ignore command comes with the awesome tj/git-extras.

Here are a few usage examples:

List all currently ignored patterns

git ignore

Add a pattern

git ignore "*.log"

Add one of the templates from gitignore.io

git ignore-io -a rails

git-extras provides many more useful commands. Definitely worth trying out.

Solution 3

On Linux/Unix, you can append files to the .gitignore file with the echo command. For example if you want to ignore all .svn folders, run this from the root of the project:

echo .svn/ >> .gitignore

Solution 4

You have two ways of ignoring files:

  • .gitignore in any folder will ignore the files as specified in the file for that folder. Using wildcards is possible.
  • .git/info/exclude holds the global ignore pattern, similar to the global-ignores in subversions configuration file.

Solution 5

Create a file named .gitignore on the root of your repository. In this file you put the relative path to each file you wish to ignore in a single line. You can use the * wildcard.

Share:
131,151
Chris J
Author by

Chris J

Updated on December 19, 2020

Comments

  • Chris J
    Chris J over 3 years

    I am a new user to git and I am starting a new project. I have a bunch of dot files that I would like to ignore. Is there an ignore command for git like there is for svn?

  • Anonigan
    Anonigan over 14 years
    Note that patterns in .gitignore file which do not contain '/' are matched recursively (i.e. also in all subdirectories of the directory the .gitignore file is in), contrary to the case of svn:ignore directory properties in Subversion.
  • jcollum
    jcollum about 12 years
    Not having a simple ignore command seems like such an oversight on the part of the git team.
  • Archangel33
    Archangel33 over 8 years
    The answer here expands $1 correctly: stackoverflow.com/a/21884533/1465227
  • JohnAndrews
    JohnAndrews over 8 years
    So if I use git ignore "index.html" it will ignore this file?
  • JohnAndrews
    JohnAndrews over 8 years
    I found out this tweet using that syntax twitter.com/noodl_io/status/669904872704253953
  • Bynho
    Bynho over 8 years
    Is the -t command still applicable? When I run it, it just adds the -t and whatever template I try to add into the git ignore file
  • Ryan
    Ryan about 8 years
    In Git Bash on Windows, I needed to add a preceding / and trailing '\' (to create a new line), such as echo /nbproject/project.properties\ >> .gitignore and echo /nbproject/project.xml\ >> .gitignore.
  • Alex Glover
    Alex Glover about 5 years
    On Mac, if you have Homebrew installed, you can run brew install git-extras.
  • abelito
    abelito about 5 years
    @jcollum At first glance it does, but the simplicity of aliasing a command to add new ones AND the complexity of creating one and working through all the edge cases/gotchas/potential issues of editing a text file also makes sense to leave it to the user. I feel conflicted because I both agree and disagree with you.
  • RBT
    RBT almost 3 years
    That forward slash / character in the end is of paramount importance. Without it, the new rule gets appended to the last entry already existing in the .gitignore file in place of getting created as a new entry at the bottom of the file.