Git for Windows: .bashrc or equivalent configuration files for Git Bash shell
Asked Answered
S

16

389

I've just installed Git for Windows and am delighted to see that it installs Bash.

I want to customise the shell in the same way I can under Linux (e.g. set up aliases like ll for ls -l), but I can't seem to find .bashrc or equivalent configuration files.

What should I be editing?

Sadness answered 30/7, 2011 at 14:27 Comment(2)
Read this for more recent versions : https://mcmap.net/q/12601/-git-for-windows-doesn-39-t-execute-my-bashrc-fileFrissell
For the WSL bash see this: stackoverflow.com/questions/40820822/…Shala
B
444

Create a .bashrc file under ~/.bashrc and away you go. Similarly for ~/.gitconfig.

~ is usually your C:\Users\<your user name> folder. Typing echo ~ in the Git Bash terminal will tell you what that folder is.

If you can't create the file (e.g. running Windows), run the below command:

copy > ~/.bashrc

The window will output an error message (command not found), but the file will be created and ready for you to edit.

Alternatively, you can create the ~/.bashrc file with:

touch ~/.bashrc
Barytone answered 30/7, 2011 at 14:33 Comment(16)
Where is the ~/.bashrc folder?Foliolate
It's a file, not a folder. ~/.bashrc is in your home directory, so it should be the directory you're in as soon as you start the git bash shell. just create a file called .bashrc and .gitconfig there.Barytone
On my system ~ is pointing to c:\users\MyUser where MyUser is the currently logged in userSadness
How do you create a file called .profile or .bashrc in ~? Everytime I try to do it in Explorer I get an error saying I must add a file nameChaperone
Ah, nevermind, I achieved this simply by echoing to a file called ~/.bashrcChaperone
Since a few people have asked about ~, I've edited the answer to clarifyBarytone
To create the .bashrc file you can execute the following from DOS prompt: copy > .bashrc. It creates the file with "The syntax of the command is incorrect." but you can edit it now.Genitourinary
Just type notepad ~/.bashrc from the bash prompt and notepad will be launched with this file opened or will ask to create it, if it doesn't exist. If you're comfortable to vim, just type vim ~/.bashrc. It worked nicely to me.Telefilm
pwd in gitbash is also super useful for seeing the bash-ified windows path. In addition, where [util] is nice for seeing the windows path to utils. For example where git will tell you (in windows style) where git is. (which [util] will give you the bash path)Vogul
There are a trick in windows to create files like .profile (without letters before dot): right click -> create new file -> text file -> name it .profile. (NOTE THE DOT at the end) -> hit enterPueblo
You can also simply type touch ~/.bashrcNabataean
Be sure to read the comment below from @harsel about .bash_profile. I needed to add the .bash_profile to my user directory ~/ on my Windows 10 Git Bash otherwise none of my alias' worked.Mincemeat
You can use Window Explorer to create a text file (be sure to include viewing extensions) and rename the file and file extension to .bashrc.Ferguson
On Windows 10, the path to your home dir is usually /c/Users/<userName>, assuming the standard C drive letter.Leshia
“.. but the file will be created and ready for you to edit…” - priceless.Nakano
For anyone wanting to see Ubuntu's default ~/.profile or ~/.bashrc files in order to copy stuff from them, they are located on Ubuntu in the /etc/skel/ dir. Since I can't find that directory online anywhere, I have copied it to my eRCaGuy_dotfiles repo here: /etc/skel/. See the default Ubuntu ~/.profile and ~/.bashrc files there.Anglicanism
T
170

In newer versions of Git for Windows, Bash is started with --login which causes Bash to not read .bashrc directly. Instead it reads .bash_profile.

If ~/.bash_profile file does not exist, create it with the following content:

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

This will cause Bash to read the .bashrc file. From my understanding of this issue, Git for Windows should do this automatically. However, I just installed version 2.5.1, and it did not.

