As we all know that Python is a great platform to automate infrastructural operations like Deploying across multiple platforms, Scheduled handler operations, Notification and alert management and more. Automating GIT Operations is highly needed in Infrastructure automation. Especially, when we have a job to publish or modify the code back to GIT Repositories. Let’s discuss some useful GIT Operations with Python Scripting.
In this tutorial, we will take Python3 for the example. Comment if you need the tutorial in multiple versions of tools and platforms. So, the knowledge required for this tutorial is:
- Git Operations.
- Python Scripting
Let’s create a python file to workaround. Here, I have created git-python.py to run Git operations. Also, GitPython is one of the great libraries to interact with Git Repositories. So, we are going to use the GitPython library for the tutorial purpose. We can import the GitPython library in our python script as mentioned below
from git import Repo
By this, we are initiating GitPython Library and from there we are importing Repo Module
Table of Contents
Basic Git Operations.
We will see how the following basic Git Operations are handled in the python script.
- Git Init
- Git Clone
- Git Checkout
- Git Fetch
- Git Add
- Git Commit
- Git push
- Git Pull
- Git Merge
- Git Reset
Let’s see how these Git Basic operations in Python GitPython one by one.
To initiate is to create the bare Git repo in the specified directory.
from git import Repo # To initiate new Git repo in the mentioned directory repository = Repo.init(repo_path)
Clone an existing repository into a new directory,
import git git.Git(repo_path).clone("https://github.com/digitalvarys/test.git")
To replace the current working files with files from a branch:
import git repo.git.checkout('branchename')
Also, if you are checking out with a new branch:
Then, To Grab the latest changes from a remote repository into the local repository:
import git repo = git.Repo('name_of_repo') repo.remote().fetch()
To put current working files into the current index:
import git repo.git.add('--all') # to add all the working files. repo.git.add('index.html’,’style.css’) # to add specific working file(s).
To commit indexed changes to a local branch in the local repo.
import git repo.git.add('--all') repo.git.commit('-m', 'commit message from python script', author='[email protected]')
To push all the committed changes in branch from the local repository to the remote repository.
import git repo.git.add('--all') repo.git.commit('-m', 'commit message from python script', author='[email protected]') origin = repo.remote(name='origin') origin.push()
To fetch all the changes from the remote repository to the local repository:
import git repo = git.Repo('name_of_repo') origin = repo.remote(name='origin') origin.pull()
To Merge the current branch to the master (or any specified) branch:
import git repo = git.Repo('.') master = repo.branches['master'] current = repo.branches['feature'] root = repo.merge_base(current, master) repo.index.merge_tree(master, base=root) repo.index.commit('merging master into current branch', parent_commits=(current.commit, master.commit))
To point the current branch to some specific revision or branch and replace all files with the specified revision or branch.
import git repo = git.Repo(‘.’) repo.git.reset('--hard')
Then, there are many more Git operations which we are using for day by day activities and for automating some Git operations. Mention the missing Git operations in the comment. I will try my best to add the content you are looking for.
In this article, we have discussed some useful GIT Operations with Python Scripting to automate DevOps Operations like CICD Automation, Release process and more. However, Python is much more capable to automate more operations of platforms and tools. So, In our upcoming articles and tutorials, we will see all the infrastructural and platform automation with python. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOps, Agile, DevSecOps and App Development.
Experienced DevSecOps Practitioner, Tech Blogger, Expertise in Designing Solutions in Public and Private Cloud. Opensource Community Contributor.