Git

How to Execute Git Commands Directly from Python?

Git is the command line terminal tool to manage projects while Python is the most popular programming language. Creating and managing the repositories for programming is an easy task using the Git Bash terminal.

However, it is much more interesting and helpful to execute Git commands directly from Python. Helpful in a way that you don’t need to open the Git Bash terminal separately. For this purpose, Python has some built-in functions specifically for Git operations to be performed.

This post will illustrate the guide for executing the Git commands directly from Python.

How to Execute Git Commands Directly from Python?

As mentioned earlier, Python allows you to execute Git commands directly from Python. Let’s perform the basic operations of Git Bash like initializing a repository, cloning, updating changes, and pushing it to the remote host from the Python code. For instance, we are using the Thonny IDE to execute and run the Python code.

Step 1: Initialize the Local Repository

Open the Thonny, import the Git library, write the variable name, initialize the repository using the “Repo.init()” function and define the repository path inside it. In the end, print the message on the screen for the successful execution of the code:

from git import Repo
new_repo = Repo.init('D:\Python')
print(f'Given directory is initialized')

 

From the above execution of the code, the particular directory has been initialized:

Step 2: Clone Remote Repository

For cloning the Git repository from the remote host, store the remote URL and local directory path in the variables. Afterward, use the “git.repo.clone_from()”, put both variables inside it and print the message. Check out the below-given code:

import git

# Clone a remote repository
repo_url = "https://github.com/Mateen900/perk"
local_path = "D:\Python\Clone"
repo = git.Repo.clone_from(repo_url, local_path)
print(f'Project Repository has been cloned: {local_path}')

 

After executing the above code, the remote repository will be cloned in the specified path.

For the verification, open the local directory and verify it:

Step 3: Track Changes and Commit

After cloning the repository, you can now change/add the files/changes in the cloned project, track, and commit it. For instance:

  • We have added two files “test1.txt” and “test2.txt” in the cloned project. So, to add the changes to the tracking index, as usual, import the Git library, specify the local directory path in the variable, and use these variables to include that particular added files.
  • Then, use the “repo.index.add()” function and enter the file’s variables inside it.
  • Lastly, print the message on the screen:

The added files have been tracked.

Similarly, for committing the changes use the “repo.index.commit()” function as the following code is showing:

To verify whether the files has been added and committed or not, open the local directory, and you will see the red sign on these files:

Step 4: Push the Changes to the Remote Host

Finally, push the applied changes to the remote host. To do so:

  • Import the Git library, include the directory path, and specify the remote host name inside the “repo.remote()” function.
  • Next, define a branch using the “repo.heads[]” function and call the checkout function on it.
  • Lastly, push the code remote host through the “origin.push()” function and print the message on the screen:
import git

repo = git.Repo("D:\Python\clone")
origin = repo.remote(name='origin')

existing_branch = repo.heads['main']
existing_branch.checkout()

origin.push()
print('Changes pushed to the remote host')

 

The change has been pushed to the remote host.

Step 5: Verification

Let’s verify out remote host for the updated changes:

From the above image, it is clear that the changes have been pushed to our remote host.

These are the basic instructions for using Git directly from Python code.

Conclusion

To execute Git commands directly from Python, open Python (Thonny) IDE, import the Git library; use the basic functions like “Repo.git()”, “git.repo.clone_from()”, “repo.index.add()”, and “repo.index.commit()” functions. This guide has practically performed the instructions to execute Git commands directly from Python.

About the author

Abdul Mateen

I hold a bachelor's degree in Computer Science and have a good technical background. As an author, I break down technical concepts in a clear and concise manner. Learning new skills and exploring new technologies is my passion.