-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
0b22e49
commit 31fd3fb
Showing
4 changed files
with
68 additions
and
20 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,60 @@ | ||
# Automate commit using Git | ||
|
||
![image](https://steadylearner.com/static/images/post/Python/python-github-by-Steadylearner.png) | ||
|
||
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. | ||
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. | ||
This code is the simplest one to automate the task. | ||
|
||
## Understanding the code | ||
|
||
![image](https://snipboard.io/iqvAFy.jpg) | ||
![image](https://snipboard.io/JaSpdt.jpg) | ||
|
||
``` | ||
import subprocess | ||
import sys | ||
``` | ||
|
||
The subprocess module allows us to spawn processes, connect to their input/output/error pipes, and obtain their return codes. | ||
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. | ||
|
||
``` | ||
subprocess.getoutput('git add .') | ||
result = subprocess.run([GIT_PATH, "add", ":/"], check=True) | ||
``` | ||
|
||
_subprocess.getoutput:_ Return output (stdout and stderr) of executing cmd in a shell. That means, it will execute the command `git add .` | ||
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. | ||
|
||
``` | ||
message = input("Enter commit message") | ||
message = input("Enter commit message (or press Enter to use default 'Auto commit'): ") | ||
``` | ||
|
||
Now, you can simply understand that we are taking an input message to give it to the commit message in the next command. | ||
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". | ||
|
||
``` | ||
subprocess.getoutput('git commit -m ' + message) | ||
remote = input("Enter remote name (or press Enter to use default 'origin'): ") | ||
``` | ||
|
||
In this line of code, we can see that the commit message will be appended to the command `git commit -m <message>`. | ||
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". | ||
|
||
``` | ||
branchname = input("Enter branch name") | ||
branchname = input("Enter branch name (or press Enter to use default 'HEAD'): ") | ||
``` | ||
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. | ||
|
||
``` | ||
result = subprocess.run([GIT_PATH, "commit", "-m", message],check=True) | ||
Then, give the branch name to which you want to push your code. | ||
``` | ||
This line of code commits the staged changes using the commit message provided by the user (or the default message, if none was provided). | ||
|
||
``` | ||
subprocess.getoutput('git push origin ' + branchname) | ||
result = subprocess.run([GIT_PATH, "push", remote, branchname],check=True) | ||
``` | ||
Finally, this line of code pushes the committed changes to the specified branch and remote. | ||
|
||
## Usage | ||
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. | ||
|
||
Finally, to push our code we are using, `git push origin <branch-name>`. | ||
You can also add a variable to the remote url for defining the origin, but by default it is origin. | ||
## Note | ||
> 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.
45 changes: 40 additions & 5 deletions
45
Python/Automate_Commiting_using_Git/automate_commiting_using_git.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,42 @@ | ||
import subprocess | ||
import sys | ||
|
||
# Define the full path to Git | ||
GIT_PATH = "/usr/bin/git" | ||
|
||
# Stage all changes from the root of the git project | ||
result = subprocess.run([GIT_PATH, "add", ":/"], check=True) | ||
if result.returncode != 0: | ||
print("Error: Failed to stage changes.") | ||
sys.exit(1) | ||
|
||
# Get commit message from user input or use default value | ||
message = input("Enter commit message (or press Enter to use default): ") | ||
if not message: | ||
message = "Auto commit" | ||
|
||
# Get remote name from user input or use default value | ||
remote = input("Enter remote name (or press Enter to use default 'origin'): ") | ||
if not remote: | ||
remote = "origin" | ||
|
||
# Get current branch name | ||
# Get current branch name | ||
branchname = input("Enter branch name (or press Enter to use default 'HEAD'): ") | ||
if not branchname: | ||
branchname = "HEAD" | ||
|
||
# Commit changes | ||
result = subprocess.run([GIT_PATH, "commit", "-m", message],check=True) | ||
if result.returncode != 0: | ||
print("Error: Failed to commit changes.") | ||
sys.exit(1) | ||
|
||
# Push changes to remote | ||
result = subprocess.run([GIT_PATH, "push", remote, branchname],check=True) | ||
if result.returncode != 0: | ||
print("Error: Failed to push changes.") | ||
sys.exit(1) | ||
|
||
print("Changes successfully committed and pushed to remote.") | ||
|
||
subprocess.getoutput("git add .") | ||
message = input("Enter commit message") | ||
subprocess.getoutput("git commit -m " + message) | ||
branchname = input("Enter branch name") | ||
subprocess.getoutput("git push origin " + branchname) |