简体   繁体   English

从 Git 远程存储库中删除 .pyc 文件

[英]Remove .pyc files from Git remote repository

Accidentally, I have pushed the .pyc files to the master repository.偶然地,我已将 .pyc 文件推送到主存储库。 Now I want to delete them but I can´t do it.现在我想删除它们,但我不能这样做。 Is there any way to remove them directly from the Bitbucket site?有什么办法可以直接从 Bitbucket 站点中删除它们?

  1. Remove .pyc files using git rm *.pyc .使用git rm *.pyc删除.pyc文件。 If this not work use git rm -f *.pyc如果这不起作用,请使用git rm -f *.pyc
  2. Commit git commit -a -m 'all pyc files removed'提交git commit -a -m 'all pyc files removed'
  3. Push git pushgit push
  4. In future commits you can ignore .pyc files by creating a .gitignore file在将来的提交中,您可以通过创建.gitignore文件来忽略.pyc文件

No, you cannot delete them directly from the BitBucket interface but you can delete them in your local checkout and find ./ -type f -name '*.pyc' -exec git rm {} \\;不,您不能直接从 BitBucket 界面删除它们,但您可以在本地结帐中删除它们并find ./ -type f -name '*.pyc' -exec git rm {} \\; ( or simply git rm each pyc file one by one ). (或者简单地 git rm 每个 pyc 文件一个一个)。 Then commit/push your changes.然后提交/推送您的更改。

Finally, to avoid ever making the same mistake again you may create a file at the root of your repo and name it '.gitignore' with the contents:最后,为了避免再次犯同样的错误,您可以在存储库的根目录下创建一个文件,并将其命名为“.gitignore”,内容如下:

*.pyc
*~
*.swp

*~ and ~.swp are other commonly forgotten file types that are often accidentally pushed. *~ 和 ~.swp 是其他经常被意外推送的通常被遗忘的文件类型。 See the github doc on gitignore https://help.github.com/articles/ignoring-files (and their repo of .gitignore files for some nice defaults).请参阅 gitignore https://help.github.com/articles/ignoring-files上的 github 文档(以及他们的 .gitignore 文件仓库以获得一些不错的默认值)。

git rm *.pyc --cached
git commit -a -m'remove pyc from index'
git push

PS: I see the date of question, but this solution looks better, imho. PS:我看到了问题的日期,但这个解决方案看起来更好,恕我直言。 May be it'll help someone.. .也许它会帮助某人......

这对我有用,

find . -name '*.pyc' | xargs -n 1 git rm --cached

I used simeg's solution but also wanted to deleted tons of *.pyc files added by mistake to a branch.我使用了 simeg 的解决方案,但也想删除大量错误添加到分支的 *.pyc 文件。 I used awk to delete them from cache recursively.我使用 awk 递归地从缓存中删除它们。

git status | awk '{if($1=="modified:" && $2!=".gitignore") ; system("git rm --cached "$2)}'

Then, I deleted the files from my local然后,我从本地删除了文件

find . -name *.pyc -delete

To remove all .pyc files use git rm -rf *.pyc要删除所有.pyc文件,请使用git rm -rf *.pyc

Then add the *.py[co] to your .gitignore file.然后将*.py[co]添加到您的.gitignore文件中。 (This will prevent .pyc and .pyo files from getting committed in the future commits) (这将防止 .pyc 和 .pyo 文件在以后的提交中被提交)

Because in default Bitbucket, there is no .gitignore file in the repo,so you can do :因为在默认的 Bitbucket 中,repo 中没有 .gitignore 文件,所以你可以这样做:

  1. you can create local .gitignore(should not be pushed) and add *.pyc as a line;您可以创建本地 .gitignore(不应推送)并将 *.pyc 添加为一行;
  2. you can copy the .gitignore in Github repo and add *.pyc as a line in this file!您可以复制 Github 存储库中的 .gitignore 并在此文件中添加 *.pyc 作为一行! You can push it or keep it in your local repo!您可以推送它或将其保存在您的本地存储库中!

Quick way with PyDev for eclipse .使用PyDev 进行 eclipse 的快速方法。

Go to the PyDev Package Explorer of your project and do:转到项目的 PyDev Package Explorer 并执行以下操作:


  • right click + Pydev / Remove *.pyc *.pyo and *$py.class File右键单击+ Pydev /删除 *.pyc *.pyo 和 *$py.class 文件

    a window will popup telling you how many files have been deleted.将弹出一个窗口,告诉您已删除多少文件。

Optional: Commit your change to the team/server:可选:将您的更改提交给团队/服务器:

  • right click + team / commit右键单击+团队/提交

In the commit window you shouldn't see any .pyc available to add as we deleted them.在提交窗口中,当我们删除它们时,您不应该看到任何可添加的 .pyc。 Also if you committed such files before then you should be able to commit their "deletion" now.此外,如果您之前提交过此类文件,那么您现在应该能够提交它们的“删除”。

===> Your local and server repository are now free of *.pyc *.pyo and *$py.class File :) ===>您的本地和服务器存储库现在没有 *.pyc *.pyo 和 *$py.class 文件:)

一个有趣的单线:

git status | grep pyc | sed -e 's/ new file: //g' | xargs -I {} git rm --cached {}

another liner for fun to remove all the pyc files.另一个有趣的衬垫可以删除所有 pyc 文件。

find .找 。 -name '*.pyc' -exec git rm {} \\; -name '*.pyc' -exec git rm {} \\;

don't forget to follow the steps in other answers to commit and add gitignore.不要忘记按照其他答案中的步骤提交和添加 gitignore。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM