diff --git a/cli/src/core/git.py b/cli/src/core/git.py index 61fc247..96646a9 100644 --- a/cli/src/core/git.py +++ b/cli/src/core/git.py @@ -48,9 +48,21 @@ def git_push_changes( origin = repo.remote(name="origin") origin.push(refspec=f"{branch_name}:{branch_name}").raise_if_error() except Exception as e: - logger.info("Git resetting files") - repo.index.reset(files_to_stage) - raise e + logger.error(f"Push failed due to an error: {e}") + logger.info("Rolling back local Git index changes...") + + # Step A: Undo the local commit if it was created (moves HEAD back 1 commit, keeps changes) + # This prevents leaving a dead/unpushed commit on your local branch + try: + repo.head.reset("HEAD~1", index=True, working_tree=False) + except Exception: + # Fallback if the exception happened BEFORE the commit step even ran + pass + + # Step B: Unstage the specific files (Equivalent to: git reset HEAD ) + # This leaves the files intact but removes them from the staging area + repo.index.reset(paths=files_to_stage) + logger.info("Staging area successfully reset to clean state.") logger.info("Successfully pushed!")