强制 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
Force git to update .gitignore
提问by Shravan40
I have a .gitignore
file, and it's ignoring some files. I have updated the .gitignore
file (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 .gitignore
is updated or deleted).
我试过这个问题,仍然没有跟踪我的所有文件(根据我更新的.gitignore
)。(简单地说,一旦.gitignore
更新或删除,我如何强制 git 收回文件)。
采纳答案by Saahithyan Vigneswaran
If you want to add all files, delete all filenames from .gitignore
file, not the .gitignore
file and commit
it, 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 .gitignore
file and add this line !*.*
, then add
and commit
.
您可以尝试一个简单的 hack,它可能会也可能不会起作用。从.gitignore
文件中删除所有文件名并添加这一行!*.*
,然后add
和commit
。
UPDATE
更新
Simple, I'll explain with an example. Say you have a build
folder which is already added and tracked by git. Now you decide not to track this folder.
很简单,我会用一个例子来解释。假设您有一个build
已由 git 添加和跟踪的文件夹。现在您决定不跟踪此文件夹。
- Add this folder (
build
) to.gitignore
- Delete
build
folder - Commit your changes
- 将此文件夹 (
build
)添加到.gitignore
- 删除
build
文件夹 - 提交您的更改
From now on git will not track build
folder.
从现在开始 git 将不会跟踪build
文件夹。
回答by Shravan40
You will have to clear the existing git cache
first.
您必须先清除现有的 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 filesgit add <file_name.ext>
// To add specific filegit 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.txt
to 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.txt
to untrack it (as Tim mentioned in his comment). Perhaps commit that untracked state first and then add it in to your .gitignore
on 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
在下一次提交时将其添加到您的状态中。过去,当我尝试在同一个提交中忽略和删除时,我看到了一些时髦的行为。