Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically remove *.pyc files and otherwise-empty directories when I check out a new branch
    primarykey
    data
    text
    <p>So here's an interesting situation when using git and python, and I'm sure it happens for other situations as well.</p> <p>Let's say I make a git repo with a folder /foo/. In that folder I put /foo/program.py. I run program.py and program.pyc is created. I have *.pyc in the .gitignore file, so git doesn't track it. </p> <p>Now let's say I make another branch, dev. In this dev branch, I remove the /foo/ folder entirely.</p> <p>Now I switch back to the master branch, and /foo/ reappears. I run the program.py and the program.pyc file reappears. All is well.</p> <p>I switch back to my dev branch. The /foo/ directory should disappear. It only exists in the master branch, not the dev branch. However, it is still there. Why? Because the ignored program.pyc file prevents the folder from being deleted when switching branches.</p> <p>The solution to this problem is to recursively delete all *.pyc files before switching branches. I can do that easily with this command.</p> <pre><code>find . -name "*.pyc" -exec rm '{}' ';' </code></pre> <p>The problem is that it is annoying to have to remember to do this almost every time I change branches. I could make an alias for this command, but then I still have to remember to type it every time I change branches. I could also make an alias for git-branch, but that's no good either. The git branch command does other things besides just change branches, and I don't want to delete all pyc files every time I use it. Heck, I might even use it in a non-python repo, then what?</p> <p>Is there a way to set a git hook that only executes when I change branches? Or is there some other way to set all *.pyc files to get erased whenever I switch branches?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload