Skip to content

Commit 31fd3fb

Browse files
geekabhinav007ClasherKasten
authored andcommitted
Enhanced Git automation script with user input options
Improved git commit and push automation with customizable commit message, remote name, and branch name with default value for each option.
1 parent 0b22e49 commit 31fd3fb

File tree

4 files changed

+68
-20
lines changed

4 files changed

+68
-20
lines changed
119 KB
Loading
Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,60 @@
11
# Automate commit using Git
22

3-
![image](https://steadylearner.com/static/images/post/Python/python-github-by-Steadylearner.png)
43

5-
Are you tired of adding, committing and pushing you code everytime you change it? If so, you can use this Python script to automate this boring stuff.
4+
Are you tired of adding, committing and pushing your code everytime you change it? If so, you can use this Python script to automate this boring stuff.
65
This code is the simplest one to automate the task.
76

87
## Understanding the code
98

10-
![image](https://snipboard.io/iqvAFy.jpg)
9+
![image](https://snipboard.io/JaSpdt.jpg)
1110

1211
```
1312
import subprocess
13+
import sys
1414
```
1515

16-
The subprocess module allows us to spawn processes, connect to their input/output/error pipes, and obtain their return codes.
16+
The subprocess module allows us to spawn processes, connect to their input/output/error pipes, and obtain their return codes. sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
1717

1818
```
19-
subprocess.getoutput('git add .')
19+
result = subprocess.run([GIT_PATH, "add", ":/"], check=True)
20+
2021
```
2122

22-
_subprocess.getoutput:_ Return output (stdout and stderr) of executing cmd in a shell. That means, it will execute the command `git add .`
23+
This line of code stages all changes made to files in the root directory of the repository, no matter what subdirectory the user is in, using the git add command.
2324

2425
```
25-
message = input("Enter commit message")
26+
message = input("Enter commit message (or press Enter to use default 'Auto commit'): ")
27+
2628
```
2729

28-
Now, you can simply understand that we are taking an input message to give it to the commit message in the next command.
30+
This line of code prompts the user to enter a commit message. If the user enters nothing and presses Enter, the commit message will default to "Auto commit".
2931

3032
```
31-
subprocess.getoutput('git commit -m ' + message)
33+
remote = input("Enter remote name (or press Enter to use default 'origin'): ")
34+
3235
```
3336

34-
In this line of code, we can see that the commit message will be appended to the command `git commit -m <message>`.
37+
This line of code prompts the user to enter a remote name for the repository. If the user enters nothing and presses Enter, the remote name will default to "origin".
3538

3639
```
37-
branchname = input("Enter branch name")
40+
branchname = input("Enter branch name (or press Enter to use default 'HEAD'): ")
41+
```
42+
This line of code prompts the user to enter a branch name to which the changes should be pushed. If the user enters nothing and presses Enter, the changes will be pushed to the current branch (HEAD) by default.
43+
3844
```
45+
result = subprocess.run([GIT_PATH, "commit", "-m", message],check=True)
3946
40-
Then, give the branch name to which you want to push your code.
47+
```
48+
This line of code commits the staged changes using the commit message provided by the user (or the default message, if none was provided).
4149

4250
```
43-
subprocess.getoutput('git push origin ' + branchname)
51+
result = subprocess.run([GIT_PATH, "push", remote, branchname],check=True)
52+
4453
```
54+
Finally, this line of code pushes the committed changes to the specified branch and remote.
55+
56+
## Usage
57+
To use this script, simply run it in the directory where your git repository is located. Follow the prompts to enter a commit message, remote name, and branch name, or press Enter to accept the defaults.
4558

46-
Finally, to push our code we are using, `git push origin <branch-name>`.
47-
You can also add a variable to the remote url for defining the origin, but by default it is origin.
59+
## Note
60+
> This script assumes that you have already initialized a git repository in the directory where it is being run. If you have not done so, you will need to initialize a git repository using git init before using this script.
Binary file not shown.

Python/Automate_Commiting_using_Git/automate_commiting_using_git.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
11
import subprocess
2+
import sys
3+
4+
# Define the full path to Git
5+
GIT_PATH = "/usr/bin/git"
6+
7+
# Stage all changes from the root of the git project
8+
result = subprocess.run([GIT_PATH, "add", ":/"], check=True)
9+
if result.returncode != 0:
10+
print("Error: Failed to stage changes.")
11+
sys.exit(1)
12+
13+
# Get commit message from user input or use default value
14+
message = input("Enter commit message (or press Enter to use default): ")
15+
if not message:
16+
message = "Auto commit"
17+
18+
# Get remote name from user input or use default value
19+
remote = input("Enter remote name (or press Enter to use default 'origin'): ")
20+
if not remote:
21+
remote = "origin"
22+
23+
# Get current branch name
24+
# Get current branch name
25+
branchname = input("Enter branch name (or press Enter to use default 'HEAD'): ")
26+
if not branchname:
27+
branchname = "HEAD"
28+
29+
# Commit changes
30+
result = subprocess.run([GIT_PATH, "commit", "-m", message],check=True)
31+
if result.returncode != 0:
32+
print("Error: Failed to commit changes.")
33+
sys.exit(1)
34+
35+
# Push changes to remote
36+
result = subprocess.run([GIT_PATH, "push", remote, branchname],check=True)
37+
if result.returncode != 0:
38+
print("Error: Failed to push changes.")
39+
sys.exit(1)
40+
41+
print("Changes successfully committed and pushed to remote.")
242

3-
subprocess.getoutput("git add .")
4-
message = input("Enter commit message")
5-
subprocess.getoutput("git commit -m " + message)
6-
branchname = input("Enter branch name")
7-
subprocess.getoutput("git push origin " + branchname)

0 commit comments

Comments
 (0)