How do I change the default location for Git Bash on Windows?
Asked Answered
S

28

308

I am using Git on Windows 7 and access my repositories through Git Bash. How can I change the default location that Git Bash opens in a convenient folder when I start it?

It's somewhat time consuming to navigate to htdocs, and then a specific folder. Is there a way to change the configuration file to have it open elsewhere? Or would it be possible to write a .sh file to do this?

Unfortunately Git Bash won't open my htdocs folder shortcut on my desktop, and it takes using cd 5 times to get to my desired directory.

Siler answered 6/10, 2011 at 7:43 Comment(1)
Related: How to change folder with git bash?Plateau
S
251

After installing msysgit I have the Git Bash here option in the context menu in Windows Explorer. So I just simply navigate to the directory and then open Bash right there.

I also copied the default Git Bash shortcut to the desktop and edited its Start in property to point to my project directory. It works flawlessly.

Windows 7x64, msysgit.

Solitude answered 6/10, 2011 at 7:49 Comment(12)
I think the Start in part is what he's most interested in - if you're using the windows shell (cmd.exe), you can also turn on QuickEdit mode through the same properties dialog to make copy/paste a bit easier and set the window size/buffer/font properties and such...Far
I don't have this Git Bash Here option. How do I do it then?Sauceda
@Sauceda "I also copied default Git Bash shortcut to desktop and edited its Start in property to point to my project directory."Nimesh
To get to Git Bash's Start in property, Right-Click your Git Bask shortcut icon and click "Properties". Then find and paste in the Start in field the path you'd like to start in. It will look something like: C:\Users\DM\Desktop\YourFolderNauseate
I went the way Mikeumus says editing the start in property. I then started gitbash and pinned it in taskbar. It now opens to the desired location from the taskbar too.Hames
@Nauseate Start In Doesn't seem to do it for me, it keeps starting in users/user am i missing something else here?Asbury
Right cd-to-home was overriding my start in value. all sorted now.Asbury
For Start in value, I use %USERPROFILE%/Git where Git is a directory under my home directory e.g. C:\users\rami\GitMarchelle
One thing that has been skipped in this answer is that you should remove the --cd-to-home part at the end of Target field value.Wayland
will this work if my directory not from C:\ but from D:\ Do I need to restart system after changing the startIn path, I have removed --cd-to-home from target but still not opening git bash from my pathBerdichev
Every time you update Git Bash a new shortcut will be automatically created and you have to repeat this process from scratch. To avoid this, see @smilyface's answer.Assistance
Yes, it works, but ONLY if you remove --cd-to-home like mentioned in the solution below which should get way more credit for that important hint in my opinion...Scissors
P
281

The easiest way without installing msysgit is right click on the Git Bash shortcut icon → Start in: → "C:\Program Files (x86)".

Change the Start in entry and point out the Git Bash starting position. If you don't remove the --cd-to-home part from the Target box, the Start in change gets overridden.

Alternate Option:

If you don't have the shortcut and installed in the default location, open up a file explorer and paste in the search path field:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git

That will take you to the Git shortcuts location

  • Right click on 'Git Bash'
  • Click 'Properties'
  • Navigate to 'Shortcut' pivot
  • As others have said, in Target remove: --cd-to-home
  • Update Start in row to C:\Program Files (x86) or for me it was
    • C:\Users\<username>\Desktop\Sites
  • Apply as Administrator

Good to go

Pugilist answered 18/5, 2012 at 11:56 Comment(7)
I found yours and Marcin's solution were the same, but for me, your instructions were more precise and to the point. Thanks!Beezer
Thanks for the tip to remove the --cd-to-home argument.Cenotaph
@ShirishHerwade I had to unpin from taskbar and pin it again to make it work.Geanticlinal
Still the most helpful solution in 2020Norean
"Change the Start in entry and point out the Git Bash starting position." What does that mean? Can someone provide an example value to use?Dowsabel
The accepted answer should have an edit to remove that --cd-to-home from the target part.Pulsometer
this did not work for me in the pinned git bash unless, only when the result was from a Windows search for git bash. I tried to unpin and pin it again and it was still not working. I needed to use the .bashrc solution below.Conjugated
S
251

After installing msysgit I have the Git Bash here option in the context menu in Windows Explorer. So I just simply navigate to the directory and then open Bash right there.

I also copied the default Git Bash shortcut to the desktop and edited its Start in property to point to my project directory. It works flawlessly.

Windows 7x64, msysgit.

Solitude answered 6/10, 2011 at 7:49 Comment(12)
I think the Start in part is what he's most interested in - if you're using the windows shell (cmd.exe), you can also turn on QuickEdit mode through the same properties dialog to make copy/paste a bit easier and set the window size/buffer/font properties and such...Far
I don't have this Git Bash Here option. How do I do it then?Sauceda
@Sauceda "I also copied default Git Bash shortcut to desktop and edited its Start in property to point to my project directory."Nimesh
To get to Git Bash's Start in property, Right-Click your Git Bask shortcut icon and click "Properties". Then find and paste in the Start in field the path you'd like to start in. It will look something like: C:\Users\DM\Desktop\YourFolderNauseate
I went the way Mikeumus says editing the start in property. I then started gitbash and pinned it in taskbar. It now opens to the desired location from the taskbar too.Hames
@Nauseate Start In Doesn't seem to do it for me, it keeps starting in users/user am i missing something else here?Asbury
Right cd-to-home was overriding my start in value. all sorted now.Asbury
For Start in value, I use %USERPROFILE%/Git where Git is a directory under my home directory e.g. C:\users\rami\GitMarchelle
One thing that has been skipped in this answer is that you should remove the --cd-to-home part at the end of Target field value.Wayland
will this work if my directory not from C:\ but from D:\ Do I need to restart system after changing the startIn path, I have removed --cd-to-home from target but still not opening git bash from my pathBerdichev
Every time you update Git Bash a new shortcut will be automatically created and you have to repeat this process from scratch. To avoid this, see @smilyface's answer.Assistance
Yes, it works, but ONLY if you remove --cd-to-home like mentioned in the solution below which should get way more credit for that important hint in my opinion...Scissors
V
190

Add this line to your .bashrc file:

cd C:/xampp/htdocs/<name of your project>;

If the .bashrc file doesn't exist, create one in your root folder. For me it is: C:\Users\tapas\

Save .bashrc and open Git Bash. That's it!

Verse answered 21/11, 2014 at 7:0 Comment(11)
This is the correct answer! And if .bashrc was not there, just create it in ~ (cd ~ && touch .bashrc)Cablet
This .bashrc approach is a handy solution if you want to put the console into /c/whatever and can't put /c/whatever into the Start in properties setting as other answers suggested. The /c/whatever path only seems to work in newer versions of gitbash.Bath
I found bash.bashrc in Git\etc folderDichromatic
I found this solution good but it doesn't play well with custom shortcuts or the Git Bash here options described by other answers. I solved it by adding a conditional so it only runs when in the home folder. if [[ $(pwd) = $HOME ]]; then cd <location> fiSayre
Thanks @Forss! That wrapper is a crazy good idea. Don't forget the semicolon after the location!Ical
Thanks @peter-mortensen &soundswaste, helped me tremendously. I just added the that line at the very end of the brashrc file, worked great!Endosmosis
Note: You may need to use vi in Git Bash itself to create the file, Windows was getting annoyed that I tried to rename a file with no "name", just an "extension"Strength
Forss's tip is great, but has a syntax error, as pointed out by @Travis Watson. It should be if [[ $(pwd) = $HOME ]]; then cd <location>; fiPetree
Good solution but if you're using Git Bash as terminal for VS Code now when opening terminal in VS code it wont default to the project's repo anymoreUni
this was the cleanest solutionWaive
In windows, I had to add this in ~/.bash_profileUnderbodice
U
30

I read it somewhere and it worked for me.

First check in git bash what is the HOME location. Open git bash and run

echo $HOME

Now change the HOME path by opening cmd and run

setx HOME "path/to/.ssh/loc" (I gave C:\Users\aXXXX)

Now cross check by running the echo command in git bash.

Unholy answered 5/6, 2018 at 9:28 Comment(6)
I think this is a very good answer, especially if your company has a network mapped home directory which you do not want to use (ie. for performance reasons)Pule
will this HOME path changing will affect only git bash or total system command prompt?Berdichev
Excellent solution, fixed a severe slowness I had with Git Bash with my P: drive on a slow remote drive over VPN. I find it strange, however, how I can use this solution for HOME but not for HOMEDRIVE and HOMEPATH - I cannot change where cmd launches with this solution. But no matter, Git Bash is what was affected and it's now working properly again.Enumeration
Note that this solution also changes where Git Bash looks for your dotfiles, which solved my problem, but might not be the right solution for other situations, and probably would not have been what OP wanted.Enumeration
This solution broke Git-Bash for me, I can no longer open the programOrland
You might have to specify the new path as "/c/YourNewFolder/" instead of "C:\YourNewFolder"Neutrino
V
26

Windows 10 tested solution

  • Type git bash in search

  • Right click on it and choose open file location

  • Right click on it and choose properties

  • At target remove --cd-to-home from "C:\Program Files\Git\git-bash.exe" --cd-to-home

  • At start in put the path of the directory you want it to start at for example Start in: C:\xampp\htdocs

Von answered 6/6, 2019 at 13:41 Comment(1)
I did this but still same as previous, will this work if start in was in the local disk D:\ any info.Berdichev
L
24

Open the properties of the bash and change to "Start in" to where you want to start in. After this, (I had this problem and I solved it by doing this) go to target and delete the --cd-to-home parameter part after "C:\Program Files\Git\git-bash.exe" . Only THEN will you start in the "Start in" directory that you specified.

Longlegged answered 15/10, 2015 at 17:8 Comment(2)
Vladimir, deleting the --cd-to-home parameter part after "C:\Program Files\Git\git-bash.exe" is an important detail. Thanks.Devy
Alert!..life saver :)Hartsfield
A
18

If you're like me, and the way you get to Git is windows key + G + I + Enter, then you can simply hit the windows key, search for Git, right click on the resulting Git Bash icon, select properties, and change the Start in: field.

I'm not exactly sure what this does, because I'm not exactly sure what Git on Windows is (a facade on top of sh which itself is a facade on cmd?), but in any case, you can now pin the shortcut that Windows pulls up on a search for "Git" to your taskbar, or to your desktop, and it will still start in whatever directory you set it to.

Arrestment answered 2/4, 2012 at 2:10 Comment(1)
So that is why it did't work. I forgot to update the pinned shortcut in my taskbar. Your "now pin (...)" did ring the bell :)Loma
S
13

1/ Go to Properties of Git Bash

2/ Change Start in: to your the directory of your choi

3/ Delete --cd-to-home

enter image description here

4/ Restart Git Bash

Selectee answered 22/5, 2021 at 14:43 Comment(0)
F
10

Right click on Git Bash shortcutand then go to properties.
In properties inside start in option add the location of the directory you want to start Git Bash in and apply the changes.

Forgiving answered 15/6, 2017 at 7:6 Comment(0)
M
9

I am using Git bash on Windows 10, here is my solution:

  1. Close all git bash sessions
  2. Hit windows key and type: env; then click Edit environment variables for your account (control panel)
  3. Under "User variables for ...."; hit New button
  4. Variable Name: HOME
  5. Variable value: path where you would like ~/ to be in git bash

Open a git bash session and test it by typing: pwd and double check by doing cd ~/ && pwd

Monoicous answered 21/3, 2018 at 13:21 Comment(1)
Some versions of Git use HOMEPATH instead of HOME. If you check the start in folder for Git Bash you can see which environment variable is used.Selfreproach
R
9

Git bash is used to play with bash commands. So I use bash method itself in windows Git bash

Edit your .bash_profile (create it if you doesnot have one - see below how to create). Enter the following lines.

Add these lines to the file

alias ws="cd /d/workspace/"
ws

My .bash_profile looks like this

Smilyface@SmilingMachine /d/workspace
$ cat ~/.bash_profile
alias ws="cd /d/workspace/"
ws

How to create a new .bash_profile ?

touch ~/.bash_profile

OR write into the file directly

vi ~/.bash_profile 

Simple, aha !

Ready answered 6/6, 2019 at 14:7 Comment(1)
precise and clear, of course ~/.bashrc file would be okay too.Augustus
E
6

Once you have updated the Start in: field make sure to remove the --cd-to-home at the end of the Target: field

Erda answered 12/4, 2017 at 15:6 Comment(0)
M
6

If using Windows OS then add target like above :

  1. Right click on git terminal > Properties

Step > one

  1. Properties>Under shortcut tab>Start in: add your folder target path like below image

Step > two

Mensal answered 13/5, 2021 at 8:57 Comment(0)
C
5

Make a Git Bash shortcut to Desktop for convenience then right click on the icon goto properties. Here you will find the Start in: section with a text box. Replace the path you want, for example like:

%USERPROFILE%\Desktop

Then open it directly by clicking on the icon. You will get the default Desktop path in Git Bash.

Crinkumcrankum answered 4/9, 2014 at 10:40 Comment(1)
Ah! I also needed to remove the --cd-to-home option.Guidry
S
5

Open this file:

C:\Program Files\Git\etc\bash.bashrc

And append the following line:

cd /c/Users/<User>/Documents/path/to/your/repos

Restart Git bash

Seeley answered 6/2, 2021 at 18:50 Comment(0)
C
5

If you're using Git Bash through Windows Terminal, you can set the starting directory (of any profile).

In Windows Terminal, go to Settings > Open JSON file.
In this file, in profiles > list, you can set the startingDirectory property (backslashes need to be escaped).

See the documentation.

open settings screenshot

JSON files screenshot

Caulis answered 4/8, 2021 at 8:9 Comment(0)
A
4

I liked Peter Mortenson's answer, but I would like to expand.

'cd ~' in the .bashrc file causes the "Git Bash Here" feature of Git Bash to stop working. Instead, add this if statement to the .bashrc file:

