Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

Is it possible to reference Git objects from a commit without adding them to the tree-object filesystem?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can create a non-branch reference that does not share any history or name with the other branches and add the file there. Chances are you do need to track changes to the file at some point so this leaves you with the option to do that later.

For example:

git checkout --orphan temporary-branch
git reset --hard
git add the_file
git commit -m 'Add the file'
git update-ref refs/hook-metadata/foo temporary-branch
git branch -D temporary-branch

Reference:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...