997

I have a repository with a file, Hello.java. 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?

2

29 Answers 29

1057

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The command git rm --cached name_of_file will remove the file from your git repo. Even if you add it to your .gitignore file. That is not ignoring that is deleting.
I'm glad your solution worked for @Kohan95! Though this question should be renamed based on the selected answer. My comments are just a warning to devs that may not be aware what the command does.
git reset name_of_file removes tracking and doesn't delete the file.
Tried git rm --cached <name_of_file> It did delete the file in the repo, but since it was in the .gitignore file, the local copy was not deleted. As a further note, to add a directory, you need to put it in with a trailing forward slash '/'. I was removing Rubymine's config directory in a ruby project that someone had checked in, which was ".idea". In the file I put ".idea/" and then "git rm --cached -r .idea" to remove the directory and everything under it (because git only versions files, not directories.)
347

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.

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.

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

To revert that ignorance use the following command:

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

2 Comments

Is there really no programmatic way to do this from the command line? like "git ignore filenamepath"
@ggb667, There is not, unfortunately. "There is no explicit git ignore command: instead the .gitignore file must be edited and committed by hand when you have new files that you wish to ignore." - atlassian.com/git/tutorials/saving-changes/gitignore
180

Add the following line to file .gitignore:

/Hello.class

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:

*.class

2 Comments

Adding Hello.class to .gitignore will ignore any file called Hello.class in any subdirectory. If you intend to only ignore the file Hello.class in the same directory as the .gitignore file, use a /Hello.class line instead.
@Imray you can put .gitignore anywhere in a git project - if a path starts with /, it will be relative to the location of the .gitignore file; otherwise it will refer recursively to files in the current directory and its descendant directories.
144

On Windows

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

Comments

68

You can use the 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>

1 Comment

Please don't: 'Assume-unchanged should not be abused for an ignore mechanism. It is "I know my filesystem operations are slow. I'll promise Git that I won't change these paths…" Especially, it is not a promise… that Git will always consider these paths are unmodified—if Git can determine a path… has changed without incurring extra lstat(2) cost, it reserves the right to report that the path has been modified (…git commit -a is free to commit that change).'
24

Create a .gitignore file in the directory where directory .git is. You can list files in it separated by a newline. You also can use wildcards:

*.o
.*.swp

Comments

19

From the official GitHub site:

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 rm --cached FILENAME

or

git rm --cached .

Comments

17
  1. Go to .gitignore file and add the entry for the files you want to ignore
  2. Run git rm -r --cached .
  3. Now run git add .

Comments

12

You should write something like

*.class

into your .gitignore file.

1 Comment

@AgentZebra You should add an entry *.class to your .gitignore to make git ignore all files ending in .class.
9

First create a .gitignore file where we have to store the names of files and directories to be ignored.

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. 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 rm -r --cached <path_of_the_file>

And the rest is the same, i.e, add the name to the .gitignore file.

Comments

8

Add which files you want to ignore to file .gitignore:

*.class

*.projects

*.prefs

*.project

Comments

8

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

  1. Navigate to your Git repository.
  2. Enter "touch .gitignore" which will create a .gitignore file.

Comments

5

By creating a .gitignore file. See here for details: Git Book - Ignoring files

Also check this one out: How do you make Git ignore files without using .gitignore?

Comments

5

Use:

git reset filename
git rm --cached filename

Then add your file which you want to ignore. Then commit and push to your repository.

Comments

4

You can also use .gitattributes (instead of .gitignore) to exclude entire filetypes. The file is pretty self-explanatory, but I'm pasting the contents here for reference. Pay attention to the last line (*.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

Comments

3

I had a similar issue with file "dump.rdb".
I tried adding this file in various ways to .gitignore file, but only one way worked.

Add your filename, at the end of .gitignore file

NOTE: Adding the file anywhere else didn't work.

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

Comments

3

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.

    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/*. I executed this command from the src directory.

Comments

3

To remove already commited/staged file from GIT git rm --cached <file_name> Add the following line to file .gitignore: /<file_name> If you want to exclude all class files from Git, add the following line to .gitignore:*<file_extension>

Comments

3

To ignore a file in Git, create a .gitignore file in the root directory of your project. This file contains a list of files that should be ignored by Git.

Here are some of my steps for creating and ignoring files.

  • Create a .gitignore in your project

  • Inside the .gitignore file add a names of a file you want to ignore for example api_keys.txt and add the following lines in your .gitignore file.

  • Run the following commands to stage and commit the modifications to your .gitignore file:

    git add .gitignore git commit -m "Add .gitignore file"

  • Git will only disregard files that haven't been committed to your Git repository yet, so keep that in mind. If you want to ignore a file that you have already committed, you must first remove it from your Git repository before adding it to your .gitignore file:

    git rm --cached api_keys.txt

The file will no longer be present in the Git repository but will remain on your local file system. After that, you may add it to your .gitignore file and push the modifications.

Comments

2

Add the following line to .git/info/exclude:
Hello.class

2 Comments

Are you supposed change files inside folder .git?
Maybe, not a good idea to touch those files under .git manually.
1

This webpage may be useful and time-saving when working with .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).

Comments

1

If any file is added to Git before, then remove that file and add that file in .gitignore.

Example: if have trouble ignoring the package.resolved file generated during adding an spm dependency.

I have added below lines to my .gitignore file:

Packages/
Package.pins
Package.resolved
*.xcworkspace

and removed the package.resolved file. Now Git starts ignoring my package.resolved file.

1 Comment

What is "an spm dependency"? Do you mean "an SPM (Swift Package Manager) dependency"? Swift. Preferably, please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
1

If you want to ignore certain files, follow the below steps

  1. get inside the .git folder located in your working directory.
  2. get inside the "info" folder.
  3. you will find a file with the name "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.

Comments

0

I have tried the --assume-unchanged and also the .gitignore but neither worked well. 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.

  1. git stash
  2. git pull
  3. git stash pop

Step 3 sometimes will trigger a merge and may result in conflict that you need to resolve, which is a good thing.

This allows me to keep local changes that are not shared with others on the team.

1 Comment

This does not solve the question permanently.
0

The one case you might be experiencing is that .gitignore won't ignore the files you committed before. You should first remove or move those files from the repository, commit the changes you just did, and then add "*.class" to .gitignore.

Comments

0

Straight from 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.

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.

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.

1 Comment

your solution is already reflected in other answers
0

You can as well use for example Sourcetree and pressing rmb at files that you added to the branch and was modified, and then select "stop tracking". This file will be now designated with a question mark icon and when you once again rmb at this file, you can now select "Ignore". This will add this certain file to gitgnore by its relative path.

2 Comments

What do you mean by "ppm"? Is it an abbreviation? Or something else?
Yeah, It should be rmb - Right Mouse Button, instead of ppm
0

Ignoring a previously committed file

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. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

$ echo name_of_file >> .gitignore

$ git rm --cached name_of_file
rm 'name_of_file'

$ git commit -m "Start ignoring name_of_file"

You can omit the --cached option if you want to delete the file from both the repository and your local file system.

Straight from Atlassian: .gitignore

Comments

-2

For compiled code, just write:

.class or .o

1 Comment

Welcome to Stack Overflow. When adding an answer to a nine year old question with an accepted answer and nineteen other answers it is very important to point out what new aspect of the question your answer addresses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.