To automatically replace the git push
command with your custom script, you can use aliases or shell functions in your environment. Here's how to do it for Linux and Windows:
-
Add an alias in your shell configuration file:
- Open your shell configuration file (
~/.bashrc
,~/.zshrc
, or~/.bash_profile
depending on your shell). - Add the following function at the end of the file:
git() { if [ "$1" = "push" ]; then DAY_OF_WEEK=$(date +%u) # Get the day of the week (1 = Monday, 7 = Sunday) if [ "$DAY_OF_WEEK" -eq 5 ]; then # Check if it's Friday (5) read -p "Are you sure you want to push on a Friday? (y/n) " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Push cancelled." return 1 fi fi fi command git "$@" # Execute the original git command }
- Open your shell configuration file (
-
Reload the shell configuration:
- Execute the following command to apply the changes:
source ~/.bashrc # or ~/.zshrc depending on your shell
- Execute the following command to apply the changes:
-
Usage:
- Now, when you type
git push
, the script will automatically check if it's Friday and ask for confirmation if necessary.
- Now, when you type
-
Create a function in your PowerShell profile:
- Open your PowerShell profile by running the following command:
notepad $PROFILE
- If the file doesn't exist, PowerShell will prompt you to create it. Accept the prompt.
- Open your PowerShell profile by running the following command:
-
Add the following function:
- Paste this code into the file:
function git { if ($args[0] -eq "push") { $DayOfWeek = (Get-Date).DayOfWeek.value__ # Get the day of the week (1 = Monday, 7 = Sunday) if ($DayOfWeek -eq 5) { # Check if it's Friday (5) $confirmation = Read-Host "Are you sure you want to push on a Friday? (y/n)" if ($confirmation -ne "y") { Write-Host "Push cancelled." return } } } # Execute the original git command & (Get-Command git -CommandType Application) @args }
-
Reload the PowerShell profile:
- Execute the following command to apply the changes:
. $PROFILE
- Execute the following command to apply the changes:
-
Usage:
- Now, when you type
git push
in PowerShell, the script will automatically check if it's Friday and ask for confirmation if necessary.
- Now, when you type
If you're using the traditional command prompt (cmd.exe
), you can create an alias using a trick with doskey
.
-
Create an alias:
- Open
cmd.exe
and run the following command to create an alias:doskey git=git-wrapper.bat $*
- Open
-
Create a
git-wrapper.bat
script:- Create a
git-wrapper.bat
file in an accessible directory (e.g.,C:\Scripts
) with the following content:
@echo off setlocal if "%1"=="push" ( for /f "tokens=2 delims==" %%I in ('"wmic path win32_localtime get dayofweek /value"') do set DAY_OF_WEEK=%%I if "%DAY_OF_WEEK%"=="5" ( set /p CONFIRM=Are you sure you want to push on a Friday? (y/n) if /i not "%CONFIRM%"=="y" ( echo Push cancelled. exit /b 1 ) ) ) :: Execute the original git command git %*
- Create a
-
Add the script directory to your
PATH
:- Add the directory containing
git-wrapper.bat
(e.g.,C:\Scripts
) to yourPATH
environment variable.
- Add the directory containing
-
Usage:
- Now, when you type
git push
incmd.exe
, the script will automatically check if it's Friday and ask for confirmation if necessary.
- Now, when you type
- Linux: The method with an alias in the shell configuration file is clean and portable.
- Windows: PowerShell is recommended for better integration and flexibility.
- These solutions seamlessly replace the
git push
command without requiring you to type a custom script.
Remember to test in a safe environment before using it in production!