将新创建的特定文件夹添加到 Git 中的 .gitignore
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1335302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add newly created specific folder to .gitignore in Git
提问by Lee
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.
我有一个干净的工作目录,并在昨晚从 Git 存储库中引入了一个克隆。但是现在我的本地服务器创建并包含一个我想忽略的 stats 文件夹。
I can't seem to get Git to ignore this folder when I run a git status.
当我运行 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
I added in my .gitignore a few different lines but it still trying to add them:
我在 .gitignore 中添加了几行不同的行,但它仍在尝试添加它们:
public_html/stats
public_html/stats/**
public_html/stats/**/*
public_html/stats/*
回答by Michael Krelin - hacker
Try /public_html/stats/*
?
试试/public_html/stats/*
?
But since the files in git status
reported as to be commitedthat 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).
但是由于git status
报告中的文件已提交,这意味着您已经手动添加了它们。在这种情况下,当然,忽略有点太晚了。你可以git rm --cache
(IIRC)。
回答by Kshitiz
For this there are two cases
为此,有两种情况
Case 1:File already added to git repo.
案例 1:文件已添加到 git repo。
Case 2:File newly created and its status still showing as untracked file when using
情况二:新创建的文件,使用时其状态仍显示为未跟踪文件
git status
If you have case 1:
如果您有案例 1:
STEP 1:Then run
第 1 步:然后运行
git rm --cached filename
to remove it from git repo cache
从 git repo 缓存中删除它
if it is a directory then use
如果它是一个目录然后使用
git rm -r --cached directory_name
STEP 2:If Case 1is over then create new file named .gitignore
in your git repo
第 2 步:如果案例 1结束,则.gitignore
在您的 git 存储库中创建新文件
STEP 3:Use following to tell git to ignore / assume file is unchanged
第 3步:使用以下命令告诉 git 忽略/假设文件未更改
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.
第 4 步:现在,.gitignore
在您的编辑器 nano、vim、geany 等中使用 git status open 检查状态...任何一个,添加要忽略的文件/文件夹的路径。如果是文件夹,则用户folder_name/*
忽略所有文件。
If you still do not understand read the article git ignore filelink.
如果您仍然不明白,请阅读文章 git ignore filelink。
回答by kajaco
From "git help ignore" we learn:
从“git help ignore”我们了解到:
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).
如果模式以斜杠结尾,则出于以下描述的目的将其删除,但它只会找到与目录的匹配项。换句话说, foo/ 将匹配目录 foo 和它下面的路径,但不会匹配常规文件或符号链接 foo (这与 pathspec 通常在 git 中的工作方式一致)。
Therefore what you need is
因此你需要的是
public_html/stats/
public_html/统计/
回答by August Lilleaas
It's /public_html/stats/*
.
它是/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>
回答by michael
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/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
EG:IGNORE_ALL 添加统计忽略
This will just append all the ignore files to your .gitignore and commit. note you might want to add annotations to the file afterwards.
这只会将所有忽略文件附加到您的 .gitignore 并提交。请注意,您可能希望之后向文件添加注释。