Tattle answered 2/9, 2015 at 12:24 Comment(6)
Thanks. This was exactly what was happening with my setup.Sheepshank
Indeed, that's what happened to me recently after upgrade to 2.5. Thanks for your answer.Bleach
Thanks from me too. it was driving me nuts that Git Bash was ignoring my .bashrc file, so your answer worked like a charm.Lenoir
"Just type notepad ~/.bashrc from the bash prompt and notepad will be launched with this file opened or will ask to create it, if it doesn't exist. If you're comfortable to vim, just type vim ~/.bashrc. It worked nicely to me. – Gerardo Lima" That solution work for meEuh
Where should .bash_profile be created? Inside ~?Hardison
With 2.30.0.windows.1 git issues a WARNING and does this automatically for you. ``` WARNING: Found ~/.bashrc but no ~/.bash_profile, ~/.bash_login or ~/.profile. This looks like an incorrect setup. A ~/.bash_profile that loads ~/.bashrc will be created for you. ```Jeroldjeroma
E
16

Please use the following command,

cat /etc/bash.bashrc > ~/.bashrc

This will generate a new bashrc file with the default values. Please use vi ~/.bashrc to edit this file.

Eisenstark answered 11/6, 2020 at 4:18 Comment(4)
this should be an accepted answerEmpirin
Agree with @Empirin this should be the accepted answerLamonicalamont
Yes; Gitbash put it elsewhere, other than ~/.bashrc. Was wondering how $PS1 and other things are defined. I don't want to create it under ~, it's mixing Windows user dir with Gitbash specific configs.Soever
@vsync Not true - this works perfectly fine on windowsLegible
L
10

I had to add a user environment variable, HOME, with C:\Users\<your user name> by going to System, Advanced System Settings, in the System Properties window, the Advanced tab, Environment Variables...

Then in my C:\Users\<your user name> I created the file .bashrc, e.g., touch .bashrc and added the desired aliases.

Lepidus answered 30/9, 2014 at 5:35 Comment(1)
add the user environment variable HOME and then create a .bashrc file with the required contents for example: #!/bin/bash export TERM=msysMarquettamarquette
M
10

Just notepad ~/.bashrc from the git bash shell and save your file.That should be all.

NOTE: Please ensure that you need to restart your terminal for changes to be reflected.

Maegan answered 2/7, 2019 at 8:9 Comment(2)
So that you do not need to restart your terminal, you can also run 'source ~/.bashrc'Flavoprotein
This is the simplest way to find your home and the .bashrc. Thank you Ayan.Guillot
H
8

1) Start by opening up git-bash.exe in Administrator mode. (Right click the file and select "Run as Administrator", or change settings in Properties → Compatibility → Run this program as administrator.)

2) Run cd ~. It will take you to C:/Users/<Your-Username>.

3) Run vi .bashrc. This will open you up into the editor. Hit INSERT and then start entering the following info:

alias ll="ls -la" # this changes the default ll on git bash to see hidden files.
cd "C:\directory\to\your\work\path\"
ll # this shows your your directory before you even type anything.
Heaney answered 5/2, 2016 at 17:22 Comment(0)
C
8

I think the question here is how to find .bashrc file on Windows.

Since you are using Windows, you can simply use commands like

start .

OR

explorer .

to open the window with the root directory of your Git Bash installation where you'll find the .bashrc file. You may need to create one if it doesn't exist.

You can use Windows tools like Notepad++ to edit the file instead of using Vim in your Bash window.

Catalepsy answered 15/6, 2016 at 0:17 Comment(1)
This is a good alternative Windoze approach should someone wish to edit the file outside of git bash. Really helped me thanks.Richmound
Z
5

Don't need to create a new file, it is already there!

/etc/bash.bashrc
Zaslow answered 6/10, 2019 at 12:37 Comment(3)
but it cannot be used for placing aliases etc.Anfractuous
Exactly what I was looking for!Ciliate
This is THE solution for finding the default bashrc file that guides the Git Bash on WindowsNakano
P
5

go to: C:\Program Files\Git\etc\profile.d inside you find all you need it.
Or in your home directory create .bashrc and .bash_profile.

.bash_profile

# generated by Git for windows
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc

.bashrc

echo "Hello World!"
Pytlik answered 28/12, 2021 at 22:42 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewMyo
H
3

In your home directory, you should edit .bash_profile if you have Git for Windows 2.21.0 or later (as of this writing).

