git 在已经跟踪大量文件的现有存储库上应用 .gitignore
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19663093/
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
Apply .gitignore on an existing repository already tracking large number of files
提问by Tohid
I have an existing Visual Studio project in my repository. I recently added a .gitignore file under my project and I assume that tells Git to ignore the files listed in the file.
我的存储库中有一个现有的 Visual Studio 项目。我最近在我的项目下添加了一个 .gitignore 文件,我假设它告诉 Git 忽略文件中列出的文件。
My problem is that all those files are already being tracked and as far as I know Git will not ignore a file that was already tracked before a rule was added to this file to ignore it.
我的问题是所有这些文件都已经被跟踪,据我所知,Git 不会忽略在将规则添加到该文件以忽略它之前已经跟踪的文件。
It was suggested to use: git rm --cached
and manually un-track them but that's going to take me forever to go through them one by one.
建议使用:git rm --cached
并手动取消跟踪它们,但这将花费我永远的时间来一一浏览它们。
I thought about deleting the repository and recreating it again but this time with .gitignore file present, but there must be a better way to do this.
我想删除存储库并重新创建它,但这次存在 .gitignore 文件,但必须有更好的方法来做到这一点。
回答by Tohid
回答by SomeAnonymousPerson
Create a .gitignore file, so to do that, you just create any blank .txt file.
Then you have to change its name writing the following line on the cmd (where
git.txt
is the name of the file you've just created):rename git.txt .gitignore
Then you can open the file and write all the untracked files you want to ignore for good. For example, mine looks like this:
创建一个 .gitignore 文件,为此,您只需创建任何空白的 .txt 文件。
然后您必须更改其名称,在 cmd 上写入以下行(
git.txt
您刚刚创建的文件的名称在哪里):rename git.txt .gitignore
然后,您可以打开该文件并写入要永久忽略的所有未跟踪文件。例如,我的看起来像这样:
```
``
OS junk files
[Tt]humbs.db
*.DS_Store
#Visual Studio files
*.[Oo]bj
*.user
*.aps
*.pch
*.vspscc
*.vssscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.[Cc]ache
*.ilk
*.log
*.lib
*.sbr
*.sdf
*.pyc
*.xml
ipch/
obj/
[Bb]in
[Dd]ebug*/
[Rr]elease*/
Ankh.NoLoad
#Tooling
_ReSharper*/
*.resharper
[Tt]est[Rr]esult*
#Project files
[Bb]uild/
#Subversion files
.svn
# Office Temp Files
~$*
There's a whole collection of useful .gitignore files by GitHub
GitHub 提供了一整套有用的 .gitignore 文件
Once you have this, you need to add it to your git repository just like any other file, only it has to be in the root of the repository.
Then in your terminal you have to write the following line:
git config --global core.excludesfile ~/.gitignore_global
一旦你有了这个,你需要像任何其他文件一样将它添加到你的 git 存储库中,只是它必须在存储库的根目录中。
然后在您的终端中,您必须编写以下行:
git config --global core.excludesfile ~/.gitignore_global
From oficial doc:
来自官方文档:
You can also create a global .gitignore file, which is a list of rules for ignoring files in every Git repository on your computer. For example, you might create the file at ~/.gitignore_global and add some rules to it.
Open Terminal. Run the following command in your terminal: git config --global core.excludesfile ~/.gitignore_global
您还可以创建一个全局 .gitignore 文件,该文件是用于忽略计算机上每个 Git 存储库中文件的规则列表。例如,您可以在 ~/.gitignore_global 中创建文件并向其添加一些规则。
打开终端。在终端中运行以下命令: git config --global core.excludesfile ~/.gitignore_global
If the respository already exists then you have to run these commands:
如果存储库已存在,则必须运行以下命令:
git rm -r --cached .
git add .
git commit -m ".gitignore is now working"
If the step 2 doesn′t work then you should write the hole route of the files that you would like to add.
如果第 2 步不起作用,那么您应该编写要添加的文件的孔路径。
回答by Patrick James McDougle
As specified hereYou can update the index:
如此处指定,您可以更新索引:
git update-index --assume-unchanged /path/to/file
By doing this, the files will not show up in git status
or git diff
.
通过这样做,文件将不会显示在git status
或 中git diff
。
To begin tracking the files again you can run:
要再次开始跟踪文件,您可以运行:
git update-index --no-assume-unchanged /path/to/file
回答by Eli Nunez
If you added your .gitignore
too late, git will continue to track already commited files regardless. To fix this, you can always remove all cached instances of the unwanted files.
如果您添加的.gitignore
太晚,git 将继续跟踪已提交的文件,无论如何。要解决此问题,您始终可以删除不需要的文件的所有缓存实例。
First, to check what files are you actually tracking, you can run:
首先,要检查您实际跟踪哪些文件,您可以运行:
git ls-tree --name-only --full-tree -r HEAD
git ls-tree --name-only --full-tree -r HEAD
Let say that you found unwanted files in a directory like cache/
so, it's safer to target that directory instead of all of your files.
假设您在这样的目录中发现了不需要的文件,cache/
定位该目录而不是您的所有文件更安全。
So instead of:
所以而不是:
git rm -r --cached .
git rm -r --cached .
It's safer to target the unwanted file or directory:
定位不需要的文件或目录更安全:
git rm -r --cached cache/
git rm -r --cached cache/
Then proceed to add all changes,
然后继续添加所有更改,
git add .
git add .
and commit...
并承诺...
git commit -m ".gitignore is now working"
git commit -m ".gitignore is now working"
Reference: https://amyetheredge.com/code/13.html
回答by Eli Nunez
Here is one way to “untrack” any files that are would otherwise be ignored under the current set of exclude patterns:
这是一种“取消跟踪”在当前排除模式集下会被忽略的任何文件的方法:
(GIT_INDEX_FILE=some-non-existent-file \
git ls-files --exclude-standard --others --directory --ignored -z) |
xargs -0 git rm --cached -r --ignore-unmatch --
This leaves the files in your working directory but removes them from the index.
这会将文件保留在您的工作目录中,但会将它们从索引中删除。
The trick used here is to provide a non-existent index file to git ls-files so that it thinks there are no tracked files. The shell code above asks for all the files that would be ignored if the index were empty and then removes them from the actual index with git rm.
这里使用的技巧是向 git ls-files 提供一个不存在的索引文件,以便它认为没有跟踪文件。如果索引为空,上面的 shell 代码会询问所有将被忽略的文件,然后使用 git rm 从实际索引中删除它们。
After the files have been “untracked”, use git status to verify that nothing important was removed (if so adjust your exclude patterns and use git reset -- path to restore the removed index entry). Then make a new commit that leaves out the “crud”.
在文件被“取消跟踪”后,使用 git status 来验证没有删除任何重要的内容(如果是这样,请调整您的排除模式并使用 git reset --path 来恢复已删除的索引条目)。然后进行一个新的提交,忽略“crud”。
The “crud” will still be in any old commits. You can use git filter-branch to produce clean versions of the old commits if you really need a clean history (n.b. using git filter-branch will “rewrite history”, so it should not be undertaken lightly if you have any collaborators that have pulled any of your historical commits after the “crud” was first introduced).
“crud”仍将在任何旧的提交中。如果你真的需要一个干净的历史,你可以使用 git filter-branch 来生成旧提交的干净版本(nb 使用 git filter-branch 将“重写历史”,所以如果你有任何合作者已经拉取了在首次引入“crud”之后您的任何历史提交)。
回答by Mahdi Ben Selimene
Remove files from the staging area
从暂存区删除文件
git reset HEAD .
Add all changes
添加所有更改
git add .
Commit it:
提交它:
git commit -m ".gitignore is now working"
回答by Roshan Poudyal
I think this is an easy way for adding a .gitignorefile to an existing repository.
我认为这是将.gitignore文件添加到现有存储库的简单方法。
Prerequisite:
先决条件:
You need a browser to access your github account.
您需要一个浏览器来访问您的 github 帐户。
Steps
脚步
- Create a new file in your required (existing) project and name it .gitignore. You will a get suggestive prompt for .gitignore templates as soon as you name the file as .gitignore. Either use these templates or use gitignore.ioto generate the content of your gitignorefile.
- Commit the changes.
- Now clone this repository.
- 在您需要的(现有)项目中创建一个新文件并将其命名为.gitignore。你会得到启发提示的.gitignore模板,只要你命名文件的.gitignore。使用这些模板或使用gitignore.io生成gitignore文件的内容。
- 提交更改。
- 现在克隆这个存储库。
Have fun!
玩得开心!
回答by Nedudi
Use git clean
Get help on this running
使用git clean
获取有关此运行的帮助
git clean -h
If you want to see what would happen first, make sure to pass the -n switch for a dry run:
如果您想先看看会发生什么,请确保通过 -n 开关进行试运行:
git clean -xn
To remove gitingnored garbage
删除 gitingnored 垃圾
git clean -xdf
Careful: You may be ignoring local config files like database.yml which would also be removed. Use at your own risk.
小心:您可能会忽略本地配置文件,例如 database.yml,这些文件也会被删除。使用风险自负。
Then
然后
git add .
git commit -m ".gitignore is now working"
git push