DEV Community

loading...

Automating some git commands with Python

endormi profile image endormi Updated on ・2 min read

I recently created a script that would automate the process of using git commands such as clone, commit, branch, pull, merge, blame and stash.

If you have any suggestions on what I should add and/or any improvements for the code, be sure to let me know. It's always very much appreciated.

Let's get into it!

Disclaimer: I didn't add all of the code, just the key functions.

For the whole code, source code.

Importing subprocess module:

import subprocess
Enter fullscreen mode Exit fullscreen mode

Simple run function which we will be using for pretty much all of the code:

def run(*args):

    return subprocess.check_call(['git'] + list(args))
Enter fullscreen mode Exit fullscreen mode

Clone:

def clone():

    print("\nYou will be asked for the user first and then the repository name\n")

    user = input("User: ")
    __user__ = f'{user}'
    repo = input("Repository: ")
    __repo__ = f'{repo}'

    print("\nChoose the local path for your clone.")
    local = input("Local path: ")
    local_path = f'{local}'

    subprocess.Popen(['git', 'clone', "https://github.com/" + __user__ + "/" + __repo__ + ".git", local_path])
Enter fullscreen mode Exit fullscreen mode

Commit:

def commit():

    message = input("\nType in your commit message: ")
    commit_message = f'{message}'

    run("commit", "-am", commit_message)
    run("push", "-u", "origin", "master")
Enter fullscreen mode Exit fullscreen mode

Creating a branch and pushing it to GitHub:

def branch():

    branch = input("\nType in the name of the branch you want to make: ")
    br = f'{branch}'

    run("checkout", "-b", br)

    choice = input("\nDo you want to push the branch right now to GitHub? (y/n): ")
    choice = choice.lower()

    if choice == "y":
        run("push", "-u", "origin", br)

    elif choice == "n":
        print("\nOkay, goodbye!\n")

    else:
        print("\nInvalid command! Use y or n.\n")
Enter fullscreen mode Exit fullscreen mode

All of the commands that are currently possible:

def main():

    choices = 'clone, commit, branch, pull, merge, blame and stash'
    print("Commands to use: " + choices)

    choose_command = input("Type in the command you want to use: ")
    choose_command = choose_command.lower()

    if choose_command == "clone":
        clone()

    elif choose_command == "commit":
        commit()

    elif choose_command == "branch":
        branch()

    elif choose_command == "pull":
        pull()

    elif choose_command == "merge":
        merge()

    elif choose_command == "blame":
        blame()

    elif choose_command == "stash":
        stash()

    else:
        print("\nNot a valid command!")
        print("\nUse " + choices)


main()
Enter fullscreen mode Exit fullscreen mode

Thanks for reading.

Discussion

pic
Editor guide
Collapse
patarapolw profile image
Pacharapol Withayasakpunt

It reminds me of the time when I wrote this -- github.com/patarapolw/xgit

Collapse
endormi profile image