You could direct .bash_profile to just source .bashrc, but if something happens to your .bash_profile, then it will be unclear why your .bashrc is again not working.

I put all my aliases and other environment stuff in .bash_profile, and I also added this line:

echo "Sourcing ~/.bash_profile - this version of Git Bash doesn't use .bashrc"

And THEN, in .bashrc I have

echo "This version of Git Bash doesn't use .bashrc. Use .bash_profile instead"

(Building on @harsel's response. I woulda commented, but I have no points yet.)

Hardhack answered 10/9, 2020 at 16:0 Comment(0)
T
0

for gitbash in windows10 look out for the config file in

/.gitconfig file

Trews answered 26/9, 2019 at 13:45 Comment(1)
Your answer is about git configuration. I think the question is about shell configurationHeeling
B
0

On Windows:

you can find .bashrc in the directory: C:\Users\[YourUserName]

Bomke answered 17/9, 2021 at 20:13 Comment(0)
F
0

Edit this file with Nodepad++: aliases.sh

C:\Program Files\Git\etc\profile.d\aliases.sh

You will see everythings you need.

Fringe answered 2/11, 2022 at 2:18 Comment(0)
Z
0

.bashrc not found, but I was able to configure it to start zsh by making the change in the file bash_profile.sh.

C:\Program Files\Git\etc\profile.d/bash_profile.sh

    # add ~/.bash_profile if needed for executing ~/.bashrc
    if [ -e ~/.bashrc -a ! -e ~/.bash_profile -a ! -e ~/.bash_login -a ! -e ~/.profile ]; then
        printf "\n\033[31mWARNING: Found ~/.bashrc but no ~/.bash_profile, ~/.bash_login or ~/.profile.\033[m\n\n"
        echo "This looks like an incorrect setup."
        echo "A ~/.bash_profile that loads ~/.bashrc will be created for you."
        cat >~/.bash_profile <<-\EOF
            # generated by Git for Windows
            test -f ~/.profile && . ~/.profile
            test -f ~/.bashrc && . ~/.bashrc
        EOF
   fi

   # Launch Zsh
   if [ -t 1 ]; then
       exec zsh
   fi
Zamindar answered 30/3 at 18:21 Comment(0)
J
-6

Sometimes the files are actually located at ~/. These are the steps I took to starting Zsh as the default terminal on Visual Studio Code/Windows 10.

  • cd ~/

  • vim .bashrc

  • Paste the following...

if test -t 1; then exec zsh fi

  • Save/close Vim.

  • Restart the terminal

Jenevajeni answered 8/7, 2017 at 0:4 Comment(2)
I'm not able to open the terminal anymore. While open the terminal it closed automatically.Case
This solution doesn't help if an user doesn't have zsh in his/her machine. @Case If you still have an issue, remove three lines you added from .bashrc file.Shrewsbury
P
-7

If you want to have projects choice list when you open Git Bash:

  • Edit ppath in the code header to your Git projects path, put this code into .bashrc file, and copy it into your $HOME directory (in Windows Vista / Windows 7 it is often C:\Users\$YOU)

.

#!/bin/bash
ppath="/d/-projects/-github"
cd $ppath
unset PROJECTS
PROJECTS+=(".")
i=0

echo
echo -e "projects:\n-------------"

for f in *
do
    if [ -d "$f" ]
    then
        PROJECTS+=("$f")
        echo -e $((++i)) "- \e[1m$f\e[0m"
    fi
done


if [ ${#PROJECTS[@]} -gt 1 ]
then
    echo -ne "\nchoose project: "
    read proj
    case "$proj" in
        [0-`expr ${#PROJECTS[@]} - 1`]) cd "${PROJECTS[proj]}" ;;
        *) echo " wrong choice" ;;
    esac
else
    echo "there is no projects"
fi
unset PROJECTS
  • You may want set this file as executable inside Git Bash, chmod +x .bashrc (but it's probably redundant, since this file is stored on an NTFS filesystem)
Portiaportico answered 26/9, 2014 at 17:29 Comment(1)
This config is completely tangential to the question.Gelignite

© 2022 - 2024 — McMap. All rights reserved.