Auto Commit Hot Key(s) – Windows 11

Tech Articles | February 19, 2025 | Automation, Coding, Windows

Sometimes, I am on a mission when coding a project, and this means I can make literally 100’s of changes to code over a very short period, and this does result in seriously broken features, occasionally, usually when adding additional features, and there is an issue with backwards compatibility.

I have my dev environment pretty well sorted out now, I can create a repo, clone it to a folder on my machine and then use syncbackpro to sync that folder content to a subfolder of my ninja updater plugin, this has a built-in snippet manager which lets me load those files as snippets for working on them without ever having to manually upload, FTP or worry about 500 errors, I save a revised version and its there within a few minutes ready for the next iteration.

I will create a folder on the above workflow sometime in the future, as it’s an interesting way to work; what I have struggled with is git commits; having to open a cmd line and type git commit and a message is too much of an interruption to my workflow. (even though I have a quick link to do just that on my right mouse click on my Windows machine.

I think I have finally nailed this workflow, though, thanks to a bat script and my stream deck (which I only use to shortcode SSH and other works and web URLs

@echo off

:: Define variables at the top
set GIT_PATH="git"
set CHANGELOG=changelog.txt
set IGNORE_MARKER=gitignore.success
set BRANCH=main
set ACTION=1

:: Explanation of variables
:: Set BRANCH to the desired branch (e.g., main, origin, working)
:: Set ACTION to 1 for commit & push, 2 for pull, 3 to exit

:: Ensure changelog.txt exists
if not exist %CHANGELOG% (
    echo Change Log > %CHANGELOG%
    echo ---------------- >> %CHANGELOG%
)

:: Ensure .gitignore is updated only once by checking for marker file
if not exist %IGNORE_MARKER% (
    echo changelog.txt >> .gitignore
    echo %IGNORE_MARKER% >> .gitignore
    echo .gitignore successfully updated on %date% at %time% > %IGNORE_MARKER%

    %GIT_PATH% add .gitignore %IGNORE_MARKER%
    %GIT_PATH% commit -m "Updated .gitignore to exclude changelog.txt"
    %GIT_PATH% push origin %BRANCH%
)

:: Execute the selected action automatically
if "%ACTION%"=="1" (
    :: Append relevant version changes with file names to changelog.txt before committing
    echo Commit on %date% at %time% >> %CHANGELOG%
    
    :: Get list of files with relevant changes, then extract specific versioning changes
    for /f "delims=" %%f in ('%GIT_PATH% diff --name-only') do (
        echo File: %%f >> %CHANGELOG%
        %GIT_PATH% diff -U0 %%f | findstr /r /c:"// save version" /c:"// function version" /c:"// module version" >> %CHANGELOG%
    )
    
    echo -------------------------------------------------- >> %CHANGELOG%
    
    :: Commit changes
    %GIT_PATH% add -A
    %GIT_PATH% commit -am "Auto-committed on %date% at %time%"
    
    :: Commit the changelog, then push everything
    %GIT_PATH% add %CHANGELOG%
    %GIT_PATH% commit -m "Updated changelog on %date% at %time%"
    %GIT_PATH% push origin %BRANCH%
    
    echo Changes have been committed, logged, and pushed to %BRANCH%.
)

if "%ACTION%"=="2" (
    %GIT_PATH% pull origin %BRANCH%
    echo Changes have been pulled from %BRANCH%.
)

if "%ACTION%"=="3" exit /b

echo.
echo Operation complete.
BAT (Batchfile)

The above script automatically pushes the to main when activated with an auto comment of date and time. I keep a digital notebook beside me and make time notes about what I’ve done before I push it, even if it 100 times in a few hours, and I can then work out what caused the issues.

I have one that pushes to dev and one for main for each of the current projects I am working on.

If I am working locally, it can be even faster as I don’t need the push-up and down for the files via syncbackpro it can all be done from the local directory and browser.

I also have a version that accepts arguments for use on the command line if already using WPCli

@echo off

:: Define default values
set GIT_PATH="git"
set CHANGELOG=changelog.txt
set IGNORE_MARKER=gitignore.success
set BRANCH=main
set ACTION=1

:: Accept command-line arguments (if provided)
if not "%~1"=="" set BRANCH=%~1
if not "%~2"=="" set ACTION=%~2

:: Ensure changelog.txt exists
if not exist %CHANGELOG% (
    echo Change Log > %CHANGELOG%
    echo ---------------- >> %CHANGELOG%
)

:: Ensure .gitignore is updated only once by checking for marker file
if not exist %IGNORE_MARKER% (
    echo changelog.txt >> .gitignore
    echo %IGNORE_MARKER% >> .gitignore
    echo .gitignore successfully updated on %date% at %time% > %IGNORE_MARKER%

    %GIT_PATH% add .gitignore %IGNORE_MARKER%
    %GIT_PATH% commit -m "Updated .gitignore to exclude changelog.txt"
    %GIT_PATH% push origin %BRANCH%
)

:: Execute the selected action automatically
if "%ACTION%"=="1" (
    :: Append relevant version changes with file names to changelog.txt before committing
    echo Commit on %date% at %time% >> %CHANGELOG%
    
    :: Get list of files with relevant changes, then extract specific versioning changes
    for /f "delims=" %%f in ('%GIT_PATH% diff --name-only') do (
        echo File: %%f >> %CHANGELOG%
        %GIT_PATH% diff -U0 %%f | findstr /r /c:"// save version" /c:"// function version" /c:"// module version" >> %CHANGELOG%
    )
    
    echo -------------------------------------------------- >> %CHANGELOG%
    
    :: Commit changes
    %GIT_PATH% add -A
    %GIT_PATH% commit -am "Auto-committed on %date% at %time%"
    
    :: Commit the changelog, then push everything
    %GIT_PATH% add %CHANGELOG%
    %GIT_PATH% commit -m "Updated changelog on %date% at %time%"
    %GIT_PATH% push origin %BRANCH%
    
    echo Changes have been committed, logged, and pushed to %BRANCH%.
)

if "%ACTION%"=="2" (
    %GIT_PATH% pull origin %BRANCH%
    echo Changes have been pulled from %BRANCH%.
)

if "%ACTION%"=="3" exit /b

echo.
echo Operation complete.
BAT (Batchfile)

And an old-school version that accepts keyed options

@echo off
echo Select a branch:
echo 1. main
echo 2. origin
echo 3. working
set /P BRANCH_OPTION=Enter branch number (1-3): 

:: Map the branch option to branch names
if "%BRANCH_OPTION%"=="1" set BRANCH=main
if "%BRANCH_OPTION%"=="2" set BRANCH=origin
if "%BRANCH_OPTION%"=="3" set BRANCH=working

echo.
echo Select an action:
echo 1. Commit and Push
echo 2. Pull
echo 3. Exit
set /P ACTION_OPTION=Enter action number (1-3): 

:: Define the Git path
set GIT_PATH="git"
set CHANGELOG=changelog.txt

:: Check if changelog.txt exists; if not, create it
if not exist %CHANGELOG% (
    echo Change Log > %CHANGELOG%
    echo ---------------- >> %CHANGELOG%
)

:: Execute the selected action
if "%ACTION_OPTION%"=="1" (
    :: Append changes to changelog.txt before committing
    echo Commit on %date% at %time% >> %CHANGELOG%
    %GIT_PATH% diff >> %CHANGELOG%
    echo -------------------------------------------------- >> %CHANGELOG%
    
    :: Commit changes
    %GIT_PATH% add -A
    %GIT_PATH% commit -am "Auto-committed on %date% at %time%"
    
    :: Commit the changelog, then push everything
    %GIT_PATH% add %CHANGELOG%
    %GIT_PATH% commit -m "Updated changelog on %date% at %time%"
    %GIT_PATH% push origin %BRANCH%
    
    echo Changes have been committed, logged, and pushed to %BRANCH%.
)
if "%ACTION_OPTION%"=="2" (
    %GIT_PATH% pull origin %BRANCH%
    echo Changes have been pulled from %BRANCH%.
)
if "%ACTION_OPTION%"=="3" exit /b

:: Ensure changelog.txt is ignored in future changes
echo changelog.txt >> .gitignore
%GIT_PATH% add .gitignore
%GIT_PATH% commit -m "Updated .gitignore to exclude changelog.txt"
%GIT_PATH% push origin %BRANCH%

echo.
echo Operation complete.
BAT (Batchfile)

Support the Author

buy me a coffee
Really Useful Plugin Logo
Appoligies for any spelling and grammer issue. As a dyslexic i need to rely on tools for this they like me are not perfect but I do try my best