0

I have the following structure in my Django project

DjangoProject Struct

The gitignore is the one suggested by https://www.toptal.com/developers/gitignore/api/django

The steps to initialize GIT were: Create the project with apps/A and apps/B, create the .gitignore file and run git init.

Then I ran makemigrations and migrate

The problem occurs when, starting from master, a new branch called Z is created with an apps/ZApp, a new model is created and makemigrations and migrate are executed from that branch. Thus:

$ git checkout -b Z
$ cd apps
$ django-admin startapp Z
$ cd ..

model

Then I apply makemigrations and migrate and return to master... When I'm in master, I see those files that should be ignored and I can't find the reason for this ... They shouldn't be there ... All files should be in their respective branch

enter image description here

I don't understand ... someone to help me


UPDATE : My gitignore

### Personal ###
secret.json

### Django ###
*.log
*.pot
*.pyc
__pycache__/
local_settings.py
db.sqlite3
db.sqlite3-journal
media

# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/
# in your Git repository. Update and uncomment the following line accordingly.
# <django-project-name>/staticfiles/

### Django.Python Stack ###
# Byte-compiled / optimized / DLL files
*.py[cod]
*$py.class

# C extensions
*.so


# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
pytestdebug.log

# Translations
*.mo

# Django stuff:

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/
doc/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
#   However, in case of collaboration, if having platform-specific dependencies or dependencies
#   having no cross-platform support, pipenv may install dependencies that don't work, or not
#   install all needed dependencies.
#Pipfile.lock

# poetry
#poetry.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
# .env
.env/
.venv/
env/
venv/
ENV/
env.bak/
venv.bak/
pythonenv*

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# operating system-related files
# file properties cache/storage on macOS
*.DS_Store
# thumbnail cache on Windows
Thumbs.db

# profiling data
.prof
Javier Valero
  • 113
  • 1
  • 2
  • 7
  • The files should be ignored from *Git*. If you're looking at a file explorer you should still see the files because they're actually there. You just shouldn't see them in your list of pending changes for creating a commit. – TTT Apr 21 '21 at 20:46
  • I'm already ignoring the files from git ... I'm going to update with the content of the gitignore – Javier Valero Apr 21 '21 at 20:50
  • I'm sorry, I didn't mean you had your .gitignore wrong... That looks fine. I meant that the view you're showing appears to be File Explorer, and the files should appear there (I assume). But take a look at your Changes view for what you're going to commit, and you should not see them there. – TTT Apr 21 '21 at 20:55
  • They should not be seen because my changes in my branch stayed in my branch ... When I return to master I should not see anything that I did in the branch ... or so I think so far. – Javier Valero Apr 21 '21 at 20:56
  • Ah! Now that the files are ignored by Git, they are untracked, so they will not be deleted when you switch branches. Solve one problem, create another. ;) – TTT Apr 21 '21 at 20:58
  • Basically, you just need to delete untracked files if you don't want to see them. I'm sure this has been asked but I don't have time to find the perfect answer. This one is close though: https://stackoverflow.com/q/3885850/184546 – TTT Apr 21 '21 at 21:07
  • I don't need these files ... I need the ignored files to be kept in the git branch ... that's exactly what gitignore should be for – Javier Valero Apr 21 '21 at 21:43
  • You may be misunderstanding what .gitignore does. When you ignore a file, you are telling Git to ignore them. So when you switch branches, Git ignores them and they stay there. You said, "I don't need these files". If that's true then you can just delete them. But you also said, "I need the ignored files to be kept in the git branch." In that case, don't ignore them, and instead add them and commit them to the branch. – TTT Apr 21 '21 at 21:49
  • I mean: I don't need these files from the branch in master, because that's why I made the changes to a branch. The files should stay on the branch and not be returned to master. That is precisely why I included them in the gitignore. What you develop in a branch must stay in a branch, right? or at least that's what I know. – Javier Valero Apr 21 '21 at 21:53
  • You'll have to commit them on that branch if you want them to stay with that branch, and disappear when you switch to other branches. – TTT Apr 21 '21 at 22:02
  • that's exactly what I do :( I do in a branch: `# Create a django model.` `makemigrations` `migrate` `git add .` `git commit -m "msg"` and when I return to master, the files that I put in the photo with a pink arrow come out. – Javier Valero Apr 21 '21 at 22:17

1 Answers1

1

This is expected behavior. Git isn't doing anything at all to files it ignores. That means if .pyc files are created while you have one branch open, then you switch to another branch, nothing will happen to the .pyc files, because all you've done is switch git branches, and those files are ignored by git.

If you like, you can add a post-checkout hook that deletes all pycache directories and .pyc files each time you check out a branch.

Greg Kaleka
  • 1,942
  • 1
  • 15
  • 32