William Pursell
William Pursell

Reputation: 212298

How do you ensure that a commit is never deleted?

Suppose I have a purely speculative commit that didn't work. I'd like to keep it, but I don't want to make a ref (I don't want a branch or a tag named 'one-single-speculative-commit', and I certainly don't want 100s of them.) I can make a branch name 'speculative', and put all the commits in it, but the commits are totally unrelated to each other so the linkage between a commit and its parent is absurd. Essentially, I'd like to be able to list a bunch of sha1 ids for commits that will not be deleted by gc, but not need a ref for each one. What's a good way to do that? (I don't want to set gc.pruneExpire to a high value, because I do still want to prune things, just want to keep a specific set of commits that are otherwise unreferenced.) In other words:

What is a good way to make a reference to a collection of totally unrelated commits?

Upvotes: 3

Views: 208

Answers (3)

William Pursell
William Pursell

Reputation: 212298

The solution I've decided to go with is relatively simple.

$ git update-ref refs/speculative/SHA1 SHA1

git-gc seems to respect the ref and not delete it. The protected commits can be listed easily with:

$ git show-ref | grep refs/speculative

The only drawback I notice so far is that git fsck --lost-found HEAD reports that they are dangling.

Upvotes: 3

mipadi
mipadi

Reputation: 410792

I'd just tag the commit (using a lightweight tag). I know that's not the solution you really want, but it's the best one in my opinion. Tagged commits will never be expunged, and they won't show up when listing branches. They will when listing tags, but if you really don't want to see them, you could begin each tag with a common prefix, and then use a git alias to filter them out. Something like:

[alias]
    tags = !sh -c 'git tag | grep -v my_prefix'

Upvotes: 4

araqnid
araqnid

Reputation: 133552

You could use git stash, which is essentially a different list connected to a ref called 'stash' that all the tools like git show-branch ignore.

Or you could use a branch for each in a fake remote, e.g. refs/remotes/graveyard/an-experiment which will not be included in --branches but is in --all so won't be pruned.

Upvotes: 2

Related Questions