897

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?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Kohan95
  • 9,359
  • 5
  • 19
  • 21
  • 9
    What do you mean "it does not work"? Have you perhaps already added Hello.class to your repository? gitignores have no effect on content that's already tracked. See for example http://stackoverflow.com/questions/1139762/gitignore-file-not-ignoring – Cascabel Nov 29 '10 at 22:14
  • 1
    possible duplicate of [Git: Remove a file from the repository without deleting it from the local filesystem](http://stackoverflow.com/questions/1143796/git-remove-a-file-from-the-repository-without-deleting-it-from-the-local-filesy) – Cascabel Feb 01 '12 at 20:31

27 Answers27

968

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Ondrej Slinták
  • 30,448
  • 20
  • 91
  • 125
  • 234
    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. – OrwellHindenberg Mar 23 '15 at 21:06
  • 6
    Which seems to be what OP wanted in this case. – Ondrej Slinták Mar 23 '15 at 22:48
  • 49
    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. – OrwellHindenberg Mar 24 '15 at 14:21
  • 101
    `git reset name_of_file` removes tracking and doesn't delete the file. – Cees Timmerman Jun 09 '15 at 13:12
  • 7
    Tried git rm --cached 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.) – Doug Noel Mar 16 '16 at 16:37
  • Hi, I have file in my repository config.php, I want to ignore this file not delete from repo which command i need to use reset or remove?? – Amit Kumar Aug 29 '16 at 20:01
  • 2
    @OrwellHindenberg I thought `git rm --cached ` deleted the file from the git history but not from the local file system and not from the repository. I was wrong thinking this ? – Stephane Nov 04 '16 at 11:12
  • 3
    @Stephane you are partially right. `git rm --cached ` will delete the file from repository once you push. You are right however in saying that the file will still exist locally and be seen in git status as `?? `. The difference between `git rm --cached ` and `git rm ` is that without `--cached` the file will be deleted locally as well. – OrwellHindenberg Nov 04 '16 at 18:37
  • 1
    what if I want to ignore the directory as well which has already been added using git add . – Raulp Jan 30 '17 at 10:58
  • 1
    @OndrejSlinták how to ignore a directory – Kasun Siyambalapitiya Mar 10 '17 at 04:54
  • @OrwellHindenberg - thanks for hilighting. Always good to have someone looking out. – Mr. Benedict Apr 28 '20 at 16:01
  • how to ignore a directory by "git reset"ting it. – Naveen Reddy Marthala Jul 20 '20 at 05:20
294

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>
Promise Preston
  • 16,322
  • 10
  • 91
  • 108
Xman Classical
  • 4,811
  • 1
  • 24
  • 26
  • Perfect for preventing a file holding an current api key from being pushed into github! – Jim In Texas Jul 29 '15 at 17:06
  • 1
    @Xman Why you said it is temporarily? I did the steps you have mentioned for `local` git ignore, and restarted my system, it still shows working directly clean. – Vivek Vardhan Sep 06 '15 at 04:55
  • 1
    Thanks for the useful `update-index` trick, absolutely perfect to avoid changing a `.gitignore` file, which is then tracked by Git ! – Thomas P. Oct 17 '15 at 12:54
  • @mcbjam There is no special git ignore command. Edit a .gitignore file located in the appropriate place within the working copy. You should then add this .gitignore and commit it. Everyone who clones that repo will than have those files ignored. – Xman Classical Feb 15 '16 at 10:47
  • @Vivek Vardhan git update-index --assume-unchanged is like a temporary mask on the index. It will stay in effect until you turn it off. To possibly make my point more clear, the index initially (that is, right after git read-tree) "contains" all the files from the tree-ish which had just been read in. Hence making Git "turn a blind eye" to an index entry does not equal actually removing it. – Xman Classical Feb 15 '16 at 11:04
  • Appreciated the mentioning of .git/info/exclude – Paul Mar 05 '19 at 14:14
  • I've noticed that Visual Studio 2019 shows the ignored files as changed until you click Save. (then it ignores it as expected) – AriesConnolly May 29 '20 at 13:06
  • 2
    git update-index --assume-unchanged is exactly what I needed to be able to remove .htpasswd from local and keep it on production – 6opko Feb 03 '21 at 12:07
  • You used the term globally and locally but I think remote repo and local repo would make more sense? Globally sounds more like the global .gitignore for all of the projects in your workspace? – Chucky Dec 07 '21 at 11:43
167

Add the following line to .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
Armand
  • 22,275
  • 20
  • 85
  • 117
  • 35
    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. – ndim Nov 29 '10 at 22:21
  • Where do you put the `.gitignore` file? – CodyBugstein Jan 01 '15 at 14:20
  • 6
    @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. – Armand Jan 05 '15 at 10:29
132

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

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
SomeAnonymousPerson
  • 2,997
  • 1
  • 18
  • 22
  • You can use the command "copy con .gitignore" in the prompt and then press CTRL+Z to directly create the file without Windows Explorer. – heringer Jun 12 '17 at 19:17
