Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git commit and push a binary file, but don't keep history

Tags:

git

c#

release

I have a .NET C# repository. Included is a binary executable which builds a database based on a number of regularly updated SQL scripts also in the same repository. Naturally, the bin folder is ignored using .gitignore.

However, I have some non-developer users who need to get regular updates to this file. Ideally what I'd like to do is put a simple batch script on their machine that does a git pull and receives the new binary. However, I really don't want to track history to this file and clog my repository with unnecessary data.

Is it possible to commit a file without retaining history of that file? So every time I commit the file, it just replaces the object stored in the repository, instead of creating a new version of that object.

If not, do any of you have any very simple solutions for this. The file is just an internal utility used by 5 or 6 staff members, so any kind of hack solution will do.

like image 323
Adam Avatar asked Apr 02 '14 23:04

Adam


People also ask

Should binary files be stored in git?

You should use Git LFS if you have large files or binary files to store in Git repositories. That's because Git is decentralized. So, every developer has the full change history on their computer.

Should I commit binary files?

It's important to never commit binary files because once you've commit them they are in the repository history and are very annoying to remove. You can delete the files from the current version of the project - but they'll remain in the repository history, meaning that the overall repository size will still be large.

Does git compress binary files?

It can, literally, compress (or "deltify") any binary data against any other binary data—but the results will be poor unless the inputs are well-chosen. It's the input choices that are the real key here. Git also has a technical documentation file describing how objects are chosen for deltification.

Does git save history?

Git stores the complete history of your files for a project in a special directory (a.k.a. a folder) called a repository, or repo.


2 Answers

You can create a blob for the binary file and tag the blob, when you replace the binary file, you actually create a new blob, prune the old one, delete the tag, create a new tag pointing the new blob.

Follow the following steps to create a blob and tag the blob:

  1. cat my_binary_file | git hash-object --stdin -w | xargs git tag -a tag_my_binary_file -m 'this is the message describing the binary file'
  2. git push --tags

Now you have created a blob for my_binary_file and created the annotated tag tag_my_binary_file pointing to the blob, and pushed the tag and blob to upstream. You can checkout the binary file from another client with the following steps:

  1. git fetch --tags
  2. git cat-file -p tag_my_binary_file | head -1 | cut -d" " -f2 | xargs git show > my_binary_file

Now you will find your binary file named my_binary_file in the current directory.

For replacing the file, follow the following steps:

  1. git tag -d tag_my_binary_file
  2. git gc; git prune
  3. repeat the steps for creating blob and tag
like image 36
neevek Avatar answered Oct 03 '22 14:10

neevek


You can maintain that file on a separate orphan branch (git checkout --orphan NEWBRANCH), and commit your file there.

When you need to replace the file, commit it with --amend and then force-push that branch.

This will overwrite previous commit, so you will only ever have single revision of the file at any given time. On the client side you can get content of the file either by checking out the branch with the file or by using git show branch:file > file

like image 163
ArtemB Avatar answered Oct 03 '22 14:10

ArtemB