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?
 
    
    - 8,169
- 7
- 57
- 74
 
    
    - 9,164
- 7
- 40
- 39
- 
                    1Good alternative to gitignore: **git update-index --assume-unchanged** , thanks to http://stackoverflow.com/a/16442091/2162226 – Gene Bo Jun 25 '15 at 19:49
10 Answers
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.
 
    
    - 35,870
- 12
- 47
- 57
- 
                    80Not having a simple ignore command seems like such an oversight on the part of the git team. – jcollum Apr 30 '12 at 21:52
- 
                    5@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. – abelito Apr 13 '19 at 13:13
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.
 
    
    - 3,880
- 2
- 26
- 44
 
    
    - 51,330
- 11
- 126
- 130
- 
                    1
- 
                    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 – Bynho Jan 29 '16 at 19:31
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
- 
                    1I found out this tweet using that syntax https://twitter.com/noodl_io/status/669904872704253953 – WJA Nov 26 '15 at 18:08
- 
                    3In 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`. – Ryan Mar 14 '16 at 02:21
- 
                    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. – RBT Jun 28 '21 at 09:23
You have two ways of ignoring files:
- .gitignorein any folder will ignore the files as specified in the file for that folder. Using wildcards is possible.
- .git/info/excludeholds the global ignore pattern, similar to the- global-ignoresin subversions configuration file.
 
    
    - 8,636
- 1
- 20
- 21
- 
                    6Note 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. – Jakub Narębski Nov 06 '09 at 19:45
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.
 
    
    - 16,892
- 20
- 80
- 117
 
    
    - 37,201
- 11
- 119
- 156
It's useful to define a complete .gitignore file for your project. The reward is safe use of the convenient --all or -a flag to commands like add and commit.
Also, consider defining a global ~/.gitignore file for commonly ignored patterns such as *~, which covers temporary files created by Emacs.
 
    
    - 14,999
- 2
- 48
- 58
Using the answers already provided, you can roll your own git ignore command using an alias. Either add this to your ~/.gitconfig file:
ignore = !sh -c 'echo $1 >> .gitignore' -
Or run this command from the (*nix) shell of your choice:
git config --global alias.ignore '!sh -c "echo $1 >> .gitignore" -'
You can likewise create a git exclude command by replacing ignore with exclude and .gitignore with .git/info/exclude in the above.
(If you don't already understand the difference between these two files having read the answers here, see this question.)
- 
                    1The answer here expands $1 correctly: http://stackoverflow.com/a/21884533/1465227 – Archangel33 Aug 25 '15 at 10:02
You have to install git-extras for this. You can install it in Ubuntu using apt-get,
$ sudo apt-get install git-extras
Then you can use the git ignore command.
$ git ignore file_name
 
    
    - 810
- 2
- 18
- 31
- 
                    3On Mac, if you have Homebrew installed, you can run `brew install git-extras`. – Alex Glover Feb 21 '19 at 16:35
for names not present in the working copy or repo:
echo /globpattern >> .gitignore
or for an existing file (sh type command line):
echo /$(ls -1 file) >> .gitignore   # I use tab completion to select the file to be ignored  
git rm -r --cached file             # if already checked in, deletes it on next commit
 
    
    - 133
- 1
- 1
- 8
You could also use Joe Blau's gitignore.io
Either through the web interfase https://www.gitignore.io/
Or by installing the CLI tool, it's very easy an fast, just type the following on your terminal:
Linux:
echo "function gi() { curl -L -s https://www.gitignore.io/api/\$@ ;}" >> ~/.bashrc && source ~/.bashrc
OSX:
echo "function gi() { curl -L -s https://www.gitignore.io/api/\$@ ;}" >> ~/.bash_profile && source ~/.bash_profile
And then you can just type gi followd by the all the platform/environment elements you need gitignore criteria for.
Example!
Lets say you're working on a node project that includes grunt and you're using webstorm on linux, then you may want to type:
gi linux,webstorm,node,grunt > .gitignore ( to make a brand new file)
or
gi linux,webstorm,node,grunt >> .gitignore ( to append/add the new rules to an existing file)  
bam, you're good to go
 
    
    - 1,897
- 1
- 22
- 30
 
     
     
    