git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC,PATCH] git-amend -- darcs like amend-record for git

From
Kirill Smelkov <kirr@mns.spb.ru>
Date
Feb 25, 2010, 14:16 UTC
Message-ID
<1267107365-2973-1-git-send-email-kirr@mns.spb.ru>

Ever wanted to type `git commit --amend HEAD~3` ? But had to fall back to commit and interactive rebase with fixup? And was tired by a lot of typing? And also rebase works only on clean worktree, so stash save and stash pop have to be used...

Forget it.

Now one can just do `git add ...` to prepare index, and then `git amend <commit>` to merge that changes from index into <commit>.

Yes, it uses rebase behind the scenes (or better sequencer, when it is available), but the end result is less typing and much better usability for humans. And it even tries to work in dirty worktree...

So operations like amending HEAD~3 take not significanltly more time, than just amending HEAD with already available `git commit --amend`.

It's only a prototype, without documentation and tests, just an RFC.

If you find the idea useful, please let me know. In case people want something like this, I'll try to polish it for inclusion into git.

Thanks,
Kirill
---
 contrib/git-amend.sh |   53 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 53 insertions(+), 0 deletions(-)
 create mode 100755 contrib/git-amend.sh
diff --git a/contrib/git-amend.sh b/contrib/git-amend.sh
new file mode 100755
index 0000000..1778594
--- /dev/null
+++ b/contrib/git-amend.sh
@@ -0,0 +1,53 @@
+#!/bin/sh -e
+# git-amend: darcs like amend-record for git
+# FIXME error handling
+
+USAGE="<commit>"
+. git-sh-setup
+
+test "$#" = 1 || usage
+
+commit=$1
+
+# let's see if $commit is a valid one, and also transform it to sha1 for it not
+# to be dependant on current HEAD
+commit=$(git rev-parse "$commit")
+
+# look out merges!
+test -z "$(git log --merges ${commit}~..HEAD)" || \
+	die "E: can't amend commits behind merges"
+
+# commit index, allowing empty commit just for being able to amend patch
+# description
+git commit --allow-empty -m "amend for $commit"
+
+# save worktree/index state
+work=$(git stash create)
+test -n "$work" && git update-ref HEAD.amend-backup $work
+
+# reset to clean work/index before going into rebase
+git reset --hard HEAD
+
+fixup_cmd=$(mktemp -t git-amend-fixupcmd.XXXXXX)
+rest_cmd=$(mktemp -t git-amend-restcmd.XXXXXX)
+trap 'rm -f "$fixup_cmd" "$rest_cmd"' 0
+{
+echo -ne "pick\t";  git log --pretty=oneline -1 ${commit}
+echo -ne "fixup\t"; git log --pretty=oneline -1 ${HEAD}
+} > "$fixup_cmd"
+
+git log --pretty=oneline --reverse ${commit}..HEAD~ | awk '{print "pick\t" $0;}' \
+	> "$rest_cmd"
+
+# now let's apply fixup part to commit
+GIT_EDITOR="cat $fixup_cmd >" git rebase -i ${commit}~
+
+# reword it
+git commit -v --amend
+
+# and apply rest
+GIT_EDITOR="cat $rest_cmd >" git rebase -i HEAD
+
+
+# restore worktree/index state
+test -n "$work" && git stash apply --index $work
-- 
1.7.0.91.g9803
Next: Tay Ray Chuan
Message 1 of 4 in “git-amend -- darcs like amend-record for git”
  1. git-amend -- darcs like amend-record for gitKirill Smelkov, Feb 25, 2010
  2. Tay Ray ChuanMar 1, 2010
  3. Larry D'AnnaMar 1, 2010
  4. Kirill SmelkovMar 3, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.