简体   繁体   中英

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM