Skip to content

Commit 045f82c

Browse files
author
Junio C Hamano
committed
git-revert: revert an existing commit.
Given one existing commit, revert the change the patch introduces, and record a new commit that records it. This requires your working tree to be clean (no modifications from the HEAD commit). This is based on what Linus posted to the list, with enhancements he suggested, including the use of -M to attempt reverting renames. Signed-off-by: Junio C Hamano <[email protected]>
1 parent d87449c commit 045f82c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ SCRIPTS=git git-apply-patch-script git-merge-one-file-script git-prune-script \
7070

7171
SCRIPTS += git-count-objects-script
7272
# SCRIPTS += git-send-email-script
73+
SCRIPTS += git-revert-script
7374

7475
PROG= git-update-cache git-diff-files git-init-db git-write-tree \
7576
git-read-tree git-commit-tree git-cat-file git-fsck-cache \

git-revert-script

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/sh
2+
. git-sh-setup-script || die "Not a git archive"
3+
4+
# We want a clean tree and clean index to be able to revert.
5+
status=$(git status)
6+
case "$status" in
7+
'nothing to commit') ;;
8+
*)
9+
echo "$status"
10+
die "Your working tree is dirty; cannot revert a previous patch." ;;
11+
esac
12+
13+
rev=$(git-rev-parse --no-flags --verify --revs-only "$@") &&
14+
commit=$(git-rev-parse --verify "$rev^0") || exit
15+
if git-diff-tree -R -M -p $commit | git-apply --index &&
16+
msg=$(git-rev-list --pretty=oneline --max-count=1 $commit)
17+
then
18+
{
19+
echo "$msg" | sed -e '
20+
s/^[^ ]* /Revert "/
21+
s/$/"/'
22+
echo
23+
echo "This reverts $commit commit."
24+
test "$rev" = "$commit" ||
25+
echo "(original 'git revert' arguments: $@)"
26+
} | git commit -F -
27+
else
28+
# Now why did it fail?
29+
parents=`git-cat-file commit "$commit" 2>/dev/null |
30+
sed -ne '/^$/q;/^parent /p' |
31+
wc -l`
32+
case $parents in
33+
0) die "Cannot revert the root commit nor non commit-ish." ;;
34+
1) die "The patch does not apply." ;;
35+
*) die "Cannot revert a merge commit." ;;
36+
esac
37+
fi

0 commit comments

Comments
 (0)