Skip to content

Commit a619d02

Browse files
committed
feat: add script to automate git amend and push
1 parent 9235e7b commit a619d02

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

git_push.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
import subprocess
3+
import sys
4+
5+
6+
def run_command(command: list[str], check: bool = True) -> subprocess.CompletedProcess[str]:
7+
"""Run a shell command and capture the output."""
8+
print(f"Running command: {' '.join(command)}")
9+
result = subprocess.run(command, check=check, text=True, capture_output=True)
10+
if result.stdout:
11+
print(result.stdout, end="")
12+
if result.stderr:
13+
print(result.stderr, end="", file=sys.stderr)
14+
return result
15+
16+
17+
def stage_changes() -> None:
18+
"""Stage all modified files."""
19+
run_command(["git", "add", "-A"])
20+
21+
22+
def amend_commit() -> None:
23+
"""Amend the last commit to include the new changes."""
24+
run_command(["git", "commit", "--amend", "--no-edit"])
25+
26+
27+
def push_changes() -> None:
28+
"""Push the amended commit to the remote repository."""
29+
run_command(["git", "push", "--force-with-lease"])
30+
31+
32+
def main() -> None:
33+
"""Main function to automate the process of staging, amending, and pushing changes."""
34+
# Stage the changes
35+
stage_changes()
36+
37+
# Amend the last commit
38+
amend_commit()
39+
40+
# Push the changes to the remote repository
41+
push_changes()
42+
43+
44+
if __name__ == "__main__":
45+
main()

0 commit comments

Comments
 (0)