Quarkstrudel im Kopf

June 29, 2015 / Martin

Save a File in Git

I’ve once read about a neat trick to save a file in a git repository but without using a history to track it. Unfortunately, I cannot find the reference anymore. In order that I don’t forget about it, I’ve added this small tutorial for my future me:

  #!/bin/bash

  # Let's go somewhere safe first.
  cd /tmp/
  mkdir asdf
  cd asdf

  # Initialize the repository.
  git init .

  # Create some content.
  echo "Hallo" > test.txt

  # Save the file inside git and tag it with "marker" so it's not garbage
  # collected and easy to find.
  git hash-object -w test.txt | xargs git tag marker

  # We can now cat the content with:
  git cat-file -p marker | less

  # If we delete the tag the object will become a dangling object.
  # This should list a hash of the text file.
  git tag -d marker
  git fsck --unreachable

  # Eventually this object will become garbage collected. We can force it with.
  git gc --prune=now

  # This should end up with an uncomplaining fsck:
  git fsck --unreachable