DEV Community

Git Bash on Windows: adding a permanent alias

Hafiz Jaafar on July 28, 2020

If you are using Git Bash on Windows and you're used to Linux bash commands, chances are that you'd like to add aliases that help making your jobs ...
Collapse
 
adeyemiadekore2 profile image
korey 🇳🇬 • Edited

thanks for this.

a little thing to note is this works while navigating in bash

cd /c/'Program Files'/Git/etc/profile.d
not
cd /c/Program Files/Git/etc/profile.d as this one won't work in bash

Collapse
 
cornelius_figgle profile image
Max

also to note: my git installation was in the AppData folder, so my path looked like this:

"%LocalAppData%\Programs\Git\etc\profile.d\aliases.sh"
OR
"C:\Users\<username>\AppData\Local\Programs\Git\etc\profile.d\aliases.sh"
OR
"~/AppData/Local/Programs/Git/etc/profile.d/aliases.sh"
OR
"/c/Users/<username>/AppData/Local/Programs/Git/etc/profile.d/aliases.sh"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
natank profile image
natank

Note: place the equal sign ('=') immediately after the alias without any spaces.
Examples:

alias k = 'kubectl' // not working
alias k= 'kubectl' // working

Collapse
 
prateek_aher profile image
Prateek Aher

Dude, you're a lifesaver.

Collapse
 
abiruzzamanmolla profile image
Abiruzzaman Molla

Tired of typing git add . && git commit -m "msg" && git push every time?
I made a gitupdate() Bash function that does it all — with colours & a spinner.

-Stage
-Commit
-Push

One line in Git Bash.
gitupdate "commit message" [branch]

gitupdate() {
    commitMessage="$1"
    branch="$2"

    if [ -z "$commitMessage" ]; then
        echo -e "\033[31mError:\033[0m Commit message is required."
        echo "Usage: gitupdate \"commit message\" [branch]"
        return 1
    fi

    echo -e "\n\033[33mExecuting gitupdate command with the following actions:\033[0m"
    echo "  - Staging all changes in the repository."
    echo "  - Committing changes with message '$commitMessage'."
    if [ -n "$branch" ]; then
        echo -e "  - Pushing changes to branch '\033[33m$branch\033[0m'."
    else
        echo -e "  - Pushing changes to the current branch."
    fi

    echo -e "\n\033[36mCurrent Git status:\033[0m"
    git status

    echo -e "\n\033[32mStaging all changes...\033[0m"
    spin &
    SPIN_PID=$!
    trap "kill -9 $SPIN_PID" $(seq 0 15)
    git add .
    kill -9 $SPIN_PID 2>/dev/null
    echo -e "\nFiles added to staging area successfully."

    echo -e "\n\033[35mCommitting changes with message '$commitMessage'...\033[0m"
    spin &
    SPIN_PID=$!
    trap "kill -9 $SPIN_PID" $(seq 0 15)
    git commit -m "$commitMessage"
    kill -9 $SPIN_PID 2>/dev/null
    echo -e "\nChanges committed successfully."

    if [ -n "$branch" ]; then
        echo -e "\n\033[33mPushing changes to branch '$branch'...\033[0m"
        spin &
        SPIN_PID=$!
        trap "kill -9 $SPIN_PID" $(seq 0 15)
        git push origin "$branch"
        kill -9 $SPIN_PID 2>/dev/null
    else
        echo -e "\n\033[32mPushing changes to current branch...\033[0m"
        spin &
        SPIN_PID=$!
        trap "kill -9 $SPIN_PID" $(seq 0 15)
        git push
        kill -9 $SPIN_PID 2>/dev/null
    fi
    echo -e "\n\033[36mChanges pushed successfully.\033[0m"

    echo -e "\n\033[36mUpdated Git status:\033[0m"
    git status
}

# Spinner animation for bash
spin() {
    local spinner='-\|/'
    while :; do
        for i in $(seq 0 3); do
            printf "\b${spinner:$i:1}"
            sleep 0.1
        done
    done
}

# Alias for convenience
alias gitupdate="gitupdate"

Enter fullscreen mode Exit fullscreen mode
Collapse
 
flyingduck92 profile image
Sekti Wicaksono

I always use ll with

alias ll='ls -lash'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
binayashrestha profile image
Binaya Shrestha • Edited

thanks a lot.
also worked multi-line alias like this:

dcosql() {
  cd ~
  cd /d/dio/dio-scripts/docker/development/
  docker-compose up -d mssql
  }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
adejota profile image
Ademir Diamente Júnior

Tks, dude! It really helped me 😁

Collapse
 
cuneyter profile image
cuneyter

Thank you. That is a great help :)

Collapse
 
florincornea profile image
Cornea Florin

Exactly what I was looking for, thanks!!

Collapse
 
rsbmk profile image
Roberto Bocio Melo

thanks for this, my workflow thanks you ❤️

Collapse
 
chrisdev_io profile image
Christopher Thompson

Great little bit of advice! Totally worked for me. Thank you. :D

Collapse
 
sddecent profile image
Decent Coder

Aliases can be created using git config command as well, without actually opening the git files.

Collapse
 
cos2004 profile image
yuly

so helpful, thanks