Git For Windows – Hard Clean And Update – Drag&Drop!


This batch script supports drag&drop of a repository folder,
it will normalize the paths to your git-of a choice
(for example you can download x32+portable version from https://github.com/git-for-windows/git/releases/ and place it in a thumb-drive to make it compatible with all Windows machines)

it is compatible with all git versions,
even very old ones.

it is a basic example on how to hard-clean a repository folder,
(starting from submodules) and then updating it.

only thing you need to change it the gitdir value.
you can copy and paste it to a new CMD file and place it in your sendto folder,
for an easy access.

keep the EOL of the file as Windows EOL
and it can be ASCII/UTF-8/Unicode encoded if you need too..

@echo off
::--------------------------------------------::
:: This batch hard-cleans and update a        ::
:: git-repository folder (submodules first).  ::
:: Compatible with all git versions.          ::
::--------------------------------------------::
:: Download git for Windows from:             ::
:: github.com/git-for-windows/git/releases/   ::
:: choose the latest x32 + portable release.  ::
:: extract and read README.portable .         ::
::--------------------------------------------::
::               Elad Karako. September 2017. ::
::--------------------------------------------::


::                      Enable Unicode support.
chcp 65001 2>nul >nul


::you should place your portable-git-path in here (https://github.com/git-for-windows/git/releases/ ---- download x32+portable release)
set "gitdir=C:\git32"


::git normalize to full and short-path
set "GIT=%gitdir%\cmd\git.exe"
if not exist "%GIT%"                 ( goto NOGIT                ) 
for /f %%a in ("%GIT%") do           ( set "GIT=%%~fsa"          ) 


::prioritize current git.exe in PATH (compatible with older git versions)
set "PATH=%GIT%\cmd;%PATH%;"


::target folder normalize to full and short-path
set "TARGET_FOLDER=%~1"
if ["%TARGET_FOLDER%"]==[""]         ( goto NOARG                ) 
if not exist "%TARGET_FOLDER%"       ( goto NOTEXIST             ) 
set "TARGET_FOLDER=%~s1"
if not exist %TARGET_FOLDER%\NUL     ( goto NOTFOLDER            ) 
for /f %%a in ("%TARGET_FOLDER%") do ( set "TARGET_DRIVE=%%~da"  ) 



:: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

echo.
echo using git from:
where "git.exe"
echo.

echo.
echo INFO: Changing To Repository Location: %TARGET_FOLDER% 
call %TARGET_DRIVE%
cd %TARGET_FOLDER%

::hard clean (submodules first then repository) - compatible with older versions.
echo.
echo INFO: Cleaning Repository...
echo.
@echo on
call git submodule foreach git clean -fdx 
call git clean -fdx 
call git clean -d -fx -- "" 
@echo off

::update (submodules first) - compatible with older versions
echo.
echo INFO: Updating Repository...
echo.
@echo on
call git submodule foreach git pull origin master 
call git pull --recurse-submodules 
call git submodule update 
call git submodule update --recursive 
@echo off



goto EXIT

:NOGIT
 echo.
 echo ERROR: no git.exe.
 goto EXIT

:NOARG
 echo.
 echo ERROR: no argument.
 goto EXIT

:NOTEXIST
 echo.
 echo ERROR: not exist.
 goto EXIT

:NOTFOLDER
 echo.
 echo ERROR: not folder.
 goto EXIT

:EXIT
  pause