将忽略的文件保留在 git status 之外

发布于 2024-08-12 15:51:25 字数 389 浏览 4 评论 0 原文

我想阻止 Git 在 git status 中显示被忽略的文件,因为在 已更改但未更新 文件列表中包含大量文档和配置文件,导致列表呈现一半-无用。

Git显示这些文件正常吗?

我将忽略信息放在 Git 存储库根目录下的 .gitignore 文件中,使用 git add 时不会添加它们。 但似乎它们并不完全忽略两者,因为它们显示在上述列表中,并且显示在 git ls-files --others -i --exclude-standard 打印的列表中。只有与 ~/.gitignore 中的模式匹配的文件才会显示在那里。

是不是因为前期我没有忽视它们,所以它们至少犯过一次?

I would like to stop Git from showing ignored files in git status, because having tons of documentation and config files in the list of Changed but not updated files, renders the list half-useless.

Is it normal for Git to show these files?

I put the ignore information in a .gitignore file in the root directory of the Git repository and they are not added when using git add . but it seems that they are not fully ignored either, as they show up in the aforementioned list and do not show up in the list printed by git ls-files --others -i --exclude-standard. Only files matched by the patterns in ~/.gitignore show up there.

Could it be because at an earlier stage I didn't ignore them and they were thus committed at least once?

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

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

发布评论

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

评论(10

巡山小妖精 2024-08-19 15:51:26

我也遇到过这个问题,这可能是因为您在初始提交流程中将被忽略的目录/文件标记为“要跟踪”的 GIT 之后将其添加到 .gitignore 中。

因此,您需要像这样清除 git 跟踪缓存:

git rm --cached -r [文件夹/文件名]

可以在此处阅读更详细的说明:
http://www.frontendjunkie.com/2014 /12/stop-git-from-tracking-changes-to.html

上述命令还从远程 GIT 源中删除了文件夹/文件的残余内容。这样你的 GIT 仓库就变得干净了。

I've had this issue come up as well, it's probably because you added your ignored directory/files to .gitignore after they were marked as "to be tracked" by GIT in an initial commit flow.

So you need to clear the git tracking cache like so:

git rm --cached -r [folder/file name]

A more detailed explanation can be read here:
http://www.frontendjunkie.com/2014/12/stop-git-from-tracking-changes-to.html

The above command also removed the remnants of the folder/files from your remote GIT origin. So your GIT repos become clean.

堇色安年 2024-08-19 15:51:26

问题可能是该文件仍在 git 缓存中。解决这个问题需要重置git缓存。

对于单个文件

git rm --cached <file>

对于整个项目

重置 git 缓存。如果您在未添加 .gitignore 文件时提交了项目,那么这是一个很好的命令。

git rm -r --cached . 

提交更改

git add . 
git commit -m ".gitignore was fixed." 

Problem can be that this file is still in the git cache. To solve this problem needs to reset git cache.

For single file

git rm --cached <file>

For whole project

Reset git cache. It is good command if you committed project when .gitignore file was not added.

git rm -r --cached . 

Commit changes

git add . 
git commit -m ".gitignore was fixed." 
相对绾红妆 2024-08-19 15:51:26

这肯定有效,

git rm --cached -r [文件夹/文件名]

This surely works,

git rm --cached -r [folder/file name]

糖果控 2024-08-19 15:51:26

我假设您不想添加 tmp 目录(Visual Studio 平台)

1- 将以下行添加到本地 .gitignore 文件

## ignore tmp
/tmp/
./tmp/

3- 在本地备份并删除 tmp 文件夹。 (备份到其他地方。例如桌面?)

4- 将更改提交到本地而不是同步到远程(例如 github)。

完成这些步骤后,您的 tmp 目录将不会再次上传。

I assume you want not to add tmp directory (Visual Studio Platform)

1- add lines below to your local .gitignore file

## ignore tmp
/tmp/
./tmp/

3- backup and delete tmp Folder locally. (Backup somewhere else. eg desktop?)

4- Commit Changes to Local than Sync to Remote (eg. github).

After these steps your tmp directory wont be uploaded again.

嘿嘿嘿 2024-08-19 15:51:26

在 .gitignore 文件中进行更改后。
请按照以下简单步骤进行

git rm -r --cached .

Run git add .

git commit -m "Commit message"

检查

git status

after Make changes in .gitignore file.
follow these simple steps

git rm -r --cached .

Run git add .

git commit -m "Commit message"

To check

git status
狼性发作 2024-08-19 15:51:26

来自 man git-lsfiles

-i, --ignored
Show ignored files in the output. Note that this also reverses any exclude list present.

就我个人而言,我倾向于将 doxygen 文件保留在我的源代码树中,因此我只是将其添加到我的 .gitignore (位于我的源代码树的最顶层目录中):

docs/*

希望有帮助。

From man git-lsfiles:

-i, --ignored
Show ignored files in the output. Note that this also reverses any exclude list present.

Personally I tend to keep doxygen files in my source tree, so I simply added this to my .gitignore (which is in the topmost directory of my source tree):

docs/*

Hope that helps.

五里雾 2024-08-19 15:51:26

以下步骤仅适用于未跟踪的文件
此信息适用于以下配置:

Platform: linux

Git version: git version 1.8.3.1

将要忽略的文件列表放入以下位置的“排除”文件中。

<path till .git directory>/.git/info/exclude

“排除”文件的初始内容

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~

“排除”文件的最终内容

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~


*.tar
*.gch

Following steps work for untracked files only.
This info is applicable for following configuration:

Platform: linux

Git version: git version 1.8.3.1

Put list of files to be ignored in "exclude" file present at following location.

<path till .git directory>/.git/info/exclude

Initial content of "exclude" file

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~

Final content of "exclude" file

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~


*.tar
*.gch
花开半夏魅人心 2024-08-19 15:51:26

根据接受的答案,您可以假设文件未更改,这样您在本地计算机上执行的任何操作都不会提交到远程分支。

类似于 git update-index --assume-unchanged src/main/resources/config.properties
其中 src/main.resources/config.properties 是在项目中查找该文件的路径示例。

现在,如果您想从存储库中完全删除文件,您可能需要使用remove cached而不是`git rm --cached

这种情况可能适用于以下情况:

假设我有一个存储密码的文件。我最初提交的文件的详细信息是错误的,只是为了让我的同事在他们的计算机上拥有相同的文件。但是,现在我已在本地计算机上添加了正确的详细信息。如何防止使用现在正确的密码详细信息再次意外提交该文件?

As per the accepted answer, you can assume the file as unchanged, so that whatever you do on your local machine is not commited to your remote branch.

Something like git update-index --assume-unchanged src/main/resources/config.properties
where src/main.resources/config.properties is a sample for the path to find that file within your project.

Now, if you want to remove the file completely from your repository, you may want to use remove cached instead as of `git rm --cached

This scenario could be applicable on the following situation:

Let's suppose I have a file where I store passwords. I've initially commited that file with wrong details, just so my colleagues have that same file on their machines. However, now I've added my correct details on my local machine. How do I prevent that file from being accidentally committed again with the now correct password details?

小清晰的声音 2024-08-19 15:51:26

像这样

git --ignored myfolder

并且只会显示 myfolder 的状态

like this

git --ignored myfolder

And will show status only for myfolder

旧伤还要旧人安 2024-08-19 15:51:25

正如我发现在这篇文章中,.gitignore 仅适用于未跟踪的文件。如果您将文件添加到存储库,您可以:

git update-index --assume-unchanged <file>

或通过

git rm --cached <file>

编辑

从存储库中删除它们这篇文章也解释了这一点

As I found in this post, .gitignore only works for untracked files. If you added files to repository, you can:

git update-index --assume-unchanged <file>

or remove them from repository by

git rm --cached <file>

Edit

This article explains that too

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