githug (version 0.0.0.9000)

git_amend: Re-do the most recent commit

Description

Undo the most recent commit WHILE LEAVING ALL YOUR FILES ALONE, combine those changes with the currently staged changes, and make a new commit. If nothing is currently staged, this is just a way to edit the most recent commit message. This function is "working directory safe" but "history unsafe". Think twice before "amending" a commit that you have pushed (see Details).

Usage

git_amend(message = character(), ask = TRUE, repo = ".")

Arguments

message
The commit message. If not provided and ask = FALSE, the original commit message is reused. If message is not given and ask = TRUE and session is interactive, you get a chance to supply the message, including an option to reuse the original message.
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_amend() will not change your files. It gives you a do over on your most recent Git commit. When might you use this? If you realize the most recent commit included the wrong file changes or had a bad commit message. If you're not ready to re-commit yet, use git_uncommit() to just undo the commit.

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_amend() addresses the fourth most up-voted question on StackOverflow: Edit an incorrect commit message in Git, with over 1.7 million views. It is equivalent to git commit --amend -m "New commit message".

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 = "lines from max")
write("Did I hear somebody say \"Peaches\"?", "jimmy.txt")
git_commit("jimmy.txt", message = "lines from some guy")
git_history()   ## note the SHA of the most recent commit

## fix the previous commit message
git_amend(message = "lines from jimmy", ask = FALSE)
git_history()   ## note the SHA of most recent commit has changed

setwd(owd)

Run the code above in your browser using DataLab