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

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?

This question is related to git gitignore ignore

The answer is


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


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.


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

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.


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.

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

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.)


I hope it's not too late.

If you are on Windows you can just do the following to create a .gitignore file

echo name_of_the_file_you_want_to_ignore.extension > .gitignore

In order to edit .gitignore you can run

notepad .gitignore

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.


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

Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to gitignore

Gitignore not working How to add files/folders to .gitignore in IntelliJ IDEA? Apply .gitignore on an existing repository already tracking large number of files Ignore .classpath and .project from Git .gitignore all the .DS_Store files in every folder and subfolder Correctly ignore all files recursively under a specific folder except for a specific file type What should be in my .gitignore for an Android Studio project? Commit empty folder structure (with git) How to remove files that are listed in the .gitignore but still on the repository? What to gitignore from the .idea folder?

Examples related to ignore

When and why do I need to use cin.ignore() in C++? Git - Ignore files during merge How to remove files that are listed in the .gitignore but still on the repository? git ignore vim temporary files Git diff -w ignore whitespace only at start & end of lines How to git ignore subfolders / subdirectories? How do I configure git to ignore some files locally? Conditionally ignoring tests in JUnit 4 Is there an ignore command for git like there is for svn? How do I make Git ignore file mode (chmod) changes?