How to handle GitHub updating correctly?

I am relatively new to github and have been trying to program a system for my discord.js bot which would update the bot’s code with my private repositories. For that system I have 3 repositories. The first one authCodeRepoUrl is the repository I made for the general code of the bot (commands, events etc.), so it should always download the newest code from that Repository. The second one is authDataRepoUrl which is for the Data my bot stores in .json files, it should check which file is newer and depending on that change either local or remote Data files. The third one authBackupRepoUrl is so I backup the entire folder from any device into the repository, so I can always check the folder.

Now I have been trying to do this and when i had issues i asked a AI to help me fix the issues, but I’m not sure if that really helped, so I want to ask here for help (sorry in advance).

My code looks like this:

const { exec } = require('child_process');
const path = require('path');
const fs = require('fs');
require('dotenv').config();
const os = require('os');
const codeRepoUrl = process.env.CODE_REPO_URL;
const dataRepoUrl = process.env.DATA_REPO_URL;
const backupRepoUrl = process.env.BACKUP_REPO_URL;
const token = process.env.GITHUB_PAT;
const allowedHost = 'ncpi';
const currentHost = os.hostname();

if (!codeRepoUrl || !dataRepoUrl || !backupRepoUrl || !token) {
    console.error('[ERROR] CODE_REPO_URL, DATA_REPO_URL, BACKUP_REPO_URL, or GITHUB_PAT is not set in .env');
    process.exit(1);
}

const codeRepoPath = path.join(__dirname, './');
const dataRepoPath = path.join(__dirname, 'Data');
const logFilePath = path.join(dataRepoPath, 'sync_log.txt');

const authCodeRepoUrl = codeRepoUrl.replace('https://', `https://${token}@`);
const authDataRepoUrl = dataRepoUrl.replace('https://', `https://${token}@`);
const authBackupRepoUrl = backupRepoUrl.replace('https://', `https://${token}@`);

// Hilfsfunktionen für Git-Operationen
function execPromise(command) {
    return new Promise((resolve, reject) => {
        exec(command, (error, stdout, stderr) => {
            if (error) {
                reject(stderr || stdout);
            } else {
                resolve(stdout);
            }
        });
    });
}

async function getDefaultBranch(repoPath) {
    try {
        const branchName = await execPromise(`git -C ${repoPath} symbolic-ref --short HEAD`);
        return branchName.trim();
    } catch {
        return 'main';
    }
}

async function syncDataRepo() {
    try {
        console.log('[INFO] Syncing data repository...');
        if (!fs.existsSync(dataRepoPath)) {
            console.log('[INFO] Cloning data repository...');
            await execPromise(`git clone ${authDataRepoUrl} ${dataRepoPath}`);
        } else {
            console.log('[INFO] Pulling latest changes for the data repository...');
            await execPromise(`git -C ${dataRepoPath} pull`);
        }

        await execPromise(`git -C ${dataRepoPath} branch --set-upstream-to=origin/main main`);

        const localChanges = await execPromise(`git -C ${dataRepoPath} status --porcelain`);
        if (localChanges.trim().length > 0) {
            console.log('[INFO] Local changes detected. Pushing to remote repository...');
            await execPromise(
                `git -C ${dataRepoPath} add . && git -C ${dataRepoPath} commit -m "Auto-sync: ${new Date().toISOString()}" && git -C ${dataRepoPath} push`
            );
        }

        console.log('[INFO] Data repository synchronized.');
        logSyncSuccess();
    } catch (error) {
        console.error('[ERROR] Failed to synchronize data repository:', error);
    }
}


function logSyncSuccess() {
    const timestamp = `[SUCCESS] Data repository synchronized at: ${new Date().toISOString()}nn`;
    try {
        fs.appendFileSync(logFilePath, timestamp, 'utf8');
        console.log('[INFO] Sync timestamp logged.');
    } catch (err) {
        console.error('[ERROR] Failed to write sync log:', err);
    }
}

async function syncCodeRepo() {
    if (currentHost !== allowedHost) {
        console.log('[INFO] Code synchronization skipped: Not running on the allowed device.');
        return;
    }

    try {
        console.log('[INFO] Syncing code repository...');
        const branchName = await getDefaultBranch(codeRepoPath);

        if (!fs.existsSync(path.join(codeRepoPath, '.git'))) {
            console.log('[INFO] Cloning code repository...');
            await execPromise(`git clone ${authCodeRepoUrl} ${codeRepoPath}`);
        } else {
            console.log('[INFO] Pulling latest changes for the code repository...');
            try {
                await execPromise(`git -C ${codeRepoPath} remote add origin ${authCodeRepoUrl}`);
            } catch (error) {
                if (!error.includes('remote origin already exists')) {
                    throw error;
                }
            }
            await execPromise(`git -C ${codeRepoPath} pull origin ${branchName}`);
        }

        await execPromise(`git -C ${codeRepoPath} branch --set-upstream-to=origin/${branchName} ${branchName}`);

        console.log('[INFO] Code repository synchronized.');
    } catch (error) {
        console.error('[ERROR] Failed to synchronize code repository:', error);
    }
}


