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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26021040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:31:54  来源:igfitidea点击:

Remove .pyc files from Git remote repository

gitpyc

提问by ePascoal

Accidentally, I have pushed the .pyc files to the master repository. Now I want to delete them but I can′t do it. Is there any way to remove them directly from the Bitbucket site?

偶然地,我已将 .pyc 文件推送到主存储库。现在我想删除它们,但我不能这样做。有什么办法可以直接从 Bitbucket 站点中删除它们?

回答by Falcoa

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

回答by smassey

No, you cannot delete them directly from the BitBucket interface but you candelete them in your local checkout and find ./ -type f -name '*.pyc' -exec git rm {} \;( or simply git rm each pyc file one by one ). Then commit/push your changes.

不,您不能直接从 BitBucket 界面删除它们,但您可以在本地结帐和find ./ -type f -name '*.pyc' -exec git rm {} \;(或简单地 git rm 每个 pyc 文件一个一个)中删除它们。然后提交/推送您的更改。

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. See the github doc on gitignore https://help.github.com/articles/ignoring-files(and their repo of .gitignore files for some nice defaults).

*~ 和 ~.swp 是其他经常被意外推送的通常被遗忘的文件类型。请参阅 gitignore https://help.github.com/articles/ignoring-files上的 github 文档(以及他们的 .gitignore 文件仓库以获得一些不错的默认值)。

回答by Dimitriy Kutnyakov

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. May be it'll help someone.. .

PS:我看到了问题的日期,但这个解决方案看起来更好,恕我直言。也许它会帮助某人......

回答by Adel

This works for me,

这对我有用,

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

回答by Angel Alvarado

I used simeg's solution but also wanted to deleted tons of *.pyc files added by mistake to a branch. I used awk to delete them from cache recursively.

我使用了 simeg 的解决方案,但也想删除大量错误添加到分支的 *.pyc 文件。我使用 awk 递归地从缓存中删除它们。

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

Then, I deleted the files from my local

然后,我从本地删除了文件

find . -name *.pyc -delete

回答by Rao

To remove all .pycfiles use git rm -rf *.pyc

要删除所有.pyc文件,请使用git rm -rf *.pyc

Then add the *.py[co]to your .gitignorefile. (This will prevent .pyc and .pyo files from getting committed in the future commits)

然后将其添加*.py[co]到您的.gitignore文件中。(这将防止 .pyc 和 .pyo 文件在以后的提交中被提交)

回答by Aton Lerin

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

    a window will popup telling you how many files have been deleted.

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

    将弹出一个窗口,告诉您已删除了多少文件。

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. Also if you committed such files before then you should be able to commit their "deletion" now.

在提交窗口中,当我们删除它们时,您不应该看到任何可添加的 .pyc。此外,如果您之前提交过此类文件,那么您现在应该能够提交它们的“删除”。

===> Your local and server repository are now free of *.pyc *.pyo and *$py.class File :)

===>您的本地和服务器存储库现在没有 *.pyc *.pyo 和 *$py.class 文件:)

回答by u4212894

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

回答by Monkpit

A one-liner for fun:

一个有趣的单线:

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

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

回答by 3white

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。