忽略已经提交到 Git 存储库的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1139762/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:44:57  来源:igfitidea点击:

Ignore files that have already been committed to a Git repository

gitcachingversion-controlgitignoregit-rm

提问by trobrock

I have an already initialized Git repository that I added a .gitignorefile to. How can I refresh the file index so the files I want ignored get ignored?

我有一个已经初始化的 Git 存储库,我向其中添加了一个.gitignore文件。如何刷新文件索引以便忽略我想要忽略的文件?

回答by trobrock

To untrack a singlefile that has already been added/initialized to your repository, i.e., stop tracking the file but not delete it from your system use: git rm --cached filename

要取消跟踪已添加/初始化到存储库的单个文件,停止跟踪该文件但不从系统中删除它,请使用:git rm --cached filename

To untrack everyfile that is now in your .gitignore:

要取消跟踪现在在您的 中的每个文件.gitignore

First commit any outstanding code changes, and then, run this command:

首先提交所有未完成的代码更改,然后运行以下命令:

git rm -r --cached .

This removes any changed files from the index(staging area), then just run:

这将从索引(暂存区)中删除任何更改的文件,然后运行:

git add .

Commit it:

提交它:

git commit -m ".gitignore is now working"


To undo git rm --cached filename, use git add filename.

要撤消git rm --cached filename,请使用git add filename.

Make sure to commit all your important changes before running git add .Otherwise, you will lose any changes to other files.

确保在运行之前提交所有重要更改,git add .否则,您将丢失对其他文件的任何更改。

回答by dyodji

If you are trying to ignore changes to a file that's already tracked in the repository (e.g. a dev.properties file that you would need to change for your local environment but you would never want to check in these changes) than what you want to do is:

如果您试图忽略对已在存储库中跟踪的文件的更改(例如,您需要为本地环境更改但您永远不想签入这些更改的 dev.properties 文件)而不是您想要做的是:

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

If you wanna start tracking changes again

如果您想再次开始跟踪更改

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

See git-update-index(1) Manual Page.

请参阅git-update-index(1) 手册页

Also have a look at the skip-worktreeand no-skip-worktreeoptions for update-index if you need this to persist past a git-reset (via)

如果您需要它持续超过 git-reset(通过),还可以查看update-index的skip-worktreeno-skip-worktree选项



Update: Since people have been asking, here's a convenient (and updated since commented on below) alias for seeing which files are currently "ignored" (--assume-unchanged) in your local workspace

更新:由于人们一直在问,这里有一个方便的(自下面评论后更新)别名,用于查看本地工作区中当前“忽略”(--assume-unchanged)的文件

$ git config --global alias.ignored = !git ls-files -v | grep "^[[:lower:]]"

回答by pagetribe

To untrack a file that has already been added/initialized to your repository, ie stop tracking the file but not delete it from your system use: git rm --cached filename

要取消跟踪已添加/初始化到存储库的文件,即停止跟踪该文件但不将其从系统中删除,请使用: git rm --cached filename

回答by Antony Stubbs

Yes - .gitignoresystem only ignores files not currently under version control from git.

是 -.gitignore系统仅忽略当前不受 git 版本控制的文件。

I.e. if you've already added a file called test.txtusing git-add, then adding test.txtto .gitignorewill still cause changes to test.txtto be tracked.

即,如果您已经添加了一个名为test.txtusing的文件git-add,那么添加test.txt.gitignore仍然会导致test.txt跟踪更改。

You would have to git rm test.txtfirst and commit that change. Only then will changes to test.txtbe ignored.

您必须git rm test.txt首先提交该更改。只有这样,更改才会test.txt被忽略。

回答by MikeJansen

Remove trailing whitespace in .gitignore

删除 .gitignore 中的尾随空格

Also, make sure you have no trailing whitespace in your .gitignore. I got to this question because I was searching for an answer, then I had a funny feeling I should open the editor instead of just cat'ing .gitignore. Removed a single extra space from the end and poofit works now :)

另外,请确保 .gitignore 中没有尾随空格。我遇到这个问题是因为我正在寻找答案,然后我有一种有趣的感觉,我应该打开编辑器而不是只看 .gitignore。移除了一单到底额外的空间和现在的工作:)

回答by Orlando

i followed these steps

我按照这些步骤

git rm -r --cached .
git add .
git reset HEAD

after that, git delete all files (*.swp in my case) that should be ignoring.

之后,git 删除所有应该忽略的文件(在我的例子中是 *.swp)。

回答by przbadu

If you want to stop tracking file without deleting the file from your local system, which I prefer for ignoring config/database.ymlfile. Simply try:

如果您想停止跟踪文件而不从本地系统中删除文件,我更喜欢忽略config/database.yml文件。只需尝试:

git rm --cached config/database.yml
# this will delete your file from git history but not from your local system.

now, add this file to .gitignorefile and commit the changes. And from now on, any changes made to config/database.yml will not get tracked by git.

现在,将此文件添加到.gitignore文件并提交更改。从现在开始,对 config/database.yml 所做的任何更改都不会被 git 跟踪。

$ echo config/database.yml >> .gitignore

Thanks

谢谢

回答by Ahmad Awais

Complex answers everywhere!

到处都是复杂的答案!

Just use the following

只需使用以下

git rm -r --cached .

It will remove the files you are trying to ignore from the origin and not from the master on your computer!

它将从源中删除您试图忽略的文件,而不是从计算机上的主文件中删除!

After that just commit and push!

之后只需提交并推送!

回答by Mark Salvatore

To remove just a few specific files from being tracked:

要仅从跟踪中删除一些特定文件:

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

If ever you want to start tracking it again:

如果您想再次开始跟踪它:

git update-index --no-assume-unchanged path/to/file                      

回答by Iman Mohamadi

As dav_i says, in order to keep the file in repo and yet removing it from changes without creating an extra commit you can use:

正如 dav_i 所说,为了将文件保留在 repo 中并从更改中删除它而不创建额外的提交,您可以使用:

git update-index --assume-unchanged filename