@@ -959,6 +959,25 @@ def _check_for_precommit_hooks(self) -> bool:
959959
960960 return False
961961
962+ def _revert_staged_changes (self ) -> None :
963+ """Revert all staged changes to leave the repository in a clean state."""
964+ try :
965+ logger .info ("Reverting staged changes..." )
966+ # First try the modern 'git restore' command (Git 2.23+)
967+ stdout , code = self ._run_git_command (["restore" , "--staged" , "." ])
968+
969+ # If that fails, fall back to the older 'git reset' command
970+ if code != 0 :
971+ logger .debug ("Modern 'git restore' failed, falling back to 'git reset'" )
972+ _ , reset_code = self ._run_git_command (["reset" , "HEAD" , "." ])
973+ if reset_code != 0 :
974+ logger .warning ("Failed to revert staged changes. Repository may be in an inconsistent state." )
975+ return
976+
977+ logger .info ("Successfully reverted all staged changes." )
978+ except Exception as e :
979+ logger .warning (f"Error while reverting staged changes: { str (e )} " )
980+
962981 def execute_commits (self , interactive : bool = True ) -> None :
963982 """Execute the commits for each group, with optional interactive mode."""
964983 if not self .commit_groups :
@@ -1137,6 +1156,8 @@ def execute_commits(self, interactive: bool = True) -> None:
11371156 self ._run_git_command (["status" , "--short" ])
11381157 except Exception as e :
11391158 logger .error (f"Error during commit execution: { str (e )} " )
1159+ # Revert any staged changes before exiting
1160+ self ._revert_staged_changes ()
11401161 raise RuntimeError (f"Failed to execute commits: { str (e )} " )
11411162
11421163 def _generate_ai_commit_message (self , group : CommitGroup ) -> str :
@@ -1191,12 +1212,14 @@ def main() -> int:
11911212 parser .add_argument ("--timeout" , type = int , help = "Timeout in seconds for HTTP requests" , default = 10 )
11921213 parser .add_argument ("--verbose" , action = "store_true" , help = "Show verbose debug output" )
11931214 parser .add_argument ("--skip-hooks" , action = "store_true" , help = "Skip Git hooks when committing (useful if pre-commit is not installed)" )
1215+ parser .add_argument ("--no-revert" , action = "store_true" , help = "Don't automatically revert staged changes on error" )
11941216 args = parser .parse_args ()
11951217
11961218 # Configure logging level
11971219 if args .verbose :
11981220 logger .setLevel (logging .DEBUG )
11991221
1222+ workflow = None
12001223 try :
12011224 # Verify the repository path exists
12021225 if not os .path .exists (args .repo_path ):
@@ -1251,11 +1274,20 @@ def main() -> int:
12511274
12521275 except KeyboardInterrupt :
12531276 print ("\n \n Operation cancelled by user." )
1277+ if workflow and not args .no_revert :
1278+ workflow ._revert_staged_changes ()
1279+ print ("Staged changes have been reverted." )
12541280 return 130 # Standard exit code for SIGINT
12551281 except Exception as e :
12561282 logger .error (f"Unexpected error during git commit workflow: { str (e )} " , exc_info = True )
12571283 print (f"\n ❌ UNEXPECTED ERROR: { str (e )} " )
12581284 print ("\n Please report this issue with the error details from the log." )
1285+
1286+ # Revert staged changes if workflow was created
1287+ if workflow and not args .no_revert :
1288+ workflow ._revert_staged_changes ()
1289+ print ("Staged changes have been reverted." )
1290+
12591291 return 1
12601292
12611293
0 commit comments