logo
Automate git add, commit and push with python (One command to rule them all)
Mwaura

Mwaura

Jan 21, 2023

Automate git add, commit and push with python (One command to rule them all)

This article is meant for Linux users. (Sorry to you Windows and Mac folks out there)

What to expect

Below is a glimpse of what to expect after finishing this tutorial.

How to do it

Step 1: The code

How the code works

  • We start by specifying the path to the interpreter used to run the script. In our case, python3
  • Following that are comments about what the script does.
  • We then import os .
    import os

The OS module in python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules.

  • The commit message is then obtained by joining all the command line arguments into one string.

Space, " " is used as the separator. (It will separate all the strings in the argument vector.)

If we used a '###', here is how our message would look like.

$push initial commit

pushed with the message: initial###commit

We iterate over all the arguments from 1, (not 0 because the first argument is always the name of the program) and join them to form the message string.

  • then pass the git commands to os.system()

os.system() method execute the command (a string) in a sub-shell.

  • We use git add . to adds all change in the working directory to the staging area. 

os.system('git commit -m "{}"'.format(commit_message))

  • git commit -m "commit message" is used to Commit a snapshot of all changes in the working directory while passing our message (notice the sting.format() method).
  • The git push command is used to upload local repository content to a remote repository. 

os.system("git push -u origin master")

Here, it's set up to track the master branch. You can change it to main depending on how you have configured your GitHub.

It would looks like this:

os.system("git push -u origin main")

  • After we are done, we print a message to the output to show us that everything has been done.

print("\n\nPushed with message: {}".format(commit_message))

Feel free to get creative with the output.

Step 2: Let's make it executable

Give yourself permission to execute the script by running the following command in your terminal.

$ chmod u+x push.py

chmod is used to control who can access files, search directories, and run scripts

We give the user execute permissions on the file push.py (I read u+x as add execute permission to user).

Step 3: Now we make the script executable from anywhere.

  • Copy the script to a directory where your programs are stored.
  • This is usually the directory

/usr/local/bin/push

  • You can do so by using the cp command, i.e.,

cp push.py /usr/local/bin/push

  • To eliminate the need for typing push.py all the time, you can remove the extension .py from the file push.py, i.e.,

$mv /usr/local/bin/push.py /usr/local/bin/push

This renames the file push.py to push

Incase of any errors while running the command, try prefixing the command with sudo so as to execute it as admin. ie. sudo mv file1 file2

Conclusion.

That's it. Happy pushing. Feel free to post any concerns on the comment section.

I'll be exploring how we can make the file accessible from anywhere using aliases in the next article.

Leave a Reply

Related Posts

Categories