简体   繁体   English

如何忽略Git中的某些文件

[英]How to ignore certain files in Git

I have a repository with a file, Hello.java .我有一个包含文件Hello.java的存储库。 When I compile it, an additional Hello.class file is generated.当我编译它时,会生成一个额外的Hello.class文件。

I created an entry for Hello.class in a .gitignore file.我在.gitignore文件中为Hello.class创建了一个条目。 However, the file still appears to be tracked.但是,该文件似乎仍被跟踪。

How can I make Git ignore Hello.class ?如何让 Git 忽略Hello.class

The problem is that .gitignore ignores just files that weren't tracked before (by git add ).问题是.gitignore只忽略以前未跟踪的文件(通过git add )。 Run git reset name_of_file to unstage the file and keep it.运行git reset name_of_file以取消暂存文件并保留它。 In case you want to also remove the given file from the repository (after pushing), use git rm --cached name_of_file .如果您还想从存储库中删除给定文件(推送后),请使用git rm --cached name_of_file

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).将要忽略的文件的路径添加到.gitignore文件中(并提交它们)。 These file entries will also apply to others checking out the repository.这些文件条目也将适用于检出存储库的其他人。

Locally本地

Add the path(s) to your file(s) which you would like to ignore to your .git/info/exclude file.将要忽略的文件的路径添加到.git/info/exclude文件中。 These file entries will only apply to your local working copy.这些文件条目将仅适用于您的本地工作副本。

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 <file1> <file2> <file3>

To revert that ignorance use the following command:要恢复这种无知,请使用以下命令:

git update-index --no-assume-unchanged <file1> <file2> <file3>

Add the following line to file .gitignore :将以下行添加到文件.gitignore

/Hello.class

This will exclude Hello.class from Git.这将从 Git 中排除Hello.class If you have already committed it, run the following command:如果您已经提交了它,请运行以下命令:

git rm Hello.class

If you want to exclude all class files from Git, add the following line to .gitignore :如果要从 Git 中排除所有类文件,请将以下行添加到.gitignore

*.class

On Windows在 Windows 上

  1. Create a .gitignore file.创建一个.gitignore文件。 To do that, you just create a .txt file and change the extension as follows:为此,您只需创建一个.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

# Microsoft Office temporary files
~$*

Once you have this, you need to add it to your Git repository.一旦你有了这个,你需要将它添加到你的 Git 存储库中。 You have to save the file where your repository is.您必须将文件保存在存储库所在的位置。

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:如果存储库已经存在,那么您必须执行以下操作:

  1. git rm -r --cached .
  2. git add .
  3. git commit -m ".gitignore is now working"

If step 2 doesn't work, then you should write the whole route of the files that you would like to add.如果第 2 步不起作用,那么您应该编写要添加的文件的整个路径。

To ignore:无视:

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

To undo ignore:要撤消忽略:

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

You can use the below methods for ignoring/not-ignoring changes in tracked files.您可以使用以下方法忽略/不忽略跟踪文件中的更改。

  1. For ignoring: git update-index --assume-unchanged <file>忽略: git update-index --assume-unchanged <file>
  2. For reverting ignored files: git update-index --no-assume-unchanged <file>恢复被忽略的文件: git update-index --no-assume-unchanged <file>

Create a .gitignore file in the directory where directory .git is.在目录.git所在的目录中创建一个.gitignore文件。 You can list files in it separated by a newline.您可以列出其中的文件,并用换行符分隔。 You also can use wildcards:您还可以使用通配符:

*.o
.*.swp

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.如果您已经签入了一个文件,并且您想忽略它,那么如果您稍后添加规则,Git 将不会忽略该文件。 In those cases, you must untrack the file first, by running the following command in your terminal:在这些情况下,您必须先取消跟踪文件,方法是在终端中运行以下命令:

git rm --cached FILENAME

or或者

git rm --cached .
  1. Go to .gitignore file and add the entry for the files you want to ignore转到 .gitignore 文件并添加要忽略的文件的条目
  2. Run git rm -r --cached .运行git rm -r --cached .
  3. Now run git add .现在运行git add .

You should write something like你应该写类似

*.class

into your .gitignore file.进入你的.gitignore文件。

Add which files you want to ignore to file .gitignore :将要忽略的文件添加到文件.gitignore

*.class *。班级

*.projects *.项目

*.prefs *.prefs

*.project *。项目

If you have already committed the file and you are trying to ignore it by adding to the .gitignore file, Git will not ignore it.如果你已经提交了文件并且你试图通过添加到.gitignore文件来忽略它,Git 不会忽略它。 For that, you first have to do the below things:为此,您首先必须执行以下操作:

