githug (version 0.0.0.9000)

git_uncommit: Undo a Git commit but leave files alone

Description

Make it as if the last Git commit never happened BUT LEAVE YOUR FILES ALONE. This function is "working directory safe" but "history unsafe". Think twice before uncommitting a commit that you have pushed (see Details).

Usage

git_uncommit(ask = TRUE, repo = ".")

Arguments

ask
Whether to confirm that user wants to change history
repo
Path to a Git repo. If unspecified, current working directory is checked to see if it is or is inside a Git repo.

Value

SHA of the commit. The when attribute holds the commit time as POSIXct. An excerpt of the commit message is in the msg_start attribute.

Details

git_uncommit() will not change your files. It just reverses the act of making the most recent Git commit. Even the staged / unstaged status of your modifications is preserved. When might you use this? To undo the last commit so you can stage different changes or files and/or redo your commit, but with a better message. Note that git_amend() might be a more efficient way to do that.

When might you NOT want to use this? If you have already pushed the most recent commit to a remote. It could still be OK if you're sure no one else has pulled. But be prepared to force push in this situation.

git_uncommit() addresses the second most up-voted question on StackOverflow: How to undo last commit(s) in Git?, with over 3.6 million views. It is equivalent to git reset --soft HEAD^, i.e. a soft reset to the commit that is parent to the commit pointed to by current HEAD.

Examples

Run this code
repo <- git_init(tempfile("githug-"))
owd <- setwd(repo)
write("Are these girls real smart or real real lucky?", "max.txt")
git_commit("max.txt",
           message = "Brains'll only get you so far and luck always runs out.")
write("Did I hear somebody say \"Peaches\"?", "jimmy.txt")
git_commit("jimmy.txt", message = "That's the code word. I miss you, Peaches.")
git_history()   ## see? 2 commits
git_status()    ## see? nothing to stage
git_uncommit()  ## roll back most recent commit
git_history()   ## see? only 1st commit is in history
git_status()    ## see? jimmy.txt is a new, staged file
## re-do that 2nd commit but with message in ALL CAPS
git_commit(message = "THAT'S THE CODE WORD. I MISS YOU, PEACHES.")
git_history()   ## see? back to 2 commits
setwd(owd)

Run the code above in your browser using DataCamp Workspace