if [ "$PWD" == '/' ]
then
        cd ~
fi

This will change to the home directory when Git Bash is run on its own, but when "Git Bash Here" is run, the current working directory will not be changed.

Algor answered 28/10, 2019 at 22:38 Comment(1)
I like this idea but it doesn't actually work as written because $PWD contains '/c/Users/<my name>/', not '/'. Git bash starts in ~. I don't see Peter's answer though, so maybe I'm basing this on a totally different solution.Windermere
Z
3

the only way that worked for me on windows 10:

  1. right-click gitbash shortcut -> proporties
  2. change target as: "C:\Program Files\Git\git-bash.exe" --cd=C:\Users\test\Desktop\

This will always open desktop path

Zamindar answered 29/10, 2020 at 8:50 Comment(0)
N
3

Add a line to change to the desired directory in your .bashrc file or create a .bashrc if you don't have one.

I.e. add cd C:/path/to/yourfolder to your .bashrc file

Be mindful that the path format is forward slash '/' Unix style and not Windows '\' backslash.

One way to do this is to open bash and edit it using vim

  1. Open up Git Bash
  2. Run vim ~/.bashrc
  3. Press i in order to insert characters
  4. Type cd C:/path/to/folder; (make sure you use forward slashes)
  5. Press esc to exit the insert mode
  6. Press :wq or ZZ to save and exit vim

I found this solution on https://simplernerd.com/cli-git-bash-default/

Nance answered 9/3, 2021 at 17:53 Comment(0)
M
2

Just type the path of your local directory (Git project home directory) in the properties of Git Bash. I.e. set path C:\yourprojsctdirectory to Git Bash's properties field "Execute In" or (Ausführen in). That's it!

Now double click Git Bash. The Git header will be on your "yourprojsctdirectory".

Marchese answered 5/11, 2014 at 10:27 Comment(0)
D
2

The working solution listed are great, but the problem occurs when you want multiple default home for your git-bash.

A simple workaround is to start git-bash using bat script.

git-bash-to-htdocs.bat

cd C:\xampp\htdocs
"C:\Program Files\Git\git-bash.exe" 

The above of course assume git-bash is installed at C:\Program Files\Git\git-bash.exe

You can create multiple .bat file so your git-bash can start where it want to be

Danedanegeld answered 8/10, 2019 at 9:5 Comment(0)
H
1

Add "cd your_repos_path" to your Git profile, which is under the %.

Humphries answered 15/11, 2013 at 3:1 Comment(0)
K
1

As from this link

lets say you want your change to default location to c:\myprojects

Open git bash

type in:

echo "cd 'C:\myprojects'" >> ~/.bash_profile

Press Enter

Close Git bash and open again

You should start from c:\myprojects

Tested on git version 2.36.1 on windows

Troubleshoot:

  1. Make sure you type in the folder inside the '' and the entire cd command inside "" just like the example
  2. make sure folder exist
Kepi answered 29/5, 2022 at 6:39 Comment(0)
M
0

I tried the following; it helped me. I hope it help you also.

cd /c/xampp/your-project
Murtagh answered 10/1, 2017 at 12:24 Comment(1)
@MuhammadSohailiqbal - The question is how to set it in default. So that whenever he opens the Git Bash, it should directly go to that directory.Ready
O
0

Since I'm using git-bash inside of vscode as a terminal I don't always want to cd into a specific directory, for this to work I checked if my current directory(pwd) is / and if so it should only then cd to ~. So whenever I open the terminal inside vscode git-bash's current working directory is my opened folder inside of vscode and it doesn't change to ~ since it only does that when my current working directory is /.

For this to work I added following if condition into ~/.bashrc:

if [ $(pwd) = '/' ] then cd ~; fi

TIP: make sure your ~/.bash_profile actually loads ~/.bashrc by a line like the following:

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
Organist answered 30/11, 2021 at 10:19 Comment(0)
G
0

You can create a .bashrc configuration file:

cd ~ && touch .bashrc && echo "cd /folder1/folder2/folder3"

Change cd command to the directory you want.

Gummous answered 30/9, 2022 at 22:28 Comment(0)
C
0

On Windows 11, create a file named .bashrc in your user directory, like C:/user/.bashrc, and add this single line to the file: cd ~ [enter the full path of where you want git bash to start up]. It took a while, but I found out that ~ is the 'wildcard' option, so you could type something specific like cd ~/MyProject and always start there.

Crosspurpose answered 5/11, 2023 at 21:44 Comment(0)
F
0

On Windows 11 if you pined gitbash to TaskBar then it loosing "--cd-to-home" flag. So it has to be added there manually again. It can be done via right click on the pinned icon and then again right click on gitbash icon. Then "properties".

Faker answered 18/2 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.