async function backupToRemoteRepo() {
    if (currentHost !== allowedHost) {
        console.log('[INFO] Backup skipped: Not running on the allowed device.');
        return;
    }

    try {
        console.log('[INFO] Backing up directly to remote repository...');
        const branchName = 'main';

        if (!fs.existsSync(path.join(codeRepoPath, '.git'))) {
            console.log('[INFO] Initializing repository for backup...');
            await execPromise(`git -C ${codeRepoPath} init`);
            await execPromise(`git -C ${codeRepoPath} checkout -b ${branchName}`);
            await execPromise(`git -C ${codeRepoPath} remote add origin ${authBackupRepoUrl}`);
        }

        await execPromise(`git -C ${codeRepoPath} config user.email "email censored"`);
        await execPromise(`git -C ${codeRepoPath} config user.name "github user censored"`);

        await execPromise(`git -C ${codeRepoPath} add .`);
        await execPromise(`git -C ${codeRepoPath} commit -m "Backup: ${new Date().toISOString()}"`);
        await execPromise(`git -C ${codeRepoPath} push origin ${branchName} --force`);

        console.log('[INFO] Backup to remote repository completed.');
    } catch (error) {
        console.error('[ERROR] Failed to backup to remote repository:', error);
    }
}


async function checkAndUpdate() {
    console.log('[INFO] Starting update process...');
    await syncCodeRepo();
    await syncDataRepo();
    await backupToRemoteRepo();
    console.log('[INFO] Update process completed.');
    scheduleNextSync();
}

function scheduleNextSync() {
    const now = new Date();
    const minutes = now.getMinutes();
    const nextInterval = 10 - (minutes % 10);
    const delay = nextInterval * 60 * 1000;

    console.log(`[INFO] Next sync scheduled in ${nextInterval} minutes (${new Date(now.getTime() + delay).toLocaleTimeString()}).`);

    setTimeout(() => {
        checkAndUpdate().then(scheduleRecurringSync);
    }, delay);
}

function scheduleRecurringSync() {
    console.log('[INFO] Scheduling recurring sync every 10 minutes.');
    setInterval(checkAndUpdate, 10 * 60 * 1000);
}

module.exports = { checkAndUpdate };

This is the log data, which comes when i try the code:

0| [ERROR] Failed to synchronize data repository: There is no tracking information for the current branch.
0| Please specify which branch you want to merge with.
0| See git-pull(1) for details.
0|     git pull <remote> <branch>
0| If you wish to set tracking information for this branch you can do so with:
0|     git branch --set-upstream-to=<remote>/<branch> master
0| [INFO] Backing up directly to remote repository...
0| [ERROR] Failed to backup to remote repository: error: src refspec main does not match any
0| error: failed to push some refs to 'https://github.com/censored/censored'
0| [INFO] Update process completed.
0| [INFO] Next sync scheduled in 10 minutes (04:10:37).
0| [INFO] Bot and data repositories are up-to-date. Starting bot...
0| [INFO] Starting update process...
0| [INFO] Syncing code repository...
0| [INFO] Pulling latest changes for the code repository...
0| [ERROR] Failed to synchronize code repository: fatal: couldn't find remote ref master
0| [INFO] Syncing data repository...
0| [INFO] Pulling latest changes for the data repository...
0| [ERROR] Failed to synchronize data repository: There is no tracking information for the current branch.
0| Please specify which branch you want to merge with.
0| See git-pull(1) for details.
0|     git pull <remote> <branch>
0| If you wish to set tracking information for this branch you can do so with:
0|     git branch --set-upstream-to=<remote>/<branch> master
0| [INFO] Backing up directly to remote repository...
0| [ERROR] Failed to backup to remote repository: On branch master
0| nothing to commit, working tree clean
0| [INFO] Update process completed.
0| [INFO] Next sync scheduled in 10 minutes (04:20:39).
0| [INFO] Scheduling recurring sync every 10 minutes.

I appreciate every help, i really just want this to work.
Thanks in advance.