Python to Extract files from Git Hub Repo through Commits( ID)

Deepak rkm
2 min readAug 18, 2019

Getting the files and Folders of the specific commit might helps us to inspect the entire repository from one point to other point and can be used to test,inspect,review and even for much more utilities.

Conventionally ,we use the command git fetch origin <Commit_ID> and stuck most of the times with error: Server does not allow request for unadvertised object <Commit_ID> in sequence will lead to change the configurations relative to sub module sync and update to solve the highlighted error.

Lets see how we can make use of python to extract the files and folders of GitHub repository of specific commit through Commit ID without getting stuck with above error

Disclaimer: Consider yourself neither contributor nor about to add and commit the files.

Example -Use case: The below solution fits very well to the Manager /team lead who just want to inspect the source/Repository without doing any contribution to the repository.

Solution:

import os

def get_code_4m_commit(repo,commit_id):

try:

os.system(‘git init’)

print(repo)

os.system(‘git pull ‘+repo)

print(“The repo is pulled “)

os.system(‘git reset — hard ‘+commit_id )

except Exception as e:

raise e

if __name__ == ‘__main__’:

repo = input(“Enter the <reponame> branchname(optional)”)

commit_id = input(“Enter the commitId:”)

get_code_4m_commit(repo,commit_id)

Consider saving the above module to pull_specific_commit.py

You may run the module python python pull_specific_commit.py

and you will see the below as output

Happy Learning and lets Pythonify more.

You may motivate by your claps and visit https://www.pylabz.com

Feel Free to reach me @deepakr6242@gmail.com,

Deepak Rayathurai(Quora& linkedin)

--

--

Deepak rkm

proud to be pythonist and aspiring to be sre with AI skills