代码之家  ›  专栏  ›  技术社区  ›  g5wx

Git-创建存储库并只跟踪新文件

git
  •  1
  • g5wx  · 技术社区  · 7 年前

    只有 初始化后新添加的文件/文件夹,忽略旧文件/文件夹。例如:

    FolderA/
    FolderB/
    file1.php
    file2.php
    

    初始化新回购并创建新文件和目录后:

    FolderA/  // old, ignore
    FolderB/  // old, ignore
    FolderC/  // new, track
    file1.php  // old, ignore
    file2.php  // old, ignore
    file3.php  // new, track
    
    4 回复  |  直到 7 年前
        1
  •  3
  •   Jörg W Mittag    7 年前

    Git只会跟踪你 告诉它使用 git add . 因此,您不需要做什么:只是不要告诉Git跟踪您不希望它跟踪的文件。

    如果你看看 git status ,然后将列出所有未跟踪的文件。

    对此也有一个简单的解决方案:忽略 全部的 文件夹:

    echo '*' > .gitignore
    

    git add -f (在 -f

    如果你想用 git add . git add somedirectory 若要添加所有新文件,则需要单独列出要在中忽略的所有文件 .gitignore . 你可以用我上面描述的“恼人的”事实,即 git状态 将列出所有未跟踪的文件:

    git status --porcelain -uall | grep -E '^\?\? ' | cut -d ' ' -f2- > .gitignore
    

    --porcelain 指定的输出格式 采用“瓷v1”格式。瓷器输出格式保证 从未 -uall git状态 列出 未跟踪的文件,否则仅在目录中的所有文件都未跟踪时列出目录名。

    这个 grep ?? 这就是未跟踪的文件是如何列出的。这个 cut 然后在 ?? ,即文件名。

    注意:这将以一个名为 abc\n?? def.txt -z 标记到 它打印由ASCII-NUL分隔的记录,而不是换行符。

        2
  •  0
  •   Shadab Shamsi    7 年前

    如果git只忽略少量文件,则可以使用 gitignore 要创建简单gitignore,请执行以下步骤:

    1. .gitignore
    2. 列出所有你想被git忽略的文件,在每一行分别列出每个文件的名称。 如。

      福尔德拉 叶德布 文件1.php

    吉特忽略 参考:

    您也可以使用诸如 https://www.gitignore.io/ 吉特忽略 给你归档。

    希望这有帮助。

        3
  •  0
  •   Michael H.    7 年前

    git init

    cd /path/to/directory/
    touch .gitignore # creates file .gitignore if not yet existing
    for file in *; do
        echo $file >> .gitignore
    done # appends all the filenames of files in directory to file .gitignore
    git init # inits the repository
    

    在repo初始化之后,您仍然需要添加文件(包括 .gitignore )在提交之前。

        4
  •  -1
  •   g5wx    7 年前

    我最终排除了所有的文件/文件夹,只包含了特定的文件/文件夹。以下是我所做的:

    1. 创建了一个空的 .gitignore 目录根目录中的文件并添加 *
    2. 已初始化该目录中的新存储库
    3. 已编辑 .gitignore 通过添加特定的文件和文件夹从这一点开始跟踪

    我的final.gitignore现在使用以下模式:

    # Ignore everything
    *
    
    # Include folders
    !/folderC
    !/folderD
    
    # Include files
    !file3.php
    !file4.php
    
    # Include files containing
    !*foo*
    

    Jörg W Mittag Michael H.

    其他SO用户也提供了一些有用的资源:

    Make .gitignore ignore everything except a few files

    How do I tell Git to ignore everything except a subdirectory?

    git ignore filenames which contain