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

[PATCH] git-revert is one of the most misunderstood command in git, help users out.

From
Pierre Habouzit <madcoder@debian.org>
Date
Nov 5, 2007, 19:01 UTC
Message-ID
<1194289301-7800-1-git-send-email-madcoder@debian.org>

When git-revert has a file argument then redirect the user to what he probably meant.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 builtin-revert.c |   24 +++++++++++++++++-------
 gitk             |    2 +-
 2 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/builtin-revert.c b/builtin-revert.c
index 62ab1fa..9660048 100644
--- a/builtin-revert.c
+++ b/builtin-revert.c
@@ -38,7 +38,7 @@ static const char *me;
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
-static void parse_args(int argc, const char **argv)
+static void parse_args(int argc, const char **argv, const char *prefix)
 {
 	const char * const * usage_str =
 		action == REVERT ?  revert_usage : cherry_pick_usage;
@@ -58,8 +58,18 @@ static void parse_args(int argc, const char **argv)
 		usage_with_options(usage_str, options);
 	arg = argv[0];
 
-	if (get_sha1(arg, sha1))
-		die ("Cannot find '%s'", arg);
+	if (get_sha1(arg, sha1)) {
+		struct stat st;
+		const char *name;
+
+		name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
+		if (!lstat(name, &st)) {
+			die("Cannot find commit '%s', did you meant: "
+				"git checkout HEAD -- '%s'", arg, arg);
+		} else {
+			die("Cannot find commit '%s'", arg);
+		}
+	}
 	commit = (struct commit *)parse_object(sha1);
 	if (!commit)
 		die ("Could not find %s", sha1_to_hex(sha1));
@@ -225,7 +235,7 @@ static int merge_recursive(const char *base_sha1,
 	return run_command_v_opt(argv, RUN_COMMAND_NO_STDIN | RUN_GIT_CMD);
 }
 
-static int revert_or_cherry_pick(int argc, const char **argv)
+static int revert_or_cherry_pick(int argc, const char **argv, const char *prefix)
 {
 	unsigned char head[20];
 	struct commit *base, *next, *parent;
@@ -237,7 +247,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
 	git_config(git_default_config);
 	me = action == REVERT ? "revert" : "cherry-pick";
 	setenv(GIT_REFLOG_ACTION, me, 0);
-	parse_args(argc, argv);
+	parse_args(argc, argv, prefix);
 
 	/* this is copied from the shell script, but it's never triggered... */
 	if (action == REVERT && !no_replay)
@@ -405,12 +415,12 @@ int cmd_revert(int argc, const char **argv, const char *prefix)
 		edit = 1;
 	no_replay = 1;
 	action = REVERT;
-	return revert_or_cherry_pick(argc, argv);
+	return revert_or_cherry_pick(argc, argv, prefix);
 }
 
 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
 {
 	no_replay = 0;
 	action = CHERRY_PICK;
-	return revert_or_cherry_pick(argc, argv);
+	return revert_or_cherry_pick(argc, argv, prefix);
 }
diff --git a/gitk b/gitk
index 1da0b0a..ab8bab2 100755
--- a/gitk
+++ b/gitk
@@ -1,6 +1,6 @@
 #!/bin/sh
 # Tcl ignores the next line -*- tcl -*- \
-exec wish "$0" -- "$@"
+exec wish8.5 "$0" -- "$@"
 
 # Copyright (C) 2005-2006 Paul Mackerras.  All rights reserved.
 # This program is free software; it may be used, copied, modified
-- 
1.5.3.5.1541.gd2b5c-dirty
Next: Pierre Habouzit
Message 1 of 39 in “git-revert is one of the most misunderstood command in git, help users out.”
  1. git-revert is one of the most misunderstood command in git, help users out.Pierre Habouzit, Nov 5, 2007
  2. Pierre HabouzitNov 5, 2007
  3. J. Bruce FieldsNov 5, 2007
  4. Pierre HabouzitNov 5, 2007
  5. Steven GrimmNov 5, 2007
  6. Pierre HabouzitNov 5, 2007
  7. Alejandro Martinez RuizNov 5, 2007
  8. David KastrupNov 5, 2007
  9. Alejandro Martinez RuizNov 5, 2007
  10. Junio C HamanoNov 5, 2007
  11. Johannes SchindelinNov 5, 2007
  12. Pierre HabouzitNov 6, 2007
  13. Junio C HamanoNov 6, 2007
  14. Johannes SchindelinNov 6, 2007
  15. Junio C HamanoNov 6, 2007
  16. Pierre HabouzitNov 6, 2007
  17. Mike HommeyNov 6, 2007
  18. Pierre HabouzitNov 6, 2007
  19. Johannes SchindelinNov 6, 2007
  20. Junio C HamanoNov 6, 2007
  21. Johannes SchindelinNov 6, 2007
  22. Pierre HabouzitNov 6, 2007
  23. Junio C HamanoNov 6, 2007
  24. Johannes SchindelinNov 6, 2007
  25. Robin RosenbergNov 6, 2007
  26. Mike HommeyNov 6, 2007
  27. Robin RosenbergNov 6, 2007
  28. Johannes SchindelinNov 6, 2007
  29. Mike HommeyNov 7, 2007
  30. Johannes SchindelinNov 7, 2007
  31. Robin RosenbergNov 7, 2007
  32. Jakub NarebskiNov 7, 2007
  33. David KastrupNov 7, 2007
  34. Junio C HamanoNov 6, 2007
  35. Johannes SixtNov 6, 2007
  36. Johannes SchindelinNov 6, 2007
  37. Johannes SchindelinNov 6, 2007
  38. Pierre HabouzitNov 6, 2007
  39. Wincent ColaiutaNov 6, 2007

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.