首页 文章

如何创建.gitignore文件

提问于
浏览
880

我需要为我的 .gitignore 文件添加一些规则,但是,我可以通过Xcode自动创建它吗?如果没有,什么命令允许我创建一个?

30 回答

  • 83

    如果您使用的是Windows,则不允许您在Windows资源管理器中创建没有文件名的文件 . 如果您尝试将文本文件重命名为 .gitignore ,它将为您提供错误“您必须键入文件名”

    enter image description here

    为了解决这个问题,我使用了以下步骤

    • 创建文本文件gitignore.txt

    • 在文本编辑器中打开它并添加规则,然后保存并关闭

    • 按住SHIFT键,右键单击您所在的文件夹,然后选择 Open command window here

    • 然后使用 ren gitignore.txt .gitignore 在命令行中重命名该文件

    或者@HenningCash在评论中建议

    您可以通过在没有扩展名的文件名后附加一个点来解决此Windows资源管理器错误:.gitignore . 将自动更改为.gitignore

  • 12

    事情可以(有时)简单:只需将以下内容添加到首选命令行界面(GNU Bash,Git Bash等)中

    touch .gitignore
    

    testmenow

    更新:使用Windows CMD测试,它在Win 8.1上不起作用 . 不过它可以使用Git Bash,如果你使用的是Git,你应该在Windows中安装它 .

    Update: 正如@Wardy在评论中指出的那样, touch 可以在Windows上运行,只要你提供完整的路径 . 这也可以解释为什么它对Windows上的某些用户不起作用: touch 命令似乎不是默认情况下某些Win版本的 $PATH .

    C:\> "c:\program files (x86)\git\bin\touch.exe" .gitignore
    
  • 3

    在Windows资源管理器中创建.gitignore文件的最简单方法是创建一个名为 .gitignore. 的新文件 . 这将跳过具有文件扩展名的验证,因为实际上具有空文件扩展名 .

  • 19

    默认情况下,.gitignore文件未添加到存储库 . 使用vi或您喜欢的文本编辑器创建.gitignore文件,然后发出 git add .gitignore ,然后发出 git commit -m "message" .gitignore . 以下命令将处理它 .

    > .gitignore
    git add .gitignore
    git commit -m "message" .gitignore
    
  • 3

    ==========在WINDOWS中==========

    • 打开记事本 .

    • 添加gitignore文件的内容 .

    • 单击"Save as"并选择"all files" .

    • 另存为 .gitignore .

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

  • 7

    在Windows上,您可以使用cmd echo "" >.gitignore

    或者使用Git Bash cmd touch .gitignore ,这对Linux和Mac系统很有用

  • 6

    MacOS / Linux单线程

    一个简单的方法来获取默认的git忽略而不会搞乱创建/复制/粘贴是使用终端的 curl 命令 . 首先 cd 进入项目根目录,然后通过从以下两个来源之一替换 MY_API_NAME 和您的API名称来运行该命令:

    gitignore.io

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

    您可以通过从列表here中搜索并单击“生成”来查找API名称 .

    Java示例:

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

    GitHub

    或者你可以使用GitHub上的那些 . 找到API here的文件名 .

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

    Java示例:

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

    Windows

    以下是Windows的一些类似替代方案 .

    但老实说,设置它看起来更麻烦,值得 . 如果我有Windows,那么我只需在项目的根文件夹中创建一个名为 .gitignore 的空文件,然后从gitignore.io或GitHub复制并粘贴默认文本 .

  • 10

    使用Git Bash控制台 .

    • 导航到你的项目-Type“touch .gitignore”

    将为您创建.gitignore文件 .

    enter image description here

  • 13

    Windows下的一个不错的提示:

    • 右键单击Windows资源管理器,新建>文本文档

    • 将其命名为.gitignore . (带尾点 - 即提示)

    • 你最终得到一个.gitignore文件:)

    在Windows 7和8下测试 .

    本技巧假定您的Windows资源管理器显示文件扩展名 .

    Windows Explorer .gitignore

  • 8

    我也想要我的贡献 . 这一次,动画一个:)

    VIM(迷你教程):

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

    enter image description here

  • 264

    我的贡献是针对mac上的那些,它不仅适用于那些在iOS项目上工作的人(如提到Xcode的问题所暗示的那样),而且适用于任何类型的项目 .


    我这样做的简单方法是进入终端并运行 vim .gitignore 然后添加文件 . 通常你可以在Github的一个模板上复制你需要的东西https://github.com/github/gitignore

    Step 1
    在你的项目运行中

    vim .gitnore

    enter image description here

    Step 2
    您现在使用vim打开文件 .

    enter image description here

    i 插入文字 . 当您在底部看到 --INSERT-- 时,您将看到文件已准备就绪 .

    enter image description here

    Step 3 (option 1)
    对于Objective-C项目,您可以从https://raw.githubusercontent.com/github/gitignore/master/Objective-C.gitignore复制并将其粘贴到 .gitignore

    enter image description here

    然后按 esc 然后键入 :wq ,然后按 return . 哪个保存文件 .

    Step 3 (option 2)
    添加适用于您的项目的任何文件 .

    如果您不确定要添加什么,在搜索引擎中使用的最佳关键字将包括您的项目类型和文本编辑器 . 例如,如果你使用Sublime,你会想要补充

    *.sublime-workspace
    

    如果您正在使用Dreamweaver中的Cordova项目,则需要添加

    _notes
    dwsync.xml
    
  • 12

    在windows中,打开dos提示符(cmd)窗口,使用命令行:

    type > .gitignore
    
  • 31

    我有另一个简单的想法
    我们在cmd中使用echo命令,

    echo ./idea > .gitignore

    这将创建具有文本内容的.gitignore文件"./idea"
    您现在可以使用文本编辑器手动更改文件中的数据 .

    或简单地说

    安慰 :

    echo .gitignore notepad.exe

    立即编辑gitignore .

    如果您不知道哪些文件应该为您的IDE或操作系统进行gitignored,请转到www.gitignore.io

    gitignore.io - 这里它会生成gitignore命令或文本,只需说你的api或os就是这样!只需复制并粘贴到您的文件中即可 . 简单!

  • 13

    这是我个人的最爱,http://help.github.com/ignore-files/

    另外,如果您想忽略xcode文件,请参阅此答案https://stackoverflow.com/a/49488/126235

  • 10

    创建一个.gitignore文件,包括您不想提交的所有文件和目录 .

    例如:

    #################
    ## 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
    
  • 34

    http://gitignore.io 是一个开源实用程序,可以帮助您为项目创建有用的.gitignore文件 . 您还可以通过 gi 命令访问命令行API:http://gitignore.io/cli

    • OSX 安装 gi 命令:

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

    $ gi xcode,osx

    • 如果要将结果附加到新的.gitignore文件,您应该在终端上看到输出 .

    $ gi xcode,osx >> .gitignore

  • 2

    使用cmd创建.gitignore的几种方法:

    • With copy con command:

    • 打开 cmd 并向您的git存储库说 cd

    • copy con .gitignore 并按 Ctrl+Z .

    enter image description here

    • With start notepad .gitignore command:

    • 打开 cmd 并对您的git存储库说 cd

    • start notepad .gitignore 并在打开的记事本对话框中按 Yes 按钮 .

    enter image description here

    • With edit .gitignore command (Windows x86 only):

    • 打开 cmd 并对您的git存储库说 cd

    • edit .gitignore 并关闭已打开的编辑编辑器 .

  • 419

    以下适用于Powershell和命令提示符

    echo '*.ignore_me' > .gitignore
    

    我遇到了一个奇怪的问题,git实际上无法读取.gitignore文件 . 然后我删除了.gitignore文件并使用vim创建了一个工作正常 .

    要添加要忽略的其他文件,只需调用以下命令:

    echo 'another_file_to_ignore' >> .gitignore
    

    它会将更多文件附加到现有的.gitignore文件中 .

  • 3

    1)创建一个.gitignore文件,这样做,你只需创建一个.txt文件并按如下方式更改扩展名:
    enter image description here

    那么你必须改变在cmd上写下面一行的名称:

    rename git.txt .gitignore
    

    其中git.txt是您刚刚创建的文件的名称 .

    然后,您可以打开该文件并写入您不想在存储库中添加的所有文件 . 例如我的样子如下:

    #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
    ~$*
    

    一旦你有了这个,你需要将它添加到您的git存储库 . 您必须将文件保存到存储库所在的位置 .

    然后在你的git bash中你必须写下以下行:

    enter image description here

    如果存储库已存在,则必须执行以下操作:

    1)git rm -r --cached . 2)git add . 3)git commit -m“.gitignore正在工作”

    如果第2步不起作用,那么您应该编写要添加的文件的孔路径 .

    希望能帮助到你!

  • 2

    如果你没有't want to have your .gitignore interfere with anyone else'存储库,你也可以使用 .git/info/exclude . (见http://help.github.com/ignore-files/

  • 6
    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**
    

    通过在步骤3中运行该命令,您将获得项目目录中的.gitignore文件 . 谢谢 .

  • 8

    不使用命令行

    • 打开texteditor并添加规则 .

    • 单击文件 - >另存为

    • 另存为".gitignore"(包括引文)

  • 4
    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.
    

    然后运行killall Finder并退出终端 .

  • 153

    在工作中我们使用的是Windows XP,并且在文件名末尾键入句点不起作用 . 在没有“您必须键入文件名”错误的情况下创建.gitignore文件的快速简便方法是:

    • 打开cmd窗口并键入"edit .gitignore" .

    • 类型“Alt(选择文件菜单),F,S . 现在,只要cmd提示符指向,就会有一个空的.gitignore文件 .

    您现在可以使用您喜欢的文本编辑器填充它

  • 6

    有一种非常简单的方法来创建.gitignore文件 . 这个是在github中创建的,我很确定大多数源代码控件都提供了自己创建文件的功能 . 按图像教程附加图像以供参考 .

    enter image description here

    enter image description here

    enter image description here

    enter image description here

  • 23

    要在Xcode中添加任何文件,请转到菜单并导航到文件 - >新建 - >文件...

    对于.gitignore文件,选择Other - > Empty并单击Next . 在“另存为”字段中键入名称(.gitignore),然后单击“创建” . 对于以点(“ . ”)开头的文件,将弹出一条警告消息,告知您该文件将被隐藏 . 只需点击“使用” . 继续...

    就这样 .

    要填写您的全新.gitignore,您可以在此处找到忽略Xcode文件的示例:https://stackoverflow.com/a/12021580/2120348

  • 39

    如果您使用Sublime作为您的IDE,您可以创建一个新文件并保存它作为.gitignore . 只需使用ctrl N作为新文件,然后按ctrl S保存为“.gitignore” .

  • 1443

    在mac中 - 您只需创建一个新的文本文件即可 . 使用https://www.gitignore.io/添加内容

    将文件格式保存为 - 带附件的富文本文档 . 将文件名更改为.gitingore,并在附加图像中弹出时选择 use"." .

    注意:因为它是一个隐藏文件,所以你将无法在目录中看到它 . 但它会被创造出来 .

    see image

  • 4

    我用记事本 . 新文件> SaveAs> .gitignore(另存为类型 - >所有类型( . ))

  • 26

    您可以在Windows Powershell中键入 new-item .gitignore .

相关问题