强制 git 更新 .gitignore

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

Force git to update .gitignore

gitgitignore

提问by Shravan40

I have a .gitignorefile, and it's ignoring some files. I have updated the .gitignorefile (removed some filenames and added some filenames). This is not reflected in git status. How can I force git to update these changes, so that track files which are not tracked before and vice versa.

我有一个.gitignore文件,它忽略了一些文件。我已经更新了.gitignore文件(删除了一些文件名并添加了一些文件名)。这在git status. 如何强制 git 更新这些更改,以便跟踪以前未跟踪的文件,反之亦然。

I have tried this question, still all of my files are not tracked (according to my updated .gitignore). (In simple, how can I force git to retract files once .gitignoreis updated or deleted).

我试过这个问题,仍然没有跟踪我的所有文件(根据我更新的.gitignore)。(简单地说,一旦.gitignore更新或删除,我如何强制 git 收回文件)。

采纳答案by Saahithyan Vigneswaran

If you want to add all files, delete all filenames from .gitignorefile, not the .gitignorefile and commitit, then try

如果要添加所有文件,请从.gitignore文件中删除所有文件名,而不是.gitignore文件和commit它,然后尝试

git config --global core.excludesfile ~/.gitignore_global

git config --global core.excludesfile ~/.gitignore_global

Some files are ignored by the git depending on the OS (like .dll in windows). For more information.

某些文件会被 git 忽略,具体取决于操作系统(如 Windows 中的 .dll)。欲了解更多信息

Now

现在

git add .

git status

git commit -m "your message"

Or

或者

You can try a simple hack, it may or may not work. Delete all filenames from .gitignorefile and add this line !*.*, then addand commit.

您可以尝试一个简单的 hack,它可能会也可能不会起作用。从.gitignore文件中删除所有文件名并添加这一行!*.*,然后addcommit

UPDATE

更新

Simple, I'll explain with an example. Say you have a buildfolder which is already added and tracked by git. Now you decide not to track this folder.

很简单,我会用一个例子来解释。假设您有一个build已由 git 添加和跟踪的文件夹。现在您决定不跟踪此文件夹。

  1. Add this folder (build) to .gitignore
  2. Delete buildfolder
  3. Commit your changes
  1. 将此文件夹 ( build)添加到.gitignore
  2. 删除build文件夹
  3. 提交您的更改

From now on git will not track buildfolder.

从现在开始 git 将不会跟踪build文件夹。

回答by Shravan40

You will have to clear the existing git cachefirst.

您必须先清除现有的 git cache

Remove the cache of all the files

删除所有文件的缓存

  • git rm -r --cached .
  • git rm -r --cached .

Remove the cache of specific file

删除特定文件的缓存

  • git rm -r --cached <file_name.ext>
  • git rm -r --cached <file_name.ext>

Once you clear the existing cache, add/stage file/files in the current directory and commit

清除现有缓存后,在当前目录中添加/暂存文件/文件并提交

  • git add .// To add all the files
  • git add <file_name.ext>// To add specific file
  • git commit -m "Suitable Message"
  • git add .// 添加所有文件
  • git add <file_name.ext>// 添加特定文件
  • git commit -m "Suitable Message"

As pointed out by Scott Biggs in comment that "This works for both adding a file that was once ignored as well as ignoring a file that was once tracked"

正如 Scott Biggs 在评论中指出的那样,“这适用于添加曾经被忽略的文件以及忽略曾经被跟踪的文件”

回答by Rajdeep Gautam

You can resolve this issue by deleting the cache of those specific files where you are getting this issue. The issue you mentioned occurs when you have committed some specific files once and then you are adding them in .gitignorelater.

您可以通过删除出现此问题的特定文件的缓存来解决此问题。当您提交了一些特定文件然后稍后将它们添加到.gitignore时,就会出现您提到的问题。

git rm -r --cached <your_file.extension>
git commit -am "Suitable Message"

Same solution is proposed by @sharvan40 above, but you don't need to remove the cache for all the files. It creates a new commit for all your files.

上面@sharvan40 提出了相同的解决方案,但您不需要删除所有文件的缓存。它为您的所有文件创建一个新的提交。

回答by Najathi

It works

有用

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

//犯罪

git commit -m "Atualizando .gitignore para..."

回答by Vidur

Assuming here that your current working directory is empty.

假设您当前的工作目录为空。

You can check what files git is currently tracking by using git ls-files. If you have a lot of files, you can use git ls-files | grep hello.txtto find if git is tracking that specific file.

您可以使用 .gitignore 来检查 git 当前正在跟踪哪些文件git ls-files。如果您有很多文件,您可以使用它git ls-files | grep hello.txt来查找 git 是否正在跟踪该特定文件。

If it is tracking it, then use git rm hello.txtto untrack it (as Tim mentioned in his comment). Perhaps commit that untracked state first and then add it in to your .gitignoreon your next commit. I have seen some funky behavior in the past when trying to ignore and remove in the same commit.

如果它正在跟踪它,则使用git rm hello.txt它来取消跟踪(正如 Tim 在他的评论中提到的那样)。也许先提交未跟踪的状态,然后.gitignore在下一次提交时将其添加到您的状态中。过去,当我尝试在同一个提交中忽略和删除时,我看到了一些时髦的行为。