zoukankan      html  css  js  c++  java
  • git add和被ignore的文件

    如果有如下的目录结构:

                    workspace tree

            |

             ---------------------

       |                             |

     hello.c                           d

     hello.o                           |

     .gitignore                     say.c

                 say.o

                 .gitignore

    在workspace tree的跟目录下有3个文件hello.c,hello.o,.gitignore以及一个文件夹d,其中根目录下的.gitignore文件忽略.o文件。在文件夹d下也有3个文件,say.c, say.o,.gitignore,其中文件夹d下的.gitignore不忽略.o文件。hello.o和say.o都是刚刚编译出来的文件。此时,如果在根目录下运行

    git add *

    则会报错:

    The following paths are ignored by one of your .gitignore files:
    hello.o
    Use -f if you really want to add them
    fatal:no files add

    而如果运行:

    git add *

    则可以将d/say.o成功的添加到index中。

    通过git add --help查询发现:

    1) git add 后面跟的文件有被忽略的文件,那么操作失败;

    2) git add 通过遍历目录遇到了要被忽略的文件,此文件不会被加入到index

    3) git add 遇到由git展开glob(不是由shell展开)形成的要被忽略的文件,此文件不会被加入到index

    因此,在上面的情形中,直接运行git add *是进行的shell 展开glob,此时相当于运行git add hello.o(由于shell 展开glob无法cross目录,因此没有匹配d/say.o),而hello.o是要被忽略的文件,所以操作失败;如果运行git add *,*由git展开,可以匹配hello.o,d/say.o,根据上面的原则3),hello.o不会被加入到index中,因此此时相当于运行git add d/say.o,所以操作得以正确进行。

  • 相关阅读:
    Codeforces 1265A Beautiful String
    1039 Course List for Student (25)
    1038 Recover the Smallest Number (30)
    1037 Magic Coupon (25)
    1024 Palindromic Number (25)
    1051 Pop Sequence (25)
    1019 General Palindromic Number (20)
    1031 Hello World for U (20)
    1012 The Best Rank (25)
    1011 World Cup Betting (20)
  • 原文地址:https://www.cnblogs.com/chaoguo1234/p/5323649.html
Copyright © 2011-2022 走看看