How can I ignore .开发者_如何学编程pyc
files in git?
If I put it in .gitignore
it doesn't work. I need them to be untracked and not checked for commits.
You should add a line with:
*.pyc
to the .gitignore
file in the root folder of your git repository tree right after repository initialization.
As ralphtheninja said, if you forgot to to do it beforehand, if you just add the line to the .gitignore
file, all previously committed .pyc
files will still be tracked, so you'll need to remove them from the repository.
If you are on a Linux system (or "parents&sons" like a MacOSX), you can quickly do it with just this one line command that you need to execute from the root of the repository:
find . -name "*.pyc" -exec git rm -f "{}" \;
This just means:
starting from the directory i'm currently in, find all files whose name ends with extension
.pyc
, and pass file name to the commandgit rm -f
After *.pyc
files deletion from git as tracked files, commit this change to the repository, and then you can finally add the *.pyc
line to the .gitignore
file.
(adapted from http://yuji.wordpress.com/2010/10/29/git-remove-all-pyc/)
You have probably added them to the repository before putting *.pyc
in .gitignore
.
First remove them from the repository.
Put it in .gitignore
. But from the gitignore(5)
man page:
· If the pattern does not contain a slash /, git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file). · Otherwise, git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, "Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".
So, either specify the full path to the appropriate *.pyc
entry, or put it in a .gitignore
file in any of the directories leading from the repository root (inclusive).
i try to use the sentence of a prior post and don't work recursively, then read some help and get this line:
find . -name "*.pyc" -exec git rm -f "{}" \;
p.d. is necessary to add *.pyc in .gitignore file to maintain git clean
echo "*.pyc" >> .gitignore
Enjoy.
If you want to ignore '.pyc' files globally (i.e. if you do not want to add the line to .gitignore file in every git directory), try the following:
$ cat ~/.gitconfig
[core]
excludesFile = ~/.gitignore
$ cat ~/.gitignore
**/*.pyc
[Reference]
https://git-scm.com/docs/gitignore
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.
A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".
if you have committed in the repo, just
- go to the folder
/__pycache__
, - delete all of them (no worries, they are temporary files and generated repeatedly)
- have a new commit, such as 'update gitignore'
- you are done!
.pyc
will not appear again.
Thanks @Enrico for the answer.
Note if you're using virtualenv you will have several more .pyc
files within the directory you're currently in, which will be captured by his find command.
For example:
./app.pyc
./lib/python2.7/_weakrefset.pyc
./lib/python2.7/abc.pyc
./lib/python2.7/codecs.pyc
./lib/python2.7/copy_reg.pyc
./lib/python2.7/site-packages/alembic/__init__.pyc
./lib/python2.7/site-packages/alembic/autogenerate/__init__.pyc
./lib/python2.7/site-packages/alembic/autogenerate/api.pyc
I suppose it's harmless to remove all the files, but if you only want to remove the .pyc
files in your main directory, then just do
find "*.pyc" -exec git rm -f "{}" \;
This will remove just the app.pyc
file from the git repository.
Zachary's answer is correct. The reason that it's likely correct in your case is that gitignore will not work for files that preexist the gitignore (or the repository).
精彩评论