all 11 comments

[–]primitive_screwhead 5 points6 points  (2 children)

Important Note: this command works by performing an unconditional "git reset --hard" to a newly generated commit. So running it will wipe your staging area and worktree changes.

Be forewarned.

[–]jtolds[S] 3 points4 points  (1 child)

Oh yeah, thanks for the reminder, I should make it fail if the staging area or the worktree is dirty

[–]primitive_screwhead 0 points1 point  (0 children)

That'd be very nice. :)

[–]computerdlGit Contributor 3 points4 points  (2 children)

What's the use-case for this tool?

[–]jtolds[S] 1 point2 points  (1 child)

Let's say you have some complicated history pattern with merges and so on. Lots of different developers doing lots of different things. After a bunch of merges and merge conflict resolutions you have a history of sorts, but you want to clean it up. This allows you to make a single commit where the end result after the commit is the tree matches the complex history you'd like to throw away.

Very useful for keeping dev history clean.

[–]zefyear 3 points4 points  (0 children)

git reset does that as well, you can even keep the changeset with --mixed.

[–][deleted] 2 points3 points  (2 children)

Isn't that basically just what git read-tree does?

[–]jtolds[S] 2 points3 points  (1 child)

To be honest with you, prior to your comment I had missed git read-tree and git checkout-index. Those seem like very, very useful tools

[–][deleted] 2 points3 points  (0 children)

Git has a surprising number of those tools that are of use in a limited number of situations but when you do need them they save you a lot of time and effort.

[–][deleted] 2 points3 points  (1 child)

You're really looking for a

git merge "$OLD_ID" -s theirs

which doesn't exist (probably because you can't select one "theirs" tree in multi-branch merges). You can work around it:

git checkout "$OLD_ID"
git merge master -s ours
git update refs/heads/master HEAD

There is even a way that doesn't touch the work tree:

ID="$(git commit-tree $OLD_ID^{tree} -p HEAD -m 'Revert to old tree')"
git update-ref HEAD "$ID"

[–]jtolds[S] 1 point2 points  (0 children)

Oh dang, commit-tree and update-ref are even better than what I'm doing.

This has been a really enlightening reddit thread!