在 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

我在我的. gitigore 中添加了几行不同的代码,但它仍在尝试添加它们:

public_html/stats
public_html/stats/**
public_html/stats/**/*
public_html/stats/*
185010 次浏览

Try /public_html/stats/* ?

But since the files in git status reported as to be commited that means you've already added them manually. In which case, of course, it's a bit too late to ignore. You can git rm --cache them (IIRC).

From "git help ignore" we learn:

If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in git).

Therefore what you need is

public_html/stats/

It's /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>

I'm incredibly lazy. I just did a search hoping to find a shortcut to this problem but didn't get an answer so I knocked this up.

~/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 added stat ignores

This will just append all the ignore files to your .gitignore and commit. note you might want to add annotations to the file afterwards.

For this there are two cases

Case 1: File already added to git repo.

Case 2: File newly created and its status still showing as untracked file when using

git status

If you have case 1:

STEP 1: Then run

git rm --cached filename

to remove it from git repo cache

if it is a directory then use

git rm -r --cached  directory_name

STEP 2: If Case 1 is over then create new file named .gitignore in your git repo

STEP 3: Use following to tell git to ignore / assume file is unchanged

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

STEP 4: Now, check status using git status open .gitignore in your editor nano, vim, geany etc... any one, add the path of the file / folder to ignore. If it is a folder then user folder_name/* to ignore all file.

If you still do not understand read the article git ignore file link.