16

I'm ready to push my existing Django project (which i've been running in a local environment) to a Bitbucket repository so I can run it on a public server. At the moment I feel like there's a lot of files created in local development that needs to be added to .gitignore.

I found this .gitignore file on github however I still feel it's missing some things, for example it doesn't seem to remove files from each migrations folders. There's also a lot of stuff there that I don't know what they do - I understand not all of it is needed. Any advice is appreciated.

2
  • you think you don't need to run migrations on public server? Dec 3, 2017 at 8:38
  • No I'm referring to the migration files inside the migrations folders. When I start development I need them to be empty to start afresh.
    – Zorgan
    Dec 3, 2017 at 8:56

3 Answers 3

20

You could consider a gitignore tailored for Django project instead.

And don't forget that, if you already have added and committed a folder content, you will need to remove them before your .gitignore can take effect.

git rm --cached -r afolder/

However, by default, migration is not ignored. (you can skip some of those migration steps)
As pointed out by Ora in the comments, see "Should I be adding the Django migration files in the .gitignore file?".

So, do not add migration/ to your .gitignore.

5
  • 4
    you should not ignore the migrations. see here: stackoverflow.com/questions/28035119/…
    – Ora
    Dec 3, 2017 at 8:40
  • @Ora I know: I was editing the answer to that effect.
    – VonC
    Dec 3, 2017 at 8:40
  • Sorry I'm abit confused - are you saying to not add any pattern in .gitignore to block the migrations content, so to simply remove them after pushing them to the repo? by using git rm --cached -r migration/? @VonC
    – Zorgan
    Dec 3, 2017 at 8:59
  • You shouldn't remove them at all. They are part of your codebase. Dec 3, 2017 at 10:09
  • @Zorgan Yes, don't remove them. My remark was more generic about the case where a .gitignore seems to "not work". I have edited the answer accordingly.
    – VonC
    Dec 3, 2017 at 10:41
2

The best example I have found is here: https://djangowaves.com/tips-tricks/gitignore-for-a-django-project/

This is not my work!!! I don't want to take credit for someone else's good collection! Not everything in there is relevant. The author documents it well, though, so you can boil it down to what you need.

I would also add:

**/migrations
*.DS_Store
**/__pycache__
**/.DS_Store
1
  • 6
    migrations are supposed to be shipped, why would you ignore these? Sep 20, 2021 at 17:08
0

I actually don't know how did you add migrations directories. If you already committed to git with those directories, then simply add migrations/* to .gitignore and delete all the migrations folder. Then commit and push. After this, you will never see any migrations changes to git until you forcefully staged and commit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.