Python code: Automatic git-diff back up

1 minute read

Published:

GIT is a wonderful tool, but it is just a tool. If you already had bad experience with GIT, or just have trust issues, here is a piece of python code, that automatically creates a back up of all (except hidden) git diff files, between 2 branches and re-creates a folder structure.

# inputs
path_to_git_folder = '<path to local git repo>'
diff_branch_0 = '<name of a branch to be backed up>'
diff_branch_1 = '<reference branch name for "git diff">'

import os
import pathlib
import shutil
from datetime import datetime

# get working dir
work_dir = os.getcwd()

# create back-up dir with unique name
now = datetime.now()
dt_string = now.strftime("%Y-%m-%d_%H-%M-%S")
back_up_directory = 'BACK_UP_'+dt_string
back_up_dir = pathlib.Path(work_dir, back_up_directory)
print(back_up_dir)
try:
    os.makedirs(back_up_dir, exist_ok=False)
except OSError as error:
    print(error)


# navigate to git folder & get full path
os.chdir(path_to_git_folder)
git_dir = os.getcwd()

# get list of files that differ in between the branches
stream = os.popen('git diff '+diff_branch_0+'..'+diff_branch_1+' --name-only')
diff_files = stream.read()
list_diff_files = str.splitlines(diff_files)

# loop through files
for file_name in list_diff_files:
    # get path to the file
    file = pathlib.Path(file_name)
    
    # check if file is hidden
    if file.exists():
        # file is not hidden
        # create directory structure for the file
        folder_struct = pathlib.Path(back_up_dir, file.parent)
        os.makedirs(folder_struct, exist_ok=True)
        
        # copy file
        git_file_path     = pathlib.Path(git_dir , file)
        back_up_file_path = pathlib.Path(back_up_dir, file)
        print('file copied from: '+str(git_file_path))
        print('              to: '+str(back_up_file_path))
        shutil.copy(git_file_path, back_up_file_path)
    else:
        # file is hidden
        print('!!! HIDDEN FILE !!! NOT COPIED !!! : '+str(file))