Saturday 2 May 2015 — This is over nine years old. Be careful.
Working in a Python project, it’s common to have a clean-up step that deletes all the .pyc files, like this:
$ find . -name '*.pyc' -delete
This works great, but there’s a slight chance of a problem: Git records information about branches in files within the .git directory. These files have the same name as the branch.
Try this:
$ git checkout -b cleanup-all-.pyc
This makes a branch called “cleanup-all-.pyc”. After making a commit, I will have files named .git/refs/heads/cleanup-all-.pyc and logs/refs/heads/cleanup-all-.pyc. Now if I run my find command, it will delete those files inside the .git directory, and my branch will be lost.
One way to fix it is to tell find not to delete the file if it’s found in the .git directory:
$ find . -name '*.pyc' -not -path './.git/*' -delete
A better way is:
$ find . -name '.git' -prune -o -name '*.pyc' -exec rm {} \;
The first command examines every file in .git, but won’t delete the .pyc it finds there. The second command will skip the entire .git directory, and not waste time examining it.
UPDATE: I originally had -delete in that latter command, but find doesn’t like -prune and -delete together. It seems simplistic and unfortunate, but there it is.
Comments
I've learned a lot about find with this post, some of it I don't agree with, but I've learned it... :)
You can probably use `git reflog` to find out where it was pointing, do something like `git branch branchname abc123hashhere`. Not tested, but think this works..
> # ignore python compiled files:
> *.py[cod]
> " >> >> .gitignore; git commit -m "ignore python compiled files"
;)
On a more serious note, why would you commit Python binaries to your repo?
Add a comment: