|
1 | 1 | # Automate commit using Git |
2 | 2 |
|
3 | | - |
4 | 3 |
|
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. |
6 | 5 | This code is the simplest one to automate the task. |
7 | 6 |
|
8 | 7 | ## Understanding the code |
9 | 8 |
|
10 | | - |
| 9 | + |
11 | 10 |
|
12 | 11 | ``` |
13 | 12 | import subprocess |
| 13 | +import sys |
14 | 14 | ``` |
15 | 15 |
|
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. |
17 | 17 |
|
18 | 18 | ``` |
19 | | -subprocess.getoutput('git add .') |
| 19 | +result = subprocess.run([GIT_PATH, "add", ":/"], check=True) |
| 20 | +
|
20 | 21 | ``` |
21 | 22 |
|
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. |
23 | 24 |
|
24 | 25 | ``` |
25 | | -message = input("Enter commit message") |
| 26 | +message = input("Enter commit message (or press Enter to use default 'Auto commit'): ") |
| 27 | +
|
26 | 28 | ``` |
27 | 29 |
|
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". |
29 | 31 |
|
30 | 32 | ``` |
31 | | -subprocess.getoutput('git commit -m ' + message) |
| 33 | +remote = input("Enter remote name (or press Enter to use default 'origin'): ") |
| 34 | +
|
32 | 35 | ``` |
33 | 36 |
|
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". |
35 | 38 |
|
36 | 39 | ``` |
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 | + |
38 | 44 | ``` |
| 45 | +result = subprocess.run([GIT_PATH, "commit", "-m", message],check=True) |
39 | 46 |
|
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). |
41 | 49 |
|
42 | 50 | ``` |
43 | | -subprocess.getoutput('git push origin ' + branchname) |
| 51 | +result = subprocess.run([GIT_PATH, "push", remote, branchname],check=True) |
| 52 | +
|
44 | 53 | ``` |
| 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. |
45 | 58 |
|
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. |
0 commit comments