File tree Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ import subprocess
2
+ import sys
3
+
4
+ def run_command (command ):
5
+ """Run a shell command and handle errors."""
6
+ try :
7
+ result = subprocess .run (command , check = True , text = True , shell = True )
8
+ return result .returncode
9
+ except subprocess .CalledProcessError as e :
10
+ print (f"Error: Command '{ command } ' failed with return code { e .returncode } " )
11
+ sys .exit (e .returncode )
12
+
13
+ def main ():
14
+ if len (sys .argv ) < 3 :
15
+ print ("Usage: python git_push.py <add-option> \" Your commit message here\" " )
16
+ print ("Example: python git_push.py . \" Initial commit\" " )
17
+ sys .exit (1 )
18
+
19
+ add_option = sys .argv [1 ] # e.g., ".", specific file, or folder
20
+ commit_message = sys .argv [2 ] # Commit message
21
+
22
+ print ("Staging changes..." )
23
+ run_command (f"git add { add_option } " )
24
+
25
+ print ("Committing changes..." )
26
+ run_command (f'git commit -m "{ commit_message } "' )
27
+
28
+ print ("Pushing to the 'main' branch..." )
29
+ run_command ("git push -u origin main" )
30
+
31
+ print ("Changes pushed to origin/main successfully!" )
32
+
33
+ if __name__ == "__main__" :
34
+ main ()
Original file line number Diff line number Diff line change
1
+ #! /bin/bash
2
+
3
+ # Check if a commit message is provided
4
+ if [ -z " $2 " ]; then
5
+ echo " Error: Commit message is required."
6
+ echo " Usage: ./git_push.sh <add-option> \" Your commit message here\" "
7
+ echo " Example: ./git_push.sh . \" Initial commit\" "
8
+ exit 1
9
+ fi
10
+
11
+ # Capture add option (file name, path, or .)
12
+ ADD_OPTION=$1
13
+
14
+ # Capture commit message
15
+ COMMIT_MESSAGE=$2
16
+
17
+ # Stage specified changes
18
+ git add $ADD_OPTION
19
+
20
+ # Commit changes with the provided message
21
+ git commit -m " $COMMIT_MESSAGE "
22
+
23
+ # Push changes to the 'main' branch
24
+ git push -u origin main
25
+
26
+ # Success message
27
+ echo " Changes pushed to origin/main successfully."
You can’t perform that action at this time.
0 commit comments