62

When doing:

>git status

It shows a big list of .pyc files under "untracked files". I don't want it to show these, as it adds noise.

In other words, how do I make git ignore .pyc files all the time and for all projects?

EDIT

I'm not asking for a way to spread my ignored files to other people, I really just mean "for all projects", meaning I don't want to configure each new project to ignore .pyc files.

UPDATE

I should add that I'm working on windows, and my git is msysgit

Patterns which a user wants git to ignore in all situations (e.g., backup or temporary files generated by the user's editor of choice) generally go into a file specified by core.excludesfile in the user's ~/.gitconfig.

Is .gitconfig a file or a folder? I don't have such a thing in my home directory (C:\users\<myusername>\)

UPDATE2

Thanks everybody for the responses,

I solved the issues by using:

>git config --global core.excludesfile c:\git\ignore_files.txt

and putting *.pyc in c:\git\ignore_files.txt

hasen
  • 161,647
  • 65
  • 194
  • 231

7 Answers7

88

As mentioned in gitignore, git has 3 ignore files:

  • Patterns which should be version-controlled and distributed to other repositories via clone (i.e., files that all developers will want to ignore) should go into a .gitignore file.

(that takes care of all time: if one clones your repo, it will get the .gitignore file)

  • Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user's workflow) should go into the $GIT_DIR/info/exclude file.

(not interesting in your case)

  • Patterns which a user wants git to ignore in all situations (e.g., backup or temporary files generated by the user's editor of choice) generally go into a file specified by core.excludesfile in the user's ~/.gitconfig.

(takes cares of all projects, but not all time since it is not replicated when one clones your repo.)

"All time for all projects" would means for instance a custom shell for creating new git projects, with a custom .gitignore as first file part of the first commit...

CharlesB
  • 86,532
  • 28
  • 194
  • 218
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • +1: Comprehensive answer. Except you should mention that `core.excludesfile` could be set *per project* as well as global setting. – jfs Mar 14 '09 at 10:09
57

Put in the file ~/.gitignore:

*.pyc

Run:

git config --global core.excludesfile ~/.gitignore

I agree with @David Wolever this option should be used with caution if ever.

The ~/.gitignore ("%HOME%\.gitignore" on Windows) is just a convention you can use any filename you like e.g., "c:\docs\gitignore.txt".

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Why should this be used with caution? – LazerSharks Jun 10 '15 at 17:22
  • @Gnuey: it is a global setting that is applied to all your git repositories. Use it only for files that you want to ignore *"all the time and for all projects"* – jfs Jun 10 '15 at 19:30
  • @9000: *"isn't working for me"* is not very informative. What commands did you run? What other solutions have you tried? Does any of them work for you? What did you expect to happen? What happens instead? What is your OS, `git` version? If None of the current answers work for you then ask a separate SO question. – jfs Jul 23 '15 at 01:32
  • @J.F.Sebastian I edited `~/.gitignore`, added the line, then ran the command you listed. Also did that in my git repo's `.gitignore` to be safe. I then ran git status and still saw a bunch of untracked, unignored PYC files. The thing is, I ignore other file types like this without problems, but these PYC files specifically refuse to be ignored. If it matters, these PYC files are generated by PyCharm. I have git version 2.3.2 (Apple Git-55). Sorry for the initial bad comment; I was really frustrated with PYC files clogging up my git repo and not being able to ignore them. – sudo Jul 23 '15 at 05:25
  • ^ I meant to say "unstaged", not "untracked". ANYWAY, I did some testing... It turns out that you can't ignore files after they've already been added to git's tracked files and committed at least once. My PYC files had already been committed before, so it's too late. I didn't know that. Worth noting. – sudo Jul 23 '15 at 05:32
  • @9000: [gitignore works only with **untracked** files](http://schacon.github.io/git/gitignore.html). It has no effect otherwise. See the NOTES section in the docs. – jfs Jul 23 '15 at 06:01
38
git config --global core.excludesfile "c:\program files\whatever\global_ignore.txt"

Then, add

*.foo

to that file.

Bombe
  • 81,643
  • 20
  • 123
  • 127
  • 1
    Oops. Use “git config --global core.excludesfile …” to set it in Git’s global configuration. That should work for all projects. – Bombe Mar 13 '09 at 13:39
7

Another way to ignore some file patterns is: add *.pyc to .git/info/excludes, this is a much cleaner way.

demoslam
  • 553
  • 1
  • 6
  • 8
6

add

*.pyc

in .gitignore file

and run this

git config --global core.excludesfile .gitignore

which worked for me

Arun G
  • 1,678
  • 15
  • 17
2

If I recall, there is a way to setup a global .gitignore... But the problem with that is you can't share it with other people (that is, other people who pull your code will see the .pyc files).

So, IMHO, it's better to just add it to the .gitignore for each project you start. I keep a stock .gitignore around for just that reason.

<flamebait>
Or you could switch to bzr, which comes with a sensible default list of things to ignore ^_^
</flamebait>

Jim Puls
  • 79,175
  • 10
  • 73
  • 78
David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • `bzr` is written in Python. It would be unexpected if it couldn't handle Python specific stuff well. – jfs Mar 12 '09 at 21:12
  • 1
    That's true. I don't know the full extent of the defaults, but I know they also handle things like Vim/Emacs swap/backup files, .o files, etc. – David Wolever Mar 12 '09 at 21:52
  • one would think it would be sensible for bzr to ignore .DS_Store files one mac, but it doesn't, svn does ad I recall. I think it only ignores pyc files by default is it's written in python and probably in emacs too :) – Vasil Mar 12 '09 at 22:05
  • Why would the implementation language of bzr dictate what files it ignores? By that logic, Git should ignore *.o files by default. – mipadi Mar 13 '09 at 00:18
0

Couldn't you add

*.pyc

to your .gitignore?

Glen Solsberry
  • 11,960
  • 15
  • 69
  • 94
  • 1
    `.gitignore` for a git repository doesn't work for "all the time and for all projects". – jfs Mar 12 '09 at 21:09