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 command git 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).
Related
Git is ignoring the folders in .gitignore file.
Here is my .gitignore : (env is a folder generated by venv in Python )
#folders
env/**
pdfs/**
# files
#source files
/timeformat.py
/pdfreader.py
I already did : git rm -r --cached . then git add . but it's adding all the files under env/Lib/site-packages
What is the problem ?
EDIT :
here is my directory structure :
Change your gitignore from
#folders
env/**
pdfs/**
to:
#folders
env/*
pdfs/*
The reason may be the .gitignore file and env folder not present at same level in directory structure.
You can start with **/dir to ignore the dir folder inside any directory.
In your case:
**/env/
change env/** to env /*
It did the job for me
I have a rule to ignore secret.py (production.py in my case) and once I added .gcloudignore, github stopped following that rule...
Is there some sort of a rule overriding between gitignore and gcloudignore that I am not aware of?
my-project/
.git
.gitignore
my-project/
.gcloudignore
settings/
base.py
local.py
production.py
my .gitignore:
my-project/my-project/settings/production.py
my-project/my-project/settings/local.py
my .gcloudignore:
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore
*.sqlite3
settings/local.py
End result is that the 'local.py' is NOT pushed to google cloud NOR github.
However, 'production.py' IS pushed to github AND gcloud.
If you had previously (perhaps accidentally) submitted a change to git that included my-project/my-project/settings/production.py, then it will remain a part of the repository even if it is subsequently added to .gitignore.
Assuming you are at the root of your project, you can use
$ git log my-project/my-project/settings/production.py
to see its git history. If it is present in your repo, you can do
$ git rm --cached my-project/my-project/settings/production.py
to remove it from the repo, but keep it in your local (working) environment.
Looks like you are enforcing to ignore the rules applied before.
Though make sure that the cloudignore is enable via
gcloud config set gcloudignore/enabled true
And make sure to place .gcloudignore - > in the root of your project / basically where you .git .gitignore reside.
I have a directory structure and files like this
data/
data/a.txt
data/folder/
data/folder/b.txt
data/folder/folder/
data/folder/folder/c.txt
...
a.txt, b.txt, and c.txt are large files that are computer-generated and renewed frequently. They should NOT be backuped -- but I want to backup the directory structure :
data/
data/folder/
data/folder/folder/
How can I do this with rsync and --exclude-from, without specifying every folder, but something like rsync -a data/* --exclude-from=exclude.rsync "" --onlyfoldersandnotfiles""?
Thanks for help !
rsync -a --include='*/' --exclude='*' source/ destination/
Basically, first include all directories, then exclude all files.
$ rsync -a -f"+ */" -f"- *" source/ destination/
"The two -f arguments mean, respectively, "copy all directories" and then "do not copy anything else"."
Further details: http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html
rsync -a -f"-! */" source/ destination/
Filter rule means "Exclude anything that's not a directory."
If you want to sync everything except one folder but you still want to keep the directory structure of that excluded folder, you should do the following:
$ rsync -a -f"+ /var/log/**/*/" -f"- /var/log/**/*" source/ destination/
See the exclude-list and the rsync command as an example.
I have a file architecture like this:
A/folder1/file.tar.gz
B/folder2/file.tar.gz
I have a python script that I want to run on these files
Is there any way I can extract every file and then run my script, using bash or something in python?
By using find you can get your list of files and process them one by one:
for TGZ in $(find . -name "file.tar.gz")
do
WD=$(dirname $TGZ)
cd $WD
tar xzf file.tar.gz
<your python script>
rm *.faa
cd -
done
That would be a direct translation of your words: uncompress the files in the same folder where they are, process them and remove the data. I'm assuming that the extracted files are only *.faa files. There is also quite a lot of directory movement due to your script only working with the filed in the current folder.
Personally, I would feel safer by using a sligtly different approach:
for TGZ in $(find . -name "file.tar.gz")
do
mkdir -p work
cd work
tar xzf $TGZ
<your python script>
cd ..
rm -rf work
done
There is also folder movement, but you always move to the same place and you do a complete cleanup, in case there are other things besides *.faa files.
Regarding your python script... did you though of using a dictionary instead of a bunch of single-letter vars?
base = dict()
base['A'] = 0
base['A'] += 1
That would reduce your code size quite a lot and make it more readable.
You might having a master folder which has all the folders which contain your tar.gz files.
This will extract all files in one cammand
Now lets say 'data' is your root folder which has all the subfolders which further contain your tar.gz files.
now
> for root,dirs,files in os.walk('path to data folder'):
> for name in files:
> if(name.endswith('.gz'):
> #Run your script here
How to add all catalog using GIT witout .pyc file?
git add catalog_name
git commit -m "Update"
git push
What add and where?
use .gitignore file. add '*.pyc' to it.
The file is located in root directory of your repo i.e. where you did git init
Here is a good .gitignore file for python projects, containing common extension to ignore in git commit.
https://github.com/github/gitignore/blob/master/Python.gitignore