How to ignore certain files in Git


I have a repository with a file, When I compile it, an additional Hello.class file is generated.

I created an entry for Hello.class in a .gitignore file. However, the file still appears to be tracked.

How can I make Git ignore Hello.class?

8/4/2019 7:46:40 PM

Accepted Answer

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.

8/4/2019 8:38:34 PM

How to ignore new files


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.


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.

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 update-index --assume-unchanged <file>

To revert that ignorance use the following command:

git update-index --no-assume-unchanged <file>

Add the following line to .gitignore:


This will exclude Hello.class from git. 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:


1) Create a .gitignore file. To do that, you just create a .txt file and change the extension as follows:

Enter image description here

Then you have to change the name, writing the following line in a cmd window:

 rename git.txt .gitignore

Where git.txt is the name of the file you've just created.

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

#Visual Studio files


#Project files

#Subversion files

# 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.

Then in Git Bash you have to write the following line:

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 the step 2 doesn’t work then you should write the whole route of the files that you would like to add.


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 below methods for ignoring/not-ignoring changes in tracked files.

  1. For ignoring: git update-index --assume-unchanged <file>
  2. For reverting ignored files: git update-index --no-assume-unchanged <file>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]