Advertisement
Advertisement


How to create a .gitignore file


Question

I need to add some rules to my .gitignore file. However, I can't find it in my project folder. Isn't it created automatically by Xcode? If not, what command allows me to create one?

2019/08/04
1
1141
8/4/2019 5:41:58 PM


As simple as things can (sometimes) be: Just add the following into your preferred command line interface (GNU Bash, Git Bash, etc.)

touch .gitignore

As @Wardy pointed out in the comments, touch works on Windows as well as long as you provide the full path. This might also explain why it does not work for some users on Windows: The touch command seems to not be in the $PATH on some Windows versions per default.

C:\> "c:\program files (x86)\git\bin\touch.exe" .gitignore
2019/08/06

The easiest way to create the .gitignore file in Windows Explorer is to create a new file named .gitignore.. This will skip the validation of having a file extension, since is actually has an empty file extension.

2015/08/20

The .gitignore file is not added to a repository by default. Use vi or your favorite text editor to create the .gitignore file then issue a git add .gitignore followed by git commit -m "message" .gitignore. The following commands will take care of it.

> .gitignore
git add .gitignore
git commit -m "message" .gitignore
2018/07/23

========== In Windows ==========

  1. Open Notepad.
  2. Add the contents of your gitignore file.
  3. Click "Save as" and select "all files".
  4. Save as .gitignore.

======== Easy peasy! No command line required! ========

2020/06/20

MacOS / Linux one-liner

An easy way to get a default git ignore without messing about with create/copy/paste is to use the curl command from the terminal. First cd into your projects root directory and then run the command by replacing MY_API_NAME with your API name from one of the following two sources:

gitignore.io

curl -o .gitignore https://www.gitignore.io/api/MY_API_NAME

You can find your API name by searching from the list here and clicking Generate.

Java Example:

curl -o .gitignore https://www.gitignore.io/api/java

GitHub

Alternatively you can use the ones at GitHub. Find the filename for your API here.

curl -o .gitignore https://raw.githubusercontent.com/github/gitignore/master/MY_API_NAME.gitignore

Java Example:

curl -o .gitignore https://raw.githubusercontent.com/github/gitignore/master/Java.gitignore

Windows

Here are some similar alternatives for Windows.

But honestly setting that up looks like more trouble that it is worth. If I had Windows then I would just create an empty file called .gitignore in my project's root folder and then copy and paste the default text from gitignore.io or GitHub.

2017/07/24