在Git中将新创建的特定文件夹添加到.gitignore

时间:2009-08-26 14:52:58

标签: git gitignore

我有一个干净的工作目录,并在昨晚从Git回购中获得了一个克隆。 但现在我的本地服务器已创建并包含一个我想忽略的stats文件夹。

当我运行git状态时,我似乎无法让Git忽略此文件夹。

On branch master
Your branch is ahead of 'origin/master' by 1 commit.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file: app_public/views/pages/privacy.php
    new file: app_public/views/pages/terms.php
    new file: public_html/stats/ctry_usage_200908.png
    new file: public_html/stats/daily_usage_200908.png
    new file: public_html/stats/dns_cache.db
    new file: public_html/stats/hourly_usage_200908.png
    new file: public_html/stats/index.html
    new file: public_html/stats/usage.png
    new file: public_html/stats/usage_200908.html
    new file: public_html/stats/webalizer.current
    new file: public_html/stats/webalizer.hist

Changed but not updated:
    modified: .gitignore

我在.gitignore中添加了一些不同的行,但它仍然试图添加它们:

public_html/stats
public_html/stats/**
public_html/stats/**/*
public_html/stats/*

5 个答案:

答案 0 :(得分:88)

试试/public_html/stats/*

但由于git status中的文件报告为要提交,这意味着您已经手动添加了这些文件。当然,在这种情况下,忽略它有点太晚了。你可以git rm --cache他们(IIRC)。

答案 1 :(得分:74)

为此有两种情况

案例1: 文件已添加到git repo。

案例2: 使用时,新创建的文件及其状态仍显示为未跟踪文件

git status

如果你有案例1:

第1步: 然后运行

git rm --cached filename 

将其从git repo cache中删除

如果是目录,请使用

git rm -r --cached  directory_name

第2步:如果案例1 结束,那么在你的git repo中创建一个名为.gitignore的新文件

第3步:使用以下命令告诉git忽略/假设文件不变

git update-index --assume-unchanged path/to/file.txt

第4步:现在,在编辑器nano,vim,geany等中使用git status open .gitignore检查状态...任何一个,添加文件/文件夹的路径到忽视。如果是文件夹,则用户folder_name/*忽略所有文件。

如果您仍然不明白,请阅读article git ignore file链接。

答案 2 :(得分:14)

从“git help ignore”我们学习:

  

如果图案以斜线结尾,则为   被删除的目的   以下描述,但它会   只找到与目录匹配。在   换句话说,foo /将匹配a   目录foo和它下面的路径,   但不会匹配常规文件或   符号链接foo(这是一致的   同              在git中,一般路径规则的工作方式。

因此您需要的是

  

的public_html /统计/

答案 3 :(得分:5)

这是/public_html/stats/*

$ ~/myrepo> ls public_html/stats/
bar baz foo
$ ~/myrepo> cat .gitignore 
public_html/stats/*
$ ~/myrepo> git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
nothing added to commit but untracked files present (use "git add" to track)
$ ~/myrepo>

答案 4 :(得分:1)

我非常懒惰。我只是做了一个搜索希望找到这个问题的快捷方式,但没有得到答案,所以我敲了这个。

〜/ bin / IGNORE_ALL

#!/bin/bash
# Usage: IGNORE_ALL <commit message>                                                                                                                             
git status --porcelain | grep '^??' | cut -f2 -d' ' >> .gitignore              
git commit -m "$*" .gitignore 

EG:IGNORE_ALL添加了stat忽略

这只会将所有忽略文件附加到.gitignore并提交。请注意,您可能希望在之后向文件添加注释。