diff options
author | Junio C Hamano <junkio@cox.net> | 2006-03-02 21:04:05 -0800 |
---|---|---|
committer | Junio C Hamano <junkio@cox.net> | 2006-03-02 21:08:33 -0800 |
commit | b4019f045646b1770a80394da876b8a7c6b8ca7b (patch) | |
tree | 8524e5abce51e9e029f3e0d1e9b33ad11951b309 | |
parent | show-branch --topics (diff) | |
download | tgif-b4019f045646b1770a80394da876b8a7c6b8ca7b.tar.xz |
git-commit --amend
The new flag is used to amend the tip of the current branch. Prepare
the tree object you would want to replace the latest commit as usual
(this includes the usual -i/-o and explicit paths), and the commit log
editor is seeded with the commit message from the tip of the current
branch. The commit you create replaces the current tip -- if it was a
merge, it will have the parents of the current tip as parents -- so the
current top commit is discarded.
It is a rough equivalent for:
$ git reset --soft HEAD^
$ ... do something else to come up with the right tree ...
$ git commit -c ORIG_HEAD
but can be used to amend a merge commit.
Signed-off-by: Junio C Hamano <junkio@cox.net>
-rwxr-xr-x | git-commit.sh | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/git-commit.sh b/git-commit.sh index f7ee1aadee..c9c22abc5e 100755 --- a/git-commit.sh +++ b/git-commit.sh @@ -3,7 +3,7 @@ # Copyright (c) 2005 Linus Torvalds # Copyright (c) 2006 Junio C Hamano -USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-e] [--author <author>] [[-i | -o] <path>...]' +USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>) [--amend] [-e] [--author <author>] [[-i | -o] <path>...]' SUBDIRECTORY_OK=Yes . git-sh-setup @@ -64,6 +64,22 @@ run_status () { # We always show status for the whole tree. cd "$TOP" + IS_INITIAL="$initial_commit" + REFERENCE=HEAD + case "$amend" in + t) + # If we are amending the initial commit, there + # is no HEAD^1. + if git-rev-parse --verify "HEAD^1" >/dev/null 2>&1 + then + REFERENCE="HEAD^1" + IS_INITIAL= + else + IS_INITIAL=t + fi + ;; + esac + # If TMP_INDEX is defined, that means we are doing # "--only" partial commit, and that index file is used # to build the tree for the commit. Otherwise, if @@ -85,10 +101,10 @@ run_status () { *) echo "# On branch $branch" ;; esac - if test -z "$initial_commit" + if test -z "$IS_INITIAL" then git-diff-index -M --cached --name-status \ - --diff-filter=MDTCRA HEAD | + --diff-filter=MDTCRA $REFERENCE | sed -e ' s/\\/\\\\/g s/ /\\ /g @@ -147,7 +163,7 @@ run_status () { if test -n "$verbose" then - git-diff-index --cached -M -p --diff-filter=MDTCRA HEAD + git-diff-index --cached -M -p --diff-filter=MDTCRA $REFERENCE fi case "$committable" in 0) @@ -173,6 +189,7 @@ also= only= logfile= use_commit= +amend= no_edit= log_given= log_message= @@ -254,6 +271,12 @@ do verify= shift ;; + --a|--am|--ame|--amen|--amend) + amend=t + log_given=t$log_given + use_commit=HEAD + shift + ;; -c) case "$#" in 1) usage ;; esac shift @@ -328,6 +351,15 @@ done ################################################################ # Sanity check options +case "$amend,$initial_commit" in +t,t) + die "You do not have anything to amend." ;; +t,) + if [ -f "$GIT_DIR/MERGE_HEAD" ]; then + die "You are in the middle of a merge -- cannot amend." + fi ;; +esac + case "$log_given" in tt*) die "Only one of -c/-C/-F/-m can be used." ;; @@ -559,6 +591,9 @@ if test -z "$initial_commit" then if [ -f "$GIT_DIR/MERGE_HEAD" ]; then PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"` + elif test -n "$amend"; then + PARENTS=$(git-cat-file commit HEAD | + sed -n -e '/^$/q' -e 's/^parent /-p /p') fi else if [ -z "$(git-ls-files)" ]; then |