I can add a file manually to the git object store using for example
echo "foo bar" | git hash-object -w --stdin
This will create a file in .git/objects
with the content foo bar
(plus some header) and name it by its SHA-1 (or SHA-256 depending on --object-type
) hash.
However, that's a dangling object now. Next time git gc
runs and my object is older than the set prune time (2 weeks I think by default), that file will be deleted. It also won't get pushed to the remote repository because it's not reachable yet by any pushable thing (branch or tag).
To reference it, I could either add it to a tree object that's referenced by a commit object or reference it in a tag.
However, adding it to a tree will add it to the file system represented by it (and it must be given a filename) and referencing it with a tag will not make it part of the merkle-tree of a commit (and it won't get pushed/pulled automatically hence).
I would like to add an object to the object store and have it be part of the merkle-tree of my commit (so that it gets pushed/pulled and is protected from garbage collection) but without giving it a filename (i.e. without it ever appearing in the staging area outside of the .git
folder when checking out a commit that references it).
Is this possible somehow with vanilla Git?
Is it for example possible to add entries to a tree object without specifying a file-name for it? If so, how? (git write-tree
takes the files from the staging area so they need to have filenames.)
Or is it maybe possible to reference the object hash from the commit message in a way that tells Git that this object is part of the commit and thus must be pushed/pulled with the commit?
Or can git update-index
be used for this?
question from:
https://stackoverflow.com/questions/66066417/is-it-possible-to-reference-git-objects-from-a-commit-without-adding-them-to-the 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…