{"thread":{"id":"1442","subject":"Extend \"git reset\" to take a reset point","startedAt":"2005-08-07T01:01:03Z","lastAt":"2005-08-07T01:42:33Z","messageCount":2,"participants":["Linus Torvalds","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"6893","messageId":"Pine.LNX.4.58.0508061746530.3258@g5.osdl.org","threadId":"1442","inReplyTo":null,"subject":"Extend \"git reset\" to take a reset point","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2005-08-07T01:01:03Z","receivedAt":"2005-08-07T01:01:03Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\nThis was triggered by a query by Sam Ravnborg, and extends \"git reset\" to \nreset the index and the .git/HEAD pointer to an arbitrarily named point.\n\nFor example\n\n\tgit reset HEAD^\n\nwill just reset the current HEAD to its own parent - leaving the working \ndirectory untouched, but effectively un-doing the top-most commit. You \nmight want to do this if you realize after you committed that you made a \nmistake that you want to fix up: reset your HEAD back to its previous \nstate, fix up the working directory and re-do the commit.\n\nIf you want to totally un-do the commit (and reset your working directory\nto that point too), you'd first use \"git reset HEAD^\" to reset to the\nparent, and then do a \"git checkout -f\" to reset the working directory\nstate to that point in time too.\n\nSigned-off-by: Linus Torvalds <torvalds@osdl.org>\n---\n\nThis is potentially a dangerous command, so maybe we should make it\nask for confirmation first?\n\nOn the other hand, it definitely is convenient: I often end up doing this\nby hand, and clearly other people have hit the \"oops, I want to undo and\nthen re-do those last five commits, I was just a bit too drunk\"\n\nThe old \"git reset\" only reset the index to the current HEAD, which is \nreally only useful if you've tried to do a \"merge\" that failed and that \nyou're giving up on. This one is more useful, but also potentially more \ndangerous - doing a\n\n\tgit reset v0.99.3\n\tgit checkout -f\n\nwill basically revert a tree to some old state, and if you didn't save the \nold point, you may not be able to get back to it (git-fsck-cache will help \nyou, but..)\n\nNot hugely tested, btw. That strange extra \"git-rev-parse\" is _meant_ to\nmake sure that if you reset to a tag, it will always extract the commit ID\nfrom that tag and not reset the HEAD to a tag object.\n\ndiff --git a/git-reset-script b/git-reset-script\n--- a/git-reset-script\n+++ b/git-reset-script\n@@ -1,5 +1,7 @@\n #!/bin/sh\n . git-sh-setup-script || die \"Not a git archive\"\n-git-read-tree --reset HEAD\n+rev=$(git-rev-parse --revs-only --verify --default HEAD \"$@\") || exit\n+rev=$(git-rev-parse --revs-only --verify $rev^0) || exit\n+git-read-tree --reset \"$rev\" && echo \"$rev\" > \"$GIT_DIR/HEAD\"\n git-update-cache --refresh\n rm -f \"$GIT_DIR/MERGE_HEAD\"\n"},{"id":"6894","messageId":"7vmznu4j5i.fsf@assigned-by-dhcp.cox.net","threadId":"1442","inReplyTo":"Pine.LNX.4.58.0508061746530.3258@g5.osdl.org","subject":"Re: Extend \"git reset\" to take a reset point","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-08-07T01:42:33Z","receivedAt":"2005-08-07T01:42:33Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"How about saving the current HEAD to $GIT_DIR/OLD_HEAD just in\ncase?  Then after fixing up the files in the working tree, you\ncould say \"git commit -m OLD_HEAD\".\n"}]}