0

I work with a team in Django project, in the local git with same database. we found that there was a lot of problems with django migrations specially when the team is large.

So we decided to make databases for each developer, sometimes the developer delete them migrations files to solve some problems with migrations.

we get a lot of the conflict in Django migrations files, we decided to add migrations files to gitignore.

Then all the work become more smoothly. but we get some problems we lost the git history for migrations files, this gives problems at updating the project versions from the specific git tag. Also, it will delete the migrations files at every time we make "checkout" in git.

I suggested the following but I did find its answers

make migrations files tracked by local repository and ignored in the remote repository.

Any one have forced this issue?

3
  • 1
    Does this answer your question? Does migrations directory in django project should be pushed to git repo? Commented Nov 25, 2021 at 20:04
  • We are maintaining a large Django project using git and 5 developers. The only workable solution for us was not to push migration files to the master remote git. The production server checks out the files from the git repo as well, but maintains its own migration files. Each developer has his/her own branch including the migration files. When merging into the master branch, the migrations are ignored. Commented Nov 25, 2021 at 20:25
  • @DavidBuck thanks for suggestion. Ok, I must not add migrations files to the ".gitignore", but I must do thing "automatically" to solve the following: 1) stop seen migrations files at the "change" or "stage" area. 2) make accept all current for migrations files at merging, automatically . Commented Nov 27, 2021 at 14:25

1 Answer 1

1

The question as to whether migration files should be committed is pretty well answered by the linked possible duplicate. This answer is not about that: it's about your proposed solution.

The idea of making some file "tracked" by some specific repository is somewhat nonsensical. In Git, a file is tracked if and only if that file is currently in Git's index. Hence we need to address this question:

When is a file in Git's index?

The simple answer is simple—too simple—and confusing and unhelpful: a file is in Git's index when that file is in Git's index. Files go in and out of Git's index all the time, though.

The purpose of Git's index is to help you build the next commit you will make. The files that are in Git's index right now—and this changes from one point in time to the next—are the files that will be in your next commit.

The git add command will, under the right conditions, copy a file from your working tree—the files you can see and work on / with are in your working tree—to Git's index. If there was already some file with that name in Git's index, that copy is kicked out of the index, so that the working tree copy can be copied in. If there was no file with that name in Git's index, the file is now added to Git's index. This copy, once copied into Git's index, remains there, in Git's index, even if you change or remove the working tree copy: these are separate copies.1

Under other conditions, git add will do nothing, or will even remove the index copy of a file. Using git rm --cached will remove the index copy of a file, and using git rm will remove both the index copy and the working tree copy. So you have several Git commands that can affect the index copy: git add, which can replace the index copy or remove it, and git rm, which can remove it.

The conditions under which git add does each of these things are a little complicated, but meant to be simple to use:

  • git add of an all-new file adds it to Git's index;
  • git add of an updated-in-working-tree file kicks out the old copy and adds the new one instead; and
  • git add of a removed file—one that you removed from the working tree, but without using git rm to do that—removes the index copy.

So git add "means" make the index copy match the working tree copy.

The condition under which git add does nothing (except maybe emit a warning that it's not adding the file) occurs when the file (a) isn't already in the index and (b) is listed in a .gitignore. Condition (b) means do not add the file even if I tell you to add it. This does not affect a file that is in the index—see condition (a)—because, well, the file is in the index. However it may have gotten there, it's there now, so git add will update it to match the working tree.

But there is one more key way that files get into Git's index, and that is: whenever you extract (git checkout or git switch) an entire commit, Git reads the commit into the index as part of extracting the commit's files to your working tree. What this means is that checking out a commit that has a migration file will put that file into Git's index. The commits control which files are in the index.

The commits are what two Git repositories share, when you connect them up with git fetch, git pull, and/or git push. A commit, once made, can never be changed in any way. So if someone—anyone—has ever committed some migration file, and you git checkout that commit, that file appears in both your working tree and Git's index. If you then switch from that commit, to some other commit where the file is not in the commit, Git will remove that file from both Git's index and your working tree.

In short, once committed, a file is there forever. Moving from a commit that has the file, to one that doesn't have the file, means delete the file. Moving from a commit that doesn't have the file, to one that does, means add the file. This is not optional. It always happens, every time. The file goes into Git's index or comes out of Git's index; it is now tracked or untracked correspondingly.

You can, having switched to or from such a commit, subsequently alter Git's index using git add and/or git rm. This will change the tracked-ness of the file. But switching to or from another commit updates Git's index, changing the tracked-ness of files.


1The index copy is, however, stored in Git's compressed and de-duplicated format, all ready to go into the next commit. This means that if you use various Git debugging facilities to locate the internal object and dump out its bytes, they won't match the working tree copy's bytes. It's a lossless compression scheme though, so the bytes are all there. It's just that they may take up little or even no disk space, due to the compression and de-duplication tricks Git uses.


The bottom line

The conclusion you should take from all of this is that "tracked-ness" of a file is not up to the repository. It's up to the individual commit. Each commit has some files. Extract that commit extracts those files, which are now tracked. Each commit doesn't have some other files. Extracting that commit will remove those files if they are sitting around in your working tree because they were extracted from a commit that did have them.

This is all controlled by the contents of Git's index, so it's possible—but extremely painful—to check out some commit, getting a bunch of files that are tracked, then use git rm --cached to remove some of those files from Git's index so that Git forgets that they came out of that commit. Switching to some other commit that lacks those files will leave those files in your working tree. But doing this repeatedly is extremely error prone. Don't do this! You will regret it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much. Please could I used hooks in the "--bare" repository to ignored the incoming change from migrations files. If I can do you have an related topic about that.
No, you can't ignore anything about a commit. You either have the commit, and then you have all of it, or you don't have it. If you don't like some commit that you do have, you make a new commit that's different from that commit in some way. You wouldn't normally do this with a Git hook, as that's the wrong place; you would do this in a system that uses Git as its underlying storage facility, but sits above Git and people use this system instead of using Git directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.