73

To ignore:

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

To undo ignore:

git update-index --no-assume-unchanged <path/to/file>
avj
  • 1,506
  • 1
  • 16
  • 23
  • 6
    I would like this to be top of the chart as solution to the problem....Its very simple & elegant – user1457958 Jan 11 '17 at 10:51
  • "Its very simple & elegant"—[not really](https://public-inbox.org/git/xmqqy4z5go1y.fsf@gitster.dls.corp.google.com/): '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).' – Chris Apr 09 '19 at 01:37
54

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>
de-russification
  • 31,008
  • 6
  • 34
  • 52
kapillohakare
  • 760
  • 5
  • 7
  • 4
    Does that apply to *all* files? It is unclear how those commands may be applied to just a single file to ignore/unignore it. – WestCoastProjects Jul 26 '15 at 00:11
  • @kapil finally !! something that actually works amongst a trillion git command variations ! Tried every answer to ignore some stupid build.xml files that kept appearing after every build...thank you! – killjoy Nov 01 '17 at 12:46
  • 1
    [Please don't](https://public-inbox.org/git/xmqqy4z5go1y.fsf@gitster.dls.corp.google.com/): '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).' – Chris Apr 09 '19 at 01:36
22

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

*.o
.*.swp
samayo
  • 15,152
  • 12
  • 83
  • 101
terminus
  • 12,897
  • 8
  • 33
  • 37
  • 3
    It is a good practice to avoid putting tool-specific patterns (i.e. your temporary Vim files) in `.gitignore` files (unless that tool is mandated by the project). You can put patterns for your favorite tools in your per-user excludes file (set `core.excludesFile`); they will work across all your repositories. E.g. `git config --global core.excludesFile "$HOME/.git-user-excludes"`, then put them in that file. – Chris Johnsen Nov 29 '10 at 22:39
  • @Chris: Thanks for the tip. But I just wrote it there because nothing better came to my mind :) – terminus Nov 29 '10 at 22:40
  • 1
    @ChrisJohnsen 'tool specific?' for vim?? i think it's safe to exclude vim swap files.. – WestCoastProjects Jul 26 '15 at 00:14
17

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 .
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Mile Mijatović
  • 2,535
  • 1
  • 22
  • 36
15
  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 .
Rahul Garg
  • 3,825
  • 31
  • 30
12

You should write something like

*.class

into your .gitignore file.

KingCrunch
  • 124,572
  • 19
  • 146
  • 171
  • 4
    @AgentZebra You should add an entry `*.class` to your `.gitignore` to make git ignore all files ending in `.class`. – KingCrunch Jan 09 '16 at 19:10
8

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

*.class

*.projects

*.prefs

*.project

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Chinmoy
  • 1,186
  • 11
  • 12
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.
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Savyasachi
  • 131
  • 1
  • 5
8

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 extention;

*.pyc  #will ignore all files with pyc extention

EDIT:

Also the above things will only work at the first time when you have not added the files to the git. But if my mistake you added a file to the 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 rest is same, i.e, add the name to the .gitignore file.

Irfan wani
  • 3,234
  • 2
  • 12
  • 24
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?

Community
  • 1
  • 1
yasouser
  • 4,991
  • 2
  • 26
  • 39
5
git reset filename
git rm --cached filename

then add your file which you want to ignore it,

then commit and push to your repository

Omar Abusabha
  • 317
  • 3
  • 8
4

I tried this -

  1. list files which we want to ignore

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

  1. Copy the content of step#1 and append it into .gitignore file.

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

  1. Validate

git status .gitignore

likewise we can add directory and all of its sub dir/files which we want to ignore in git status using directoryname/* and I executed this command from src directory.

VIPIN KUMAR
  • 2,838
  • 1
  • 19
  • 34
3

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
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
David Niki
  • 1,062
  • 1
  • 11
  • 12
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

Manohar Reddy Poreddy
  • 21,015
  • 9
  • 137
  • 125
2

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

Dado
  • 21
  • 1
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).

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Jaime Caffarel
  • 2,251
  • 4
  • 30
  • 40
1

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

Example: if have trouble ignoring package.resolved file generated during adding spm dependency. i have added below lines to my .gitignore file

Packages/
Package.pins
Package.resolved
*.xcworkspace

and removed package.resolved file. Now git start ignoring my package.resolved file.

Amrit
  • 283
  • 2
  • 13
1

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 file path to .gitignore file.

farhad.kargaran
  • 2,103
  • 1
  • 22
  • 29
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.

John Henckel
  • 8,771
  • 3
  • 67
  • 74
0

for compiled code. just write

.class or .o
  • 1
    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. – Jason Aller Jul 09 '20 at 02:14
0

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

0

Straight from Atlassian: https://www.atlassian.com/git/tutorials/saving-changes/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 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 repo, unless you force it.

Ryan
  • 1
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 that files from the repository, commit the changes you just did, then add "*.class" to .gitignore.