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

发布于 2024-08-03 04:48:32 字数 1034 浏览 4 评论 0原文

我有一个干净的工作目录,并于昨晚从 Git 存储库中引入了一个克隆。 但现在我的本地服务器创建并包含一个我想忽略的统计文件夹。

当我运行 git status 时,我似乎无法让 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/*

I had a clean working directory and brought in a clone from a Git repo last night.
But now my local server created and contains a stats folder which I want to ignore.

I can't seem to get Git to ignore this folder when I run a git status.

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

I added in my .gitignore a few different lines but it still trying to add them:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ι不睡觉的鱼゛ 2024-08-10 04:48:32

对于这种情况,有两种情况

情况 1: 文件已添加到 git 存储库。

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

git status

如果您有情况 1:

步骤 1:
然后运行

git rm --cached filename 

将其从 git repo 缓存中删除

(如果它是一个目录),然后使用

git rm -r --cached  directory_name

步骤 2: 如果情况 1 结束,则创建名为 .gitignore 的新文件code> 在 git 存储库中

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

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

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

如果您仍然不明白,请阅读文章 git 忽略文件 链接。

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.

秋千易 2024-08-10 04:48:32

尝试 /public_html/stats/* 吗?

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

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).

ヤ经典坏疍 2024-08-10 04:48:32

从“git helpignore”中我们了解到:

如果模式以斜杠结尾,则
被删除的目的是
以下描述,但它会
只查找与目录匹配的内容。在
换句话说, foo/ 将匹配 a
目录 foo 及其下面的路径,
但不会匹配常规文件或
符号链接 foo (这是一致的

pathspec 在 git 中的一般工作方式)。

因此你需要的是

public_html/统计/

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/

┾廆蒐ゝ 2024-08-10 04:48:32

它是/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>

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>
愛上了 2024-08-10 04:48:32

我实在是太懒了。我只是做了一个搜索,希望找到解决这个问题的捷径,但没有得到答案,所以我把它敲了出来。

~/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 添加了统计忽略

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文