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:
- Creating a local folder if needed
- Initializing a Git repo with
master
branch - Adding
_venv
to.gitignore
- Creating a GitHub repo using the
gh
CLI - Making the first commit
- Setting remote and pushing to GitHub
How do I use it?
Just run it like this:
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
tomaster
- 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:
GitHub CLI (gh) is not installed. → Download: https://cli.github.com/
Or, if not authenticated:
Please run 'gh auth login' to authenticate with GitHub.
Then the script exits safely.
Behind the Scenes
It uses:
git init -b master gh repo create ... git push -u origin master
…and adds _venv
to .gitignore
automatically.
Full Script: firstrepo.bat
@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:
C:\Windows\
…so you can run firstrepo.bat my-project
from any folder.
Full Stack Software Engineer | AI Author – Credentials
Experience: 20+ Years in Software Development
Credentials: B.E. Computer, SVNIT Surat (2004)