git rm --cached FILENAME

If you are starting the project freshly and you want to add some files to Git ignore, follow the below steps to create a Git ignore file:如果您刚开始项目并且想要将一些文件添加到 Git 忽略,请按照以下步骤创建 Git 忽略文件:

  1. Navigate to your Git repository.导航到您的 Git 存储库。
  2. Enter "touch .gitignore" which will create a .gitignore file.输入“touch .gitignore”,这将创建一个.gitignore文件。

First create a .gitignore file where we have to store the names of files and directories to be ignored.首先创建一个.gitignore文件,我们必须在其中存储要忽略的文件和目录的名称。

To ignore a directory;忽略目录;

name_of_directory/

To ignore a file;忽略文件;

name_of_file

We don't need to provide the complete path of the file or directory to be ignored;我们不需要提供要忽略的文件或目录的完整路径; we just have to provide its name.我们只需要提供它的名字。

If you want to ignore all files with same extension;如果您想忽略所有具有相同扩展名的文件;

*.pyc  #will ignore all files with pyc extention

Also the above things will only work at the first time when you have not added the files to Git.此外,上述内容仅在您尚未将文件添加到 Git 时才第一次起作用。 But if my mistake you added a file to Git and now you want it to be ignored for the rest of the commits, you need to first clear the Git cache regarding that file by running this command;但是,如果我的错误是您将文​​件添加到 Git,而现在您希望在其余提交中忽略该文件,则需要首先通过运行此命令清除有关该文件的 Git 缓存;

git rm -r --cached <path_of_the_file>

And the rest is the same, ie, add the name to the .gitignore file.其余的都是一样的,即在.gitignore文件中添加名称。

By creating a .gitignore file.通过创建 .gitignore 文件。 See here for details: Git Book - Ignoring files有关详细信息,请参见此处: Git Book - 忽略文件

Also check this one out: How do you make Git ignore files without using .gitignore?还要检查一下: 如何让 Git 在不使用 .gitignore 的情况下忽略文件?

Use:利用:

git reset filename
git rm --cached filename

Then add your file which you want to ignore.然后添加您要忽略的文件。 Then commit and push to your repository.然后提交并推送到您的存储库。

I tried this -我试过这个 -

  1. List files which we want to ignore列出我们要忽略的文件

    git status .idea/xyz.xml .idea/pqr.iml Output .DS_Store

  2. Copy the content of step#1 and append it into the .gitignore file.复制第 1 步的内容并将其附加到.gitignore文件中。

    echo " .idea/xyz.xml .idea/pqr.iml Output .DS_Store" >> .gitignore

  3. Validate证实

    git status .gitignore

