How can i manually build git commit objects using git hash-object? I now it works with blobs, and its documentation says it can build different objects by using -t but how do you build a commit with that?
2 Answers
Here's a complete and working example of a script that creates a commit object without ever running git commit:
mkdir project
cd project
git init
hash=`echo -n "" | git hash-object -w --stdin`
tree=`echo -e "100644 blob ${hash}\temptyfile" | git mktree`
commit=`echo -e "yourname\[email protected]\n2013 12:20:15 +0200\ncommittername\[email protected]\n2013 10:13:15 +0200" | git commit-tree ${tree}`
git update-ref refs/heads/master ${commit}
To verify that the script created a commit containing an empty file, run:
git checkout --
git log --oneline
#2abbdc2 yourname [email protected] 2013 12:20:15 +0200 committername [email protected]
Edit: fixed and improved
4 Comments
\t stands for TAB character, so yes, it's \temptyfile. However giving it wrong as \tempfile this only means, it shows up as empfile in the git-worktree. So the filename used for git hash-object does not neccessarily need to be the same for git mktree.echo. Maybe this changed in modern versions of git?For the benefit of the reader:
The accepted answer from Arialdo Martini is fully correct and explains how to create an empty git repo with proper plumbing commands. Please note that his variant works for bare repositories, too (you can create emptyfile in the git-dir with no bad sideeffects).
This answer here sums it up into a script with a minor tweak: The contents of the file is taken from "stdin" and the file can be placed into a sub-directory of the worktree.
Script git-init-with-file-from-stdin.sh new-git-workdir filename 'commit message':
#!/bin/bash
mkdir "$1" &&
cd "$1" &&
git init &&
dir="$(dirname "$2")" &&
name="$(basename "${2:-dummyfile}")" &&
obid="$(git hash-object -w --stdin)" &&
treeid="$(git mktree < <(printf '100644 blob %q\t%q\n' "$obid" "$name"))" &&
if [ . = "$dir" ]; then git read-tree -i "$treeid";
else git read-tree --prefix="$dir/" -i "$treeid"; fi &&
git commit -m "${3:-automatic commit}" &&
git reset --hard
Explained for the call
./git-init-with-file-from-stdin.sh newgitdir path/to/file <<< "hellOw W0rld"
mkdir "$1",cd "$1",git initcreates the newgitworktree and initiailizes it. It is named after the first argument (herenewgitdir), which is assumed to name a nonexisting directory in an existing path.dir="$(dirname "$2")",name="$(basename "${2:-dummyfile}")"extracts the path and the name part of the second argument, which defines the wanted filename. The path is relative to the createdgit-workdir. Note that this argument must not start with a/, else the command fails. Heredir=path/toandname=file. If left away, the new file is called "dummyfile" in the top of thegit-workdir.obid="$(git hash-object -w --stdin)"then stores the object with the information read fromstdininto thegitrepo and stores the SHA (object id) of the new object in the variableobid. In the example, the contents ishellOw W0rldfollowed by anNL.git mktree < <(printf '100644 blob %q\t%q\n' "$obid" "$name")is nearly the same asprintf '100644 blob %q\t%q\n' "$obid" "$name" | git mktreeand creates a proper git-tree.100644is the usual file mode. If you want to create executables, you need100755here.treeid="$(...)"then assigns this to the given variabletreeidif [ . = "$dir" ]; then git read-tree -i "$treeid";this reads this newly created tree into thegitstaging area for later commit. However this is for the case, that the file shall be directly in thegit-workdir.else git read-tree --prefix="$dir/" -i "$treeid"; fiis the same for the case, when you want to put it into a subdirectory of thegit-workdir. The nice thing is, thatgitcreates all intermediate directory nodes for you, automatically. (Sadly--prefix="./"produces an error.)git commit -m "${3:-automatic commit}"then uses the well-known commit to create the commitgit reset --hardthen syncs thegit-worktree with the latest commit.
Changes for bare variant:
Everything works similar, except
git commit(this needs a workdir) andgit reset(you do not need it).replace
git init &&with
git init --bare &&also replace
git commit -m "${3:-automatic commit}" && git reset --hardwith a similar sequence as seen in the accepted answer:
commitid="$(git commit-tree "$(git write-tree)" <<< "${3:-automatic commit}")" && git update-ref "refs/heads/master" "$commitid"
Remarks:
See also https://stackoverflow.com/a/25556917
This here needs
bash, which means, it works for Windows 10, too
1 Comment
git update index command do i need to remove old object manually?
write-treeor justmktree. You can usehash-objectif you really want but it seems like a lot of hassle to me.git cat-file, e.g.git cat-file commit master,git cat-file tree master^{tree}.git rev-parse masterandgit cat-file commit master | git hash-object -t commit --stdinwill give the same result.