Instantly Push a New GitHub Repo Using One .bat File

Instantly Push a New GitHub Repo Using One .bat File

Creating a new GitHub repository manually each time?
Let’s automate the entire process — from folder creation to the first commit — using a single reusable .bat file.

This guide explains the how and why in a simple Q\&A format, and gives you the full working script.


What does firstrepo.bat do?

It automates:

  1. Creating a local folder if needed
  2. Initializing a Git repo with master branch
  3. Adding _venv to .gitignore
  4. Creating a GitHub repo using the gh CLI
  5. Making the first commit
  6. Setting remote and pushing to GitHub

How do I use it?

Just run it like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
firstrepo.bat myname
firstrepo.bat myname
firstrepo.bat myname

It’ll set everything up and open your GitHub repo instantly.


What does it prevent?

  • Double nesting like desoise-images/desoise-images/
  • Missing GitHub credentials
  • Forgetting to switch from main to master
  • Missing .gitignore for _venv

What if I’m already inside the repo folder?

No problem. The script checks the folder name and skips mkdir and cd if you’re already there.


What if gh CLI is not installed or not authenticated?

You’ll get this clear message:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
GitHub CLI (gh) is not installed. → Download: https://cli.github.com/
GitHub CLI (gh) is not installed. → Download: https://cli.github.com/
GitHub CLI (gh) is not installed. → Download: https://cli.github.com/

 

Or, if not authenticated:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Please run 'gh auth login' to authenticate with GitHub.
Please run 'gh auth login' to authenticate with GitHub.
Please run 'gh auth login' to authenticate with GitHub.

 

Then the script exits safely.


Behind the Scenes

It uses:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git init -b master
gh repo create ...
git push -u origin master
git init -b master gh repo create ... git push -u origin master
git init -b master 
gh repo create ... 
git push -u origin master

 

…and adds _venv to .gitignore automatically.


Full Script: firstrepo.bat

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@echo off
setlocal
:: Check if repo name is passed
if "%~1"=="" (
echo Usage: firstrepo.bat YourNewRepoName
exit /b
)
set "REPO_NAME=%~1"
set "GITHUB_USER=vikaskbh"
set "VISIBILITY=private"
:: Check if GitHub CLI is installed
where gh >nul 2>&1
if errorlevel 1 (
echo GitHub CLI (gh) is not installed.
echo → Download: https://cli.github.com/
exit /b
)
:: Check if gh is authenticated
gh auth status >nul 2>&1
if errorlevel 1 (
echo Please run 'gh auth login' to authenticate with GitHub.
exit /b
)
echo.
echo Requested repo: %REPO_NAME%
:: Get current folder name
for %%I in ("%CD%") do set "CURRENT_FOLDER=%%~nxI"
if /I "%CURRENT_FOLDER%"=="%REPO_NAME%" (
echo Already in %REPO_NAME% folder, skipping mkdir and cd
) else (
if not exist "%REPO_NAME%" (
echo Creating folder: %REPO_NAME%
mkdir "%REPO_NAME%"
)
cd "%REPO_NAME%"
)
echo.
echo Initializing Git...
git init -b master
echo _venv> .gitignore
echo.
echo First commit
git add .
git commit -m "Initial commit"
echo.
echo Creating GitHub repo: %REPO_NAME% (%VISIBILITY%)
gh repo create %GITHUB_USER%/%REPO_NAME% --%VISIBILITY% --source=. --remote=origin --push
:: Fallback remote add in case gh fails
git remote add origin https://github.com/%GITHUB_USER%/%REPO_NAME%.git 2>nul
echo.
echo Pushing to GitHub...
git push -u origin master
echo.
echo Repo created and pushed: https://github.com/%GITHUB_USER%/%REPO_NAME%
endlocal
@echo off setlocal :: Check if repo name is passed if "%~1"=="" ( echo Usage: firstrepo.bat YourNewRepoName exit /b ) set "REPO_NAME=%~1" set "GITHUB_USER=vikaskbh" set "VISIBILITY=private" :: Check if GitHub CLI is installed where gh >nul 2>&1 if errorlevel 1 ( echo GitHub CLI (gh) is not installed. echo → Download: https://cli.github.com/ exit /b ) :: Check if gh is authenticated gh auth status >nul 2>&1 if errorlevel 1 ( echo Please run 'gh auth login' to authenticate with GitHub. exit /b ) echo. echo Requested repo: %REPO_NAME% :: Get current folder name for %%I in ("%CD%") do set "CURRENT_FOLDER=%%~nxI" if /I "%CURRENT_FOLDER%"=="%REPO_NAME%" ( echo Already in %REPO_NAME% folder, skipping mkdir and cd ) else ( if not exist "%REPO_NAME%" ( echo Creating folder: %REPO_NAME% mkdir "%REPO_NAME%" ) cd "%REPO_NAME%" ) echo. echo Initializing Git... git init -b master echo _venv> .gitignore echo. echo First commit git add . git commit -m "Initial commit" echo. echo Creating GitHub repo: %REPO_NAME% (%VISIBILITY%) gh repo create %GITHUB_USER%/%REPO_NAME% --%VISIBILITY% --source=. --remote=origin --push :: Fallback remote add in case gh fails git remote add origin https://github.com/%GITHUB_USER%/%REPO_NAME%.git 2>nul echo. echo Pushing to GitHub... git push -u origin master echo. echo Repo created and pushed: https://github.com/%GITHUB_USER%/%REPO_NAME% endlocal
@echo off
setlocal

:: Check if repo name is passed
if "%~1"=="" (
    echo Usage: firstrepo.bat YourNewRepoName
    exit /b
)

set "REPO_NAME=%~1"
set "GITHUB_USER=vikaskbh"
set "VISIBILITY=private"

:: Check if GitHub CLI is installed
where gh >nul 2>&1
if errorlevel 1 (
    echo GitHub CLI (gh) is not installed.
    echo → Download: https://cli.github.com/
    exit /b
)

:: Check if gh is authenticated
gh auth status >nul 2>&1
if errorlevel 1 (
    echo Please run 'gh auth login' to authenticate with GitHub.
    exit /b
)

echo.
echo  Requested repo: %REPO_NAME%

:: Get current folder name
for %%I in ("%CD%") do set "CURRENT_FOLDER=%%~nxI"

if /I "%CURRENT_FOLDER%"=="%REPO_NAME%" (
    echo Already in %REPO_NAME% folder, skipping mkdir and cd
) else (
    if not exist "%REPO_NAME%" (
        echo  Creating folder: %REPO_NAME%
        mkdir "%REPO_NAME%"
    )
    cd "%REPO_NAME%"
)

echo.
echo  Initializing Git...
git init -b master

echo _venv> .gitignore

echo.
echo First commit
git add .
git commit -m "Initial commit"

echo.
echo  Creating GitHub repo: %REPO_NAME% (%VISIBILITY%)
gh repo create %GITHUB_USER%/%REPO_NAME% --%VISIBILITY% --source=. --remote=origin --push

:: Fallback remote add in case gh fails
git remote add origin https://github.com/%GITHUB_USER%/%REPO_NAME%.git 2>nul

echo.
echo  Pushing to GitHub...
git push -u origin master

echo.
echo Repo created and pushed: https://github.com/%GITHUB_USER%/%REPO_NAME%

endlocal

 


Where should I keep it?

You can put it inside:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\Windows\
C:\Windows\
C:\Windows\

 

…so you can run firstrepo.bat my-project from any folder.

 

Total
0
Shares
Previous Post

ReferenceError: X is not a constructor – Common Instantiation Problems