自动删除* pyc文件和其它空目录,当我检查出一个新的分支 [英] Automatically remove *.pyc files and otherwise-empty directories when I check out a new branch

查看:185
本文介绍了自动删除* pyc文件和其它空目录,当我检查出一个新的分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里有一个有趣的情况,我敢肯定,它发生的其他情形也是如此。

So here's an interesting situation when using git and python, and I'm sure it happens for other situations as well.

比方说我做一个文件夹/富/一个混帐回购协议。在该文件夹我把/foo/program.py。我跑program.py并创建program.pyc。我在.gitignore文件以* .pyc文件,所以Git并不跟踪它。

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.

现在让我们说我做另一个分支,开发。在这种Dev分支,我完全删除/富/文件夹中。

Now let's say I make another branch, dev. In this dev branch, I remove the /foo/ folder entirely.

现在我切换回主分支,和/富/再次出现。我运行program.py和program.pyc文件再次出现。一切都很好。

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.

我切换回我的Dev分支。在/富/目录应该消失。它只存在于主分支,而不是Dev分支。然而,它仍然存在。为什么?由于忽略program.pyc文件prevents被删除的文件夹切换分支时。

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.

这个问题的解决方案是切换分支前递归删除所有* pyc文件。我能做到这一点很容易与此命令。

The solution to this problem is to recursively delete all *.pyc files before switching branches. I can do that easily with this command.

find . -name "*.pyc" -exec rm '{}' ';'

问题是,它是恼人一定要记住这几乎是我改变分支都必须这样做。我可以让这个命令的别名,但我还是要记得每次我修改分支时键入它。我也可以做出混帐分支的别名,但没有不太妙。 git的分支指令做其他的事情,除了只是改变树枝,我不想每次我用它的时间来删除所有的pyc文件。哎呀,我甚至在非蟒蛇回购使用它,然后呢?

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?

有没有一种方法来设置一个Git挂钩,只有当我改变分支执行?或者是有一些其他的方式来设置所有* pyc文件到将被删除时,我改用树枝?

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?

推荐答案

有一个结账后钩,被放置在git的/挂钩/后结账。有可能是一个样本那里,可能.sample或可能不是可执行的命名,这取决于你的Git版本。简短说明:获得三个参数,previous HEAD,新HEAD,一个标志是1,如果分行变更和0,如果它只是一个文件签出。参见男人githooks 了解更多信息!你应该能够写一个shell脚本来完成你需要什么,并把它放在那里。

There is a post-checkout hook, to be placed in .git/hooks/post-checkout. There's probably a sample there, possibly named .sample or possibly not executable, depending on your git version. Short description: it gets three parameters, the previous HEAD, the new HEAD, and a flag which is 1 if the branch changed and 0 if it was merely a file checkout. See man githooks for more information! You should be able to write a shell script to do what you need and put it there.

编辑:我知道你正在寻找这样做pre-结账,这样结账自动清理目录,这成为空。有没有pre-结帐钩,虽然如此,你必须使用你的脚本删除的目录了。

I realize you're looking to do this pre-checkout, so that the checkout automatically cleans up directories which become empty. There's no pre-checkout hook, though, so you'll have to use your script to remove the directories too.

另外要注意的:
别名是gitconfig,它可以是本地的存储库的一部分(在的.git /配置,而不​​是〜/的.gitconfig)。如果您选择使用别名来做到这一点(对git的结帐,不git的分支),你可以很容易地把它们只在Python相关的资料库。在这种情况下,我会做一个别名专门为此目的(例如CC结帐干净)。您仍然可以使用Google Checkout(或它的另一种形式的别名),如果你不想清理的pyc文件。

Another note: Aliases are part of gitconfig, which can be local to a repository (in .git/config, not ~/.gitconfig). If you choose to do this with aliases (for git-checkout, not git-branch) you can easily put them only in python-related repositories. Also in this case, I'd make an alias specifically for this purpose (e.g. cc for checkout clean). You can still use checkout (or another aliased form of it) if you don't want to clean up pyc files.

这篇关于自动删除* pyc文件和其它空目录,当我检查出一个新的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