如何忽略 Git 中的某些文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4308610/
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
How to ignore certain files in Git
提问by Kohan95
I have a repository with a file, Hello.java
. When I compile it, an additional Hello.class
file is generated.
我有一个带有文件的存储库,Hello.java
. 当我编译它时,Hello.class
会生成一个附加文件。
I created an entry for Hello.class
in a .gitignore
file. However, the file still appears to be tracked.
我Hello.class
在.gitignore
文件中创建了一个条目。但是,该文件似乎仍被跟踪。
How can I make Git ignore Hello.class
?
如何让 Git 忽略Hello.class
?
回答by Ondrej Slinták
The problem is that .gitignore
ignores just files that weren't tracked before (by git add
). Run git reset name_of_file
to unstage the file and keep it. In case you want to also remove the given file from the repository (after pushing), use git rm --cached name_of_file
.
问题是.gitignore
忽略了之前(by git add
)未跟踪的文件。运行git reset name_of_file
以取消暂存文件并保留它。如果您还想从存储库中删除给定的文件(推送后),请使用git rm --cached name_of_file
.
回答by Xman Classical
How to ignore new files
如何忽略新文件
Globally
全球范围内
Add the path(s) to your file(s) which you would like to ignore to your .gitignore
file (and commit them). These file entries will also apply to others checking out the repository.
将要忽略的.gitignore
文件路径添加到文件中(并提交它们)。这些文件条目也适用于签出存储库的其他人。
Locally
本地
Add the path(s) to your file(s) which you would like to ignore to your .git/info/exclude
file. These file entries will only apply to your local working copy.
将要忽略的.git/info/exclude
文件路径添加到文件中。这些文件条目仅适用于您的本地工作副本。
How to ignore changed files (temporarily)
如何忽略更改的文件(暂时)
In order to ignore changed files to being listed as modified, you can use the following git command:
为了忽略已更改的文件被列为已修改,您可以使用以下 git 命令:
git update-index --assume-unchanged <file>
To revert that ignorance use the following command:
要恢复这种无知,请使用以下命令:
git update-index --no-assume-unchanged <file>
回答by Armand
Add the following line to .gitignore:
将以下行添加到 .gitignore:
/Hello.class
This will exclude Hello.class from git. If you have already committed it, run the following command:
这将从 git 中排除 Hello.class。如果您已经提交,请运行以下命令:
git rm Hello.class
If you want to exclude all class files from git, add the following line to .gitignore:
如果要从 git 中排除所有类文件,请将以下行添加到 .gitignore:
*.class
回答by SomeAnonymousPerson
1) Create a .gitignore
file. To do that, you just create a .txt
file and change the extension as follows:
1) 创建.gitignore
文件。为此,您只需创建一个.txt
文件并按如下方式更改扩展名:
Then you have to change the name, writing the following line in a cmd window:
然后您必须更改名称,在 cmd 窗口中写入以下行:
rename git.txt .gitignore
Where git.txt
is the name of the file you've just created.
git.txt
您刚刚创建的文件的名称在哪里。
Then you can open the file and write all the files you don't want to add on the repository. For example, mine looks like this:
然后您可以打开文件并将所有不想添加到存储库的文件写入。例如,我的看起来像这样:
#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
~$*
Once you have this, you need to add it to your Git repository. You have to save the file where your repository is.
一旦你有了这个,你需要将它添加到你的 Git 存储库中。您必须将文件保存在您的存储库所在的位置。
Then in Git Bash you have to write the following line:
然后在 Git Bash 中,您必须编写以下行:
git config --global core.excludesfile ~/.gitignore_global
If the repository already exists then you have to do the following:
如果存储库已经存在,那么您必须执行以下操作:
git rm -r --cached .
git add .
git commit -m ".gitignore is now working"
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 whole route of the files that you would like to add.
如果第 2 步不起作用,那么您应该编写要添加的文件的整个路径。
回答by avj
To ignore:
忽略:
git update-index --assume-unchanged <path/to/file>
To undo ignore:
要撤消忽略:
git update-index --no-assume-unchanged <path/to/file>
回答by kapillohakare
You can use below methods for ignoring/not-ignoring changes in tracked files.
您可以使用以下方法忽略/不忽略跟踪文件中的更改。
- For ignoring:
git update-index --assume-unchanged <file>
- For reverting ignored files:
git update-index --no-assume-unchanged <file>
- 忽略:
git update-index --assume-unchanged <file>
- 恢复被忽略的文件:
git update-index --no-assume-unchanged <file>
回答by terminus
Create a .gitignore
in the directory where .git is. You can list files in it separated by a newline. You also can use wildcards:
.gitignore
在 .git 所在的目录中创建一个。您可以列出其中以换行符分隔的文件。您还可以使用通配符:
*.o
.*.swp
回答by Mile Mijatovi?
From the official GitHub site:
来自官方 GitHub 站点:
If you already have a file checked in, and you want to ignore it, Git will not ignore the file if you add a rule later. In those cases, you must untrack the file first, by running the following command in your terminal:
如果您已经签入了一个文件,并且您想忽略它,那么如果您稍后添加规则,Git 将不会忽略该文件。在这些情况下,您必须首先通过在终端中运行以下命令来取消跟踪文件:
git rm --cached FILENAME
or
或者
git rm --cached .
回答by KingCrunch
You should write something like
你应该写一些类似的东西
*.class
into your .gitignore
file.
进入你的.gitignore
档案。
回答by Chinmoy
Add which files you want to ignore to file .gitignore
:
将要忽略的文件添加到 file .gitignore
:
*.class
*。班级
*.projects
*.projects
*.prefs
*.prefs
*.project
*。项目