Cheung
Cheung

Reputation: 15552

Commit a SQL Server LocalDB to GIT repo

I have a SOLO project is on early phrase and on-going design and change the scheme any times.

And I want commit the projects with the LocalDB .MDF & .LDF files (under 50MB) to the REPO.

The reason I commit the localdb is not for versioning, it is just backup.

It is success commit or clone the repo and restore the database.

But should I do something to make GIT better deal with .MDF and .LDF?

For example, set the parameter on gitattribute to force Git to consider .MDF/.LDF as binary file?

Upvotes: 0

Views: 2241

Answers (1)

kostix
kostix

Reputation: 55573

A couple of ideas:

  • Use a separate branch to store these files; otherwise they are versioned (with the rest of your project).

    You might use git checkout --orphan + git rm -rf . to prepare a branch with no ancestry. Then add just these two files, and commit. Do that each time you have something new to "save".

    You can fetch any state of these files not checking out the branch which contains them by doing something like

    C:\> git show my_database_branch:LocalDB.MDF > LocalDB.MDF
    
  • Why use branches at all? Git, above anything else, is a set of tools to support "versioned filesystem", and it's able to store in its database any object you tell it to; observe:

    C:\> git hash-object -w LocalDB.MDF
    44931246e7973736c9635ea0b715c882b248297f
    C:\> git tag 44931 localdb.mdf
    C:\> git push origin -f localdb.mdf
    

    or you can combine the first two steps info one (requires Git Bash):

    $ git tag $(git hash-object -w LocalDB.MDF) localdb.mdf
    $ git push origin -f localdb.mdf
    

    That is, you have binary object containing your database file in the Git's object database, and you have a tag referencing it. You can push and fetch that tag and recover the data from that object easily:

    C:\> git show localdb.mdf > LocalDB.MDF
    

    The benefit of this approach is that when you later decide to make your history public, you will want to prettify it and pretend those database files were never committed. While this is doable if they have been committed normally, with all the other files, it's hard. Keeping them on a separate branch, or "directly" in the database makes things much easier: just delete the branch or tag(s) referencing these files.

  • Why not use the SQL Server Studio's ability to export the database's schema (and data, if needed) as a script?

  • Keep in mind that Git is case-sensitive, so naming files LocalDB.MDF is asking for trouble. I'd rename them to localdb.mdf or something like this.

Upvotes: 1

Related Questions