首页 > 解决方案 > 在 Django 中迁移时出现 Git 文件问题

问题描述

我的 Django 项目中有以下结构

Django项目结构

gitignore 是https://www.toptal.com/developers/gitignore/api/django建议的

初始化 GIT 的步骤是:使用 apps/A 和 apps/B 创建项目,创建 .gitignore 文件并运行git init.

然后我跑makemigrationsmigrate

当从 master 开始,使用 apps/ZApp 创建一个名为 Z 的新分支时,就会出现问题,创建一个新模型并makemigrationsmigrate该分支执行。因此:

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

模型

然后我申请makemigrationsmigrate返回到主人......当我在主人时,我看到那些应该被忽略的文件,我找不到原因......他们不应该在那里......所有文件应该在各自的分支

在此处输入图像描述

没看懂。。。有人帮帮我


更新:我的 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

标签: djangogitvisual-studio-code

解决方案


这是预期的行为。Git 根本没有对它忽略的文件做任何事情。这意味着如果在您打开一个分支时创建 .pyc 文件,然后切换到另一个分支,则 .pyc 文件不会发生任何事情,因为您所做的只是切换 git 分支,而这些文件会被 git 忽略。

如果你喜欢,你可以添加一个 post-checkout钩子,每次你签出一个分支时删除所有pycache目录和 .pyc 文件。


推荐阅读