How to create .gitignore file

2019-01-03 11:23发布

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?

30条回答
萌系小妹纸
2楼-- · 2019-01-03 11:37

My contribution is aimed at those on a mac, 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 run

vim .gitnore

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

enter image description here

Then press esc then type in :wq then 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 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
查看更多
The star\"
3楼-- · 2019-01-03 11:38

To add any file in Xcode go to the menu and navigate to File -> New -> File...

For a .gitignore file choose Other -> Empty 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/a/12021580/2120348

查看更多
孤傲高冷的网名
4楼-- · 2019-01-03 11:41

The following works in Powershell and Command Prompt

echo '*.ignore_me' > .gitignore

I ran into a weird issue where git effectively wouldn't read the .gitignore file. I then deleted .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.

查看更多
看我几分像从前
5楼-- · 2019-01-03 11:42

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
查看更多
来,给爷笑一个
6楼-- · 2019-01-03 11:42

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

type > .gitignore
查看更多
混吃等死
7楼-- · 2019-01-03 11:42

Without using command line

  1. Open texteditor and add your rules.
  2. Click File->Save As
  3. Save it as ".gitignore" (include the quotations)
查看更多
登录 后发表回答