Likewise, we can add a directory and all of its subdirectories and files which we want to ignore in Git status using directoryname/* .同样,我们可以使用directoryname/*添加一个目录及其所有要在 Git 状态中忽略的子目录和文件。 I executed this command from the src directory.我从src目录执行了这个命令。

You can also use .gitattributes (instead of .gitignore) to exclude entire filetypes.您还可以使用 .gitattributes(而不是 .gitignore)来排除整个文件类型。 The file is pretty self-explanatory, but I'm pasting the contents here for reference.该文件非常不言自明,但我将内容粘贴在这里以供参考。 Pay attention to the last line (*.class binary):注意最后一行(*.class binary):

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.gif binary
*.ico binary
*.mo binary
*.pdf binary
*.phar binary
*.class binary

I had a similar issue with file "dump.rdb".我对文件“dump.rdb”有类似的问题。
I tried adding this file in various ways to .gitignore file, but only one way worked.我尝试以各种方式将此文件添加到 .gitignore 文件中,但只有一种方式有效。

Add your filename, at the end of .gitignore file在 .gitignore 文件的末尾添加您的文件名

NOTE: Adding the file anywhere else didn't work.注意:将文件添加到其他任何地方都不起作用。

For example, see: https://gitlab.com/gitlab-org/gitlab-ce/raw/b3ad3f202478dd88a3cfe4461703bc3df1019f90/.gitignore例如,参见: https ://gitlab.com/gitlab-org/gitlab-ce/raw/b3ad3f202478dd88a3cfe4461703bc3df1019f90/.gitignore

Add the following line to .git/info/exclude:将以下行添加到 .git/info/exclude:
Hello.class同学们好

This webpage may be useful and time-saving when working with .gitignore .使用.gitignore时,此网页可能有用且省时。

It automatically generates .gitignore files for different IDEs and operating systems with the specific files/folders that you usually don't want to pull to your Git repository (for instance, IDE-specific folders and configuration files).它会自动为不同的 IDE 和操作系统生成 .gitignore 文件,其中包含您通常不想拉入 Git 存储库的特定文件/文件夹(例如,特定于 IDE 的文件夹和配置文件)。

If any file is added to Git before, then remove that file and add that file in .gitignore .如果之前将任何文件添加到 Git,则删除该文件并将该文件添加到.gitignore中。

Example: if have trouble ignoring the package.resolved file generated during adding an spm dependency.示例:如果无法忽略在添加 spm 依赖项期间生成的package.resolved文件。

I have added below lines to my .gitignore file:我在.gitignore文件中添加了以下几行:

Packages/
Package.pins
Package.resolved
*.xcworkspace

and removed the package.resolved file.并删除了package.resolved文件。 Now Git starts ignoring my package.resolved file.现在 Git 开始忽略我的package.resolved文件。

If you want to ignore a file that is already checked in, you must untrack the file before you add a rule to ignore it:如果要忽略已签入的文件,则必须在添加规则以忽略它之前取消跟踪该文件:

git rm --cached FILENAME

Now commit changes and add the file path to the .gitignore file.现在提交更改并将文件路径添加到.gitignore文件。

I have tried the --assume-unchanged and also the .gitignore but neither worked well.我已经尝试过--assume-unchanged.gitignore但都没有奏效。 They make it hard to switch branches and hard to merge changes from others.它们使切换分支变得困难,并且难以合并来自其他分支的更改。 This is my solution:这是我的解决方案:

When I commit , I manually remove the files from the list of changes当我提交时,我从更改列表中手动删除文件

Before I pull , I stash the changed files.在我之前,我存储了更改的文件。 And after pull, I do a stash pop .拉后,我做了一个stash pop

  1. git stash混帐藏匿
  2. git pull git 拉
  3. git stash pop git stash 弹出

Step 3 sometimes will trigger a merge and may result in conflict that you need to resolve, which is a good thing.第 3 步有时会触发合并,并可能导致您需要解决的冲突,这是一件好事。

This allows me to keep local changes that are not shared with others on the team.这使我可以保留不与团队中的其他人共享的本地更改。

You can as well use for example Sourcetree and pressing ppm at files that you added to the branch and was modified, and then select "stop tracking".您也可以使用例如Sourcetree并在添加到分支并已修改的文件上按 ppm,然后选择“停止跟踪”。 This file will be now designated with a question mark icon and when you once again ppm at this file, you can now select "Ignore".该文件现在将用问号图标指定,当您再次 ppm 访问该文件时,您现在可以选择“忽略”。 This will add this certain file to gitgnore by its relative path.这将通过其相对路径将此特定文件添加到gitgnore

Straight from Atlassian: .gitignore直接来自 Atlassian: .gitignore

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a .gitignore rule for it.如果您想忽略过去提交的文件,则需要从存储库中删除该文件,然后为其添加.gitignore规则。

Using the --cached option with git rm means that the file will be deleted from your repository, but it will remain in your working directory as an ignored file.--cached选项与git rm一起使用意味着该文件将从您的存储库中删除,但它将作为一个被忽略的文件保留在您的工作目录中。

You should still have the file in your working directory, but it will not be able to pushed to your remote repository, unless you force it.您的工作目录中应该仍然有该文件,但它无法推送到您的远程存储库,除非您强制它。

The one case you might be experiencing is that .gitignore won't ignore the files you committed before.您可能遇到的一种情况是.gitignore不会忽略您之前提交的文件。 You should first remove or move those files from the repository, commit the changes you just did, and then add "*.class" to .gitignore .您应该首先从存储库中删除或移动这些文件,提交您刚刚所做的更改,然后将“*.class”添加到.gitignore

要从 GIT 中删除已提交/暂存的文件git rm --cached <file_name>将以下行添加到文件.gitignore/<file_name>如果要从 Git 中排除所有类文件,请将以下行添加到.gitignore*<file_extension>

If you want to ignore certain files, follow the below steps如果您想忽略某些文件,请按照以下步骤操作

  1. get inside the.git folder located in your working directory.进入位于工作目录中的 .git 文件夹。
  2. get inside the "info" folder.进入“信息”文件夹。
  3. you will find a file with the name "exclude".你会发现一个名为“exclude”的文件。
  4. add filenames of which you want to ignore into this file.将要忽略的文件名添加到此文件中。

If you don't see.git folder in your directory, in the file-explorer view panel just check the Hidden-item field.如果您在目录中没有看到 .git 文件夹,请在文件资源管理器视图面板中选中隐藏项目字段。

对于已编译的代码,只需编写:

.class or .o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM