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>
Junio C Hamano committed
Mar 2, 2006 at 21:04 UTC
b4019f045646b1770a80394da876b8a7c6b8ca7b
1 file changed
+39
-4
git-commit.sh
+39
-4
@@ -3,7 +3,7 @@
3
# Copyright (c) 2005 Linus Torvalds
4
# Copyright (c) 2006 Junio C Hamano
5
6
-USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-e] [--author <author>] [[-i | -o] <path>...]'
6
+USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>) [--amend] [-e] [--author <author>] [[-i | -o] <path>...]'
7
SUBDIRECTORY_OK=Yes
8
. git-sh-setup
9
@@ -64,6 +64,22 @@ run_status () {
64
# We always show status for the whole tree.
65
cd "$TOP"
66
67
+ IS_INITIAL="$initial_commit"
68
+ REFERENCE=HEAD
69
+ case "$amend" in
70
+ t)
71
+ # If we are amending the initial commit, there
72
+ # is no HEAD^1.
73
+ if git-rev-parse --verify "HEAD^1" >/dev/null 2>&1
74
+ then
75
+ REFERENCE="HEAD^1"
76
+ IS_INITIAL=
77
+ else
78
+ IS_INITIAL=t
79
+ fi
80
+ ;;
81
+ esac
82
+
83
# If TMP_INDEX is defined, that means we are doing
84
# "--only" partial commit, and that index file is used
85
# to build the tree for the commit. Otherwise, if
@@ -85,10 +101,10 @@ run_status () {
101
*) echo "# On branch $branch" ;;
102
esac
103
88
- if test -z "$initial_commit"
104
+ if test -z "$IS_INITIAL"
105
then
106
git-diff-index -M --cached --name-status \
91
- --diff-filter=MDTCRA HEAD |
107
+ --diff-filter=MDTCRA $REFERENCE |
108
sed -e '
109
s/\\/\\\\/g
110
s/ /\\ /g
@@ -147,7 +163,7 @@ run_status () {
163
164
if test -n "$verbose"
165
then
150
- git-diff-index --cached -M -p --diff-filter=MDTCRA HEAD
166
+ git-diff-index --cached -M -p --diff-filter=MDTCRA $REFERENCE
167
fi
168
case "$committable" in
169
0)
@@ -173,6 +189,7 @@ also=
189
only=
190
logfile=
191
use_commit=
192
+amend=
193
no_edit=
194
log_given=
195
log_message=
@@ -254,6 +271,12 @@ do
271
verify=
272
shift
273
;;
274
+ --a|--am|--ame|--amen|--amend)
275
+ amend=t
276
+ log_given=t$log_given
277
+ use_commit=HEAD
278
+ shift
279
+ ;;
280
-c)
281
case "$#" in 1) usage ;; esac
282
shift
@@ -328,6 +351,15 @@ done
351
################################################################
352
# Sanity check options
353
354
+case "$amend,$initial_commit" in
355
+t,t)
356
+ die "You do not have anything to amend." ;;
357
+t,)
358
+ if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
359
+ die "You are in the middle of a merge -- cannot amend."
360
+ fi ;;
361
+esac
362
+
363
case "$log_given" in
364
tt*)
365
die "Only one of -c/-C/-F/-m can be used." ;;
@@ -559,6 +591,9 @@ if test -z "$initial_commit"
591
then
592
if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
593
PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
594
+ elif test -n "$amend"; then
595
+ PARENTS=$(git-cat-file commit HEAD |
596
+ sed -n -e '/^$/q' -e 's/^parent /-p /p')
597
fi
598
else
599
if [ -z "$(git-ls-files)" ]; then