How to create a .gitignore file

Gitxcode4.3Gitignore

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

If you're using Windows it will not let you create a file without a filename in Windows Explorer. It will give you the error "You must type a file name" if you try to rename a text file as .gitignore

enter image description here

To get around this I used the following steps

  1. Create the text file gitignore.txt
  2. Open it in a text editor and add your rules, then save and close
  3. Hold SHIFT, right click the folder you're in, then select Open command window here
  4. Then rename the file in the command line, with ren gitignore.txt .gitignore

Alternatively @HenningCash suggests in the comments

> You can get around this Windows Explorer error by appending a dot to > the filename without extension: .gitignore. will be automatically > changed to .gitignore

Solution 2 - Git

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

Note: The path might differ, depending on your setup and installation path.

Solution 3 - Git

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.

Solution 4 - Git

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

Solution 5 - Git

========== 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! ========

Solution 6 - Git

On Windows you can use cmd echo "" >.gitignore

Or use Git Bash cmd touch .gitignore, this useful for Linux and Mac System

Solution 7 - Git

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.

Solution 8 - Git

I want my contribution as well. This time, animated one :)

VIM (mini tutorial):

i   - start editing
ESC - get back to normal mode
:w  - save 
:q  - quit

enter image description here

Solution 9 - Git

Using Git Bash console.

-Navigate to your project -Type "touch .gitignore"

the .gitignore file will be created for you.

enter image description here

Solution 10 - Git

My contribution is aimed at those on a Mac, and it can be applied to not only those working on an iOS project (as implied by the question mentioning Xcode), but any type of project.



The easy way that I do it is to go into the terminal and run vim .gitignore and then add the files. Usually you can just copy what you need from one of the templates on GitHub at https://github.com/github/gitignore.


Step 1
While in your project, type the following command

vim .gitignore

Enter image description here



Step 2
You now have your file open with Vim.

Enter image description here

Press i to insert text. You will see that the file is ready when you see the --INSERT-- at the bottom.

Enter image description here



Step 3 (option 1)
For Objective-C projects, you can copy from https://raw.githubusercontent.com/github/gitignore/master/Objective-C.gitignore and paste it into your .gitignore file:

Enter image description here

Press Esc, type in :wq, and press Return. Which saves the file.



Step 3 (option 2)
Add whatever files apply to your project.

If you are not sure what to add, the best keywords to use in your search engine would be to include your project type and text editor. For example, if you use Sublime Text you would want to add

*.sublime-workspace

And if you are working with a Cordova project in Dreamweaver you would want to add

_notes
dwsync.xml

Solution 11 - Git

Here a nice tip under Windows:

  • Right click in Windows Explorer, New > Text Document
  • Name it .gitignore. (with a trailing dot - that is the tip)
  • You end up with a .gitignore file :)

Tested under Windows 7 and 8.

This tip assumes that your Windows Explorer displays the file extensions.

Windows Explorer .gitignore

Solution 12 - Git

Create a .gitignore file in include all files and directories that you don't want to commit.

Example:

#################
## Eclipse
#################

*.pydevproject
.project
.metadata
.gradle
bin/
tmp/
target/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath


#################
## Visual Studio
#################

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.sln.docstates

# Build results

[Dd]ebug/
[Rr]elease/
x64/
build/
[Bb]in/
[Oo]bj/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.scc

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile

# Visual Studio profiler
*.psess
*.vsp
*.vspx

# Guidance Automation Toolkit
*.gpState

# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper

# TeamCity is a build add-in
_TeamCity*

# DotCover is a Code Coverage Tool
*.dotCover

# NCrunch
*.ncrunch*
.*crunch*.local.xml

# Installshield output folder
[Ee]xpress/

# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html

# Click-Once directory
publish/

# Publish Web Output
*.Publish.xml
*.pubxml

# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/

# Windows Azure Build Output
csx
*.build.csdef

# Windows Store app package directory
AppPackages/

# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.[Pp]ublish.xml
*.pfx
*.publishsettings

Solution 13 - Git

http://gitignore.io is an open source utility that can help you create useful .gitignore files for your project. There is also a command line API that you can access via a gi command: http://gitignore.io/cli

  1. Install gi command for OS X:

$ echo "function gi() { curl http://gitignore.io/api/\$@ ;}" >> ~/.bash_profile && source ~/.bash_profile

  1. View .gitignore file contents (Output: http://gitignore.io/api/xcode,osx):

$ gi xcode,osx

  1. You should see output on the terminal, if you want to append the results to a new .gitignore file.

$ gi xcode,osx >> .gitignore

Solution 14 - Git

I have another simple idea
Let's use the echo command in cmd ,

echo ./idea > .gitignore

this will create the .gitignore file having text content "./idea"
you may now manually change data from the file using text editor.

or simply

console :

> echo .gitignore notepad.exe

to instantly edit gitignore.

If you dont know which files are should be gitignored for your IDE or Operating System just goto www.gitignore.io

>gitignore.io - here it wll generate the gitignore commands or text for you just say your api or os thats it!. Just copy and paste into your file. simple!

Solution 15 - Git

Here's my personal favorite, http://help.github.com/ignore-files/

Also just in case you wanted to ignore Xcode files, refer to an answer to Git ignore file for Xcode projects.

Solution 16 - Git

You can go to https://www.toptal.com/developers/gitignore

Select the IDE, operating systems or programming language. It will automatically generate for you. enter image description here

Solution 17 - Git

in windows, open a dos prompt(cmd) windows, use command line:

type > .gitignore

Solution 18 - Git

If you don't want to have your .gitignore interfere with anyone else's repository, you can also use .git/info/exclude. (See http://help.github.com/ignore-files/)

Solution 19 - Git

The following works in PowerShell and a command prompt (CMD):

echo '*.ignore_me' > .gitignore

I ran into a weird issue where Git effectively wouldn't read the .gitignore file. I then deleted the .gitignore file and created one using Vim which worked fine.

To add additional files to ignore, just call the following command:

echo 'another_file_to_ignore' >> .gitignore

It will append further files to the existing .gitignore file.

Solution 20 - Git

#Few ways to create .gitignore using cmd:

  • With copy con command:
  1. open cmd and say cd to your git repository
  2. say copy con .gitignore and press Ctrl+Z.

enter image description here

  • With start notepad .gitignore command:
  1. open cmd and say cd to your git repository
  2. say start notepad .gitignore and press Yes button in opened notepad dialog box.

enter image description here

  • With edit .gitignore command (Windows x86 only):
  1. open cmd and say cd to your git repository
  2. say edit .gitignore and close opened edit editor.

Solution 21 - Git

Windows enter image description here

> File Name : ".gitignore" , > Save as type : All Files

Solution 22 - Git

You can directly create empty .gitignore file , open cmd in the location you need to add this file and type this command

copy con .gitignore

press ENTER you are now in edit mode of the newly created file, but we do not need to add anything now, just press F6 and then press ENTER Now you have an empty .gitignore file, edit your file in whatever editor you have

Solution 23 - Git

  1. create a .gitignore file, so to do that, you just create a .txt file and change the extention as following: enter image description here

then you have to change the name writing the following line on the cmd:

 rename git.txt .gitignore

where git.txt is the name of the file you've just created.

Then you can open the file and write all the files you don´t want to add on the repository. For example mine looks like this:

#OS junk files
[Tt]humbs.db
*.DS_Store

#Visual Studio files
*.[Oo]bj
*.user
*.aps
*.pch
*.vspscc
*.vssscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.[Cc]ache
*.ilk
*.log
*.lib
*.sbr
*.sdf
*.pyc
*.xml
ipch/
obj/
[Bb]in
[Dd]ebug*/
[Rr]elease*/
Ankh.NoLoad

#Tooling
_ReSharper*/
*.resharper
[Tt]est[Rr]esult*

#Project files
[Bb]uild/

#Subversion files
.svn

# Office Temp Files
~$*

Once you have this, you need to add it to your git repository. You have to save the file where your repository is.

Then in your git bash you have to write the following line:

enter image description here

If the respository already exists then you have to do the following:

  1. git rm -r --cached .
  2. git add .
  3. git commit -m ".gitignore is now working"

If the step 2 dowsn´t work then you should write the hole route of the files that you would like to add.

Hope it helps!

Solution 24 - Git

1. Open git terminal
2. go to git repository of the project
3. create a .gitignore file by **touch .gitignore** command
4. **git add .gitignore** command to add ignore file
5. set ignore rules in the ignore file
6. run the command **cat .gitignore**

By running the command in step 3 you will get the .gitignore file in the project directory. Thanks.

Solution 25 - Git

windows: in the commandline:

.>.gitignore

this will show an error but will work

Solution 26 - Git

To add .gitignore file to your not application you can use the

> npx add-gitignore

Now you can type "node" and use user space bar to choose it and Enter. That will add the node .gitignore to the project.

enter image description here

Solution 27 - Git

Without using command line

  1. Open texteditor and add your rules.
  2. Click File->Save As
  3. Save it as ".gitignore" (include the quotations)

Solution 28 - Git

To force Finder to display hidden files and folders via Terminal:

    Open Terminal
    For OS X 10.9 Mavericks, run this command (lower-case finder):
    defaults write com.apple.finder AppleShowAllFiles TRUE
    For OS X 10.8 Mountain Lion, 10.7, or 10.6, run this command (upper-case Finder):
    defaults write com.apple.Finder AppleShowAllFiles true
    notice the setting for true
    Then run this command: killall Finder
    Then exit Terminal
    To revert back to Finder’s default setting (hide hidden files and folders), 
run the opposite command but with the false setting.

Then run killall Finder and exit Terminal.

Solution 29 - Git

At work we are on Windows XP, and typing a period at the end of a filename doesn't work. A quick easy way to create a .gitignore file without having the "You must type a filename"error is:

  1. open a cmd window and type "edit .gitignore".
  2. type "Alt (selects file menu), F, S. You now have an empty .gitignore file wherever your cmd prompt is pointing.

You can now populate it with your favorite text editor

Solution 30 - Git

To add any file in Xcode, go to the menu and navigate to menu FileNewFile...

For a .gitignore file choose OtherEmpty and click on Next. Type in the name (.gitignore) into the Save As field and click Create.

For files starting with a dot (".") a warning message will pop up, telling you that the file will be hidden. Just click on Use "." to proceed...

That's all.

To fill your brand new .gitignore you can find an example for ignoring Xcode file here: https://stackoverflow.com/questions/49478/git-ignore-file-for-xcode-projects/12021580#12021580

Solution 31 - Git

If you use Sublime Text as your IDE, you can create a new file and save it as .gitignore. Simply using Ctrl + N for the new file, and Ctrl + S to save as ".gitignore".

Solution 32 - Git

You can type new-item .gitignore in Windows Powershell.

Solution 33 - Git

In mac - you can just create a new text file. add content using https://www.gitignore.io/

save the file with file format as - Rich Text document with attachments. change file name to .gitingore and select use"." when a pop up comes as in the attached image.

NOTE : since it is a hidden file so you wont be able to see it in the directory. but it will be created.

see image

Solution 34 - Git

I use notepad++. New File > SaveAs > .gitignore (Save as type -> All types(.))

Solution 35 - Git

There is a pretty simple way to create a .gitignore file.This one is created in github and I'm pretty sure that most source controls offer the feature for creating a file there itself.Attaching an image by image tutorial for reference.

1.enter image description here

2.enter image description here

3.enter image description here

4.enter image description here

Solution 36 - Git

I realize this question is focused on how to "create" the gitignore file, but in case someone is interested in a quick way to add contents to the file once it is created, here is my answer for those trying to "ignore" files that appear in their changes list.

  1. Make the changes to your code which generate unwanted changes in your repository.
  2. Go to GitHub Desktop and to your repository.
  3. Select all the changes and right click them.
  4. Add your changes to the gitignore file.

enter image description here

Solution 37 - Git

This was right up my ally -- though, I'm still trying to figure out how some .dlls got in:

http://www.gitignore.io/ http://www.diaryofaninja.com/blog/2011/06/01/how-to-easily-create-a-gitignore-file-inside-windows-explorer

Hope this helps!

Solution 38 - Git

Yes windows explorer wouldn't allow you to create this file name. Another easy way to come around this is to create a dummy file in the directory for example NewFile.txt and than just simply rename it in git bash like following:

mv NewFile.txt .gitignore

Solution 39 - Git

One thing that I haven't seen mentioned yet, is that you can actually make Xcode generate it automatically, when you start a new project. In order to do so, you'll have to start doing some harmless hacking yourself...

Before you begin: Make a backup of "Project Templates", as I predict you'll want to do more than I just mention, once you've discovered it.

Now, go to /Developer/Library/Xcode/Project Templates/Application/Cocoa Application/ Add your .gitignore file there.

That's all. When you create a new "Cocoa Application" project, then the .gitignore file is automatically copied from your project templates.

Beware if you want to edit the templates themselves. Use nano for that; do not use Xcode or TextEdit, they mess up the unicode characters! -Well Xcode also messes up everything else.

Note: There's also a "File Templates", which you should also make a backup of before you start modifying them. Again: Use nano for editing them; not Xcode, nor TextEdit.

Here's one of my own .gitignore files, which you can use for inspiration:

.DS_Store
Icon\15
Icon\r
Icon\n
/*.xcodeproj/*.mode*
/*.xcodeproj/*.pbxuser
/*.xcodeproj/TemplateIcon.icns
/*.xcodeproj/.LSOverride
!/*.xcodeproj/default.*
/*.pbproj/*.mode*
/*.pbproj/*.pbxuser
/*.pbproj/*.perspective*
/build/
*.moved-aside
*~.nib
*~.xib

Note: I use Xcode 2.5 and Xcode 3.1.4 (would prefer 3.1, but it keeps spamming my console)

Solution 40 - Git

On mac:

  • open terminal and run defaults write com.apple.finder AppleShowAllFiles YES anywhere.

  • restart finder so you can see hidden files: command + option + escape ---> Relaunch

Then create a text file and you will be able to change the file extension to .gitignore

Solution 41 - Git

In Ubuntu, you can create the .gitignore file and add (ignore) all the files over 50Mb from the current directory . using

find . -size +100M | cat >> .gitignore

Solution 42 - Git

Here is a one liner version of linux "touch" in Windows

c:\<folder>\break > .gitignore

that will create a blank .gitignore file where you can edit and add items to ignore

C:\Users\test>dir .gitignore
 Volume in drive C is Windows
 Volume Serial Number is 9223-E93F

 Directory of C:\Users\test

18/04/2019  02:23 PM                 0 .gitignore
               1 File(s)              0 bytes
               0 Dir(s)  353,009,770,496 bytes free

C:\Users\test>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLucaView Question on Stackoverflow
Solution 1 - GitajcwView Answer on Stackoverflow
Solution 2 - GitkaiserView Answer on Stackoverflow
Solution 3 - GitchosView Answer on Stackoverflow
Solution 4 - Gitsully6768View Answer on Stackoverflow
Solution 5 - Gituser3083619View Answer on Stackoverflow
Solution 6 - GitseayView Answer on Stackoverflow
Solution 7 - GitSuragchView Answer on Stackoverflow
Solution 8 - GitOo.oOView Answer on Stackoverflow
Solution 9 - GitRoberto RodriguezView Answer on Stackoverflow
Solution 10 - GitJGallardoView Answer on Stackoverflow
Solution 11 - Gittanguy_kView Answer on Stackoverflow
Solution 12 - GitAnderson LopesView Answer on Stackoverflow
Solution 13 - GitJoeView Answer on Stackoverflow
Solution 14 - GitNIKHIL C MView Answer on Stackoverflow
Solution 15 - GitSivakumar KailasamView Answer on Stackoverflow
Solution 16 - GitZohra KhanView Answer on Stackoverflow
Solution 17 - GitcaoglishView Answer on Stackoverflow
Solution 18 - GitEfForEffortView Answer on Stackoverflow
Solution 19 - GitMadJangiView Answer on Stackoverflow
Solution 20 - GitYuliia AshomokView Answer on Stackoverflow
Solution 21 - GitIshan LakshithaView Answer on Stackoverflow
Solution 22 - GitAmado SaladinoView Answer on Stackoverflow
Solution 23 - GitSomeAnonymousPersonView Answer on Stackoverflow
Solution 24 - GitRashedul.RubelView Answer on Stackoverflow
Solution 25 - GitTimar Ivo BatisView Answer on Stackoverflow
Solution 26 - GitShinoy BabuView Answer on Stackoverflow
Solution 27 - GitYellowJView Answer on Stackoverflow
Solution 28 - GitRaja JawaharView Answer on Stackoverflow
Solution 29 - GitMarioView Answer on Stackoverflow
Solution 30 - GitNoctisView Answer on Stackoverflow
Solution 31 - GitmtpultzView Answer on Stackoverflow
Solution 32 - GitNitoriKawashiroView Answer on Stackoverflow
Solution 33 - GitJaspinder KaurView Answer on Stackoverflow
Solution 34 - GitKrishhView Answer on Stackoverflow
Solution 35 - GitHaseeb MohamedView Answer on Stackoverflow
Solution 36 - GitThorView Answer on Stackoverflow
Solution 37 - GitCodyView Answer on Stackoverflow
Solution 38 - GitzarView Answer on Stackoverflow
Solution 39 - Gituser1985657View Answer on Stackoverflow
Solution 40 - GitloleloView Answer on Stackoverflow
Solution 41 - GitMihai.MeheView Answer on Stackoverflow
Solution 42 - GitDonAristonView Answer on Stackoverflow