180

I am developing on a windows machine. The only place I need for linux command line is Git Bash. The problem is: When I open it, I am in the home directory. I have to change the directory to my workspace, like:

cd ../../../d/work_space_for_my_company/project/code_source

Can I wrap this in a .sh file so I don't have to hand-type it anymore? This should be simple but I have zero knowledge about Linux command line. I am really appreciated If you can walk me through how to create that .sh file.

codeforester
  • 28,846
  • 11
  • 78
  • 104
Xi 张熹
  • 8,556
  • 16
  • 48
  • 74
  • 5
    The answers below are right for fixing this permanently, however even without these you shouldn't be handtyping this every time. Git Bash includes a command history which persists between sessions (unlike Windows consoles), so whenever you want to repeat this command, instead of typing it again, just type Ctrl-r and then part of the command, eg. `ctrl-r../d` and the command will come up, and then type enter. In general, invest a few minutes in learning how to use the command history and then you will be using it a lot, as you repeat the various git commands. – Stephen Hosking Jul 05 '16 at 02:11
  • FYI here's the WSL bash alternative `C:\Windows\System32\bash.exe -i -c 'cd /mnt/c/Data; exec "${SHELL:-bash}"'` (or if you use cmder `cmd /c "C:\Windows\System32\bash.exe -i -c 'cd /mnt/c/Data; exec "${SHELL:-bash}"'" -new_console:t:Data`) where `-i` makes the arrow keys work – KCD Apr 02 '18 at 23:23

16 Answers16

376

Here's a more Windows-ish solution: Right click on the Windows shortcut that you use to launch git bash, and click Properties. Change the value of "Start In" to your desired workspace path.

Edit: Also check that the Target value does not include the --cd-to-home option as noted in the comments below.

Chris Stahl
  • 1,329
  • 3
  • 11
  • 16
David Siegal
  • 4,324
  • 2
  • 12
  • 14
  • 13
    the only problem with this solution is that you're not setting the default path for the home directory, the one accessed with `~` – worc Oct 02 '13 at 21:20
  • 68
    This worked for me. In addition, I also had to remove the --cd-to-home option in the startup command. – ChinLoong Aug 24 '15 at 10:36
  • This can work with just one more task to do. As mentioned here, if you have a shortcut icon in taskbar or other, do set the "Start In" to desired path. However, (for me anyway) git bash kept getting "CD"d back to my cygwin home directory whenever launching git shell, even when using the `Git Bash Here` from context menu. Found it annoying, so (for me) the fix was to add `cd -` to after the line of code near the bottom of `C:\Program Files(x86)\Git\etc\profile` that `sources` ~/.bashrc. Look at # read user-specific settings, possibly overriding anything above – Adam T Nov 04 '15 at 15:56
  • 2
    In case you have the shortcut pinned on Windows 7 taskbar, you'll need to right click twice on the program as suggested by http://lifehacker.com/5475752/customize-pinned-shortcuts-on-the-windows-7-taskbar – manat Nov 24 '15 at 06:37
  • 11
    Agreed that this is the most straight-forward solution, but absolutely should be amended to include removing the "--cd-to-home" in the target. Without that last change, you'll always default to the C:\Users\[name] folder (unless you've modified your setup that is). – Zeus56 Mar 14 '16 at 14:36
  • don't work when started from taskbar icon, works only when started from desktop icon – Shirish Herwade Jul 22 '16 at 12:17
  • 2
    To clarify, in the shortcut the "Target" should be `%WINDIR%\System32\bash.exe` and the "Start In" should be `C:\Temp` or whatever path you want. – demoncodemonkey Nov 10 '16 at 11:53
  • Is there any other way to access git bash properties? It's not in the icon's context menu on my system. – dinosaur May 04 '17 at 18:50
101

Just write that line to a file "cd.sh", then do this from your shell prompt:

. ./cd.sh

Or you can create an alias or function in your $HOME/.bashrc file:

foo() { cd /d/work_space_for_my_company/project/code_source ; }

If the directory name includes spaces or other shell metacharacters, you'll need quotation marks; it won't hurt to add them even if they're not necessary:

foo() { cd "/d/Work Space/project/code_source" ; }

(Note that I've omitted the ../../..; you don't need it.)

EDIT: If you add a line

foo

to your .bashrc after the function definition, your shell will start in that directory. Or you can just use the cd command directly in your .bashrc if you aren't going to need to use the function later.

(The name foo is just an example; you should pick a more meaningful name.)

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
  • 5
    As @orip points out, you might as well just do the `cd` in your .bashrc. But if you make it a function, you can use the command again later, after you've changed to another directory. – Keith Thompson Aug 10 '11 at 20:17
  • 2
    To explain the "../../..", the command you gave specifies a path relative to your current directory; `"../../../d/work_space_for_my_company/project/code_source"` goes up three levels from your current directory (which happens to be `$HOME`), then down to "d", then down to "work...". The git bash shell environment has `"/d"` referring to what Windows calls `"D:\"`. Using an absolute path name, `"/d/work_space_for_my_company/project/code_source"`, is simpler and doesn't depend on where your `$HOME` happens to be. – Keith Thompson Aug 10 '11 at 20:42
  • 2
    Where can I find this `.bashrc` file? – ptamzz Feb 23 '12 at 20:21
  • 2
    @ptamzz: Normally a `.bashrc` file will be created in your home directory when your account is set up. If not, you can create it yourself. – Keith Thompson Feb 23 '12 at 20:22
  • 1
    Where do you find "cd.sh"? And when you say home directory. Are you talking about the home dir for the current app or ?? – WowBow Apr 16 '12 at 23:30
  • @WowBow: My suggestion was to *create* a file called `cd.sh`, but actually defining a function in your `$HOME/.bashrc` makes more sense. The home directory is the *user's* home directory; the variable `$HOME` expands to it in the git/bash shell. – Keith Thompson Apr 17 '12 at 04:07
  • 1
    THIS is exactly what I wanted, thanks! I created a .bashrc file, added a function that does a cd straight to my project. Now I type the name of my project and it takes me there. Thank you! – Nathan Strutz Jun 15 '12 at 18:56
  • exactly what I was looking for! Thanks bro! – Varun Oct 17 '13 at 14:25
  • I did this. created a .bashrc in the default user directory for git bash. when I type foo i get sh.exe": wf: command not found. And ideas? – Norman Bird Jul 29 '14 at 23:59
  • @NormanBird: What is `wf`? Check the line endings on your `.bashrc` file. What does `file .bashrc` say? – Keith Thompson Jul 30 '14 at 00:06
  • @KeithThompson I used wf instead of foo. But It was a syntax error. I had to wrap the path in quotes due to a space in the user name. So I got it to work. works pretty good, now when I start bash, it goes to my new folder. Thanks. – Norman Bird Jul 30 '14 at 04:07
  • Excellent. This was starting to really get on my nerves. – timbrown Feb 16 '15 at 07:30
  • Very nice. Remember to restart Git Bash after editing .bashrc – Mateng Dec 07 '16 at 10:43
  • As @Mateng said, you need to restart Git Bash for the new function to be available. Alternatively, instead of restarting Git Bash each time you make a change to your `.bashrc file`, run the command `source .bashrc` to pick up the changes. – Asencion Feb 07 '17 at 01:11
  • I put `cd /C/Workspace` directly into my .bashrc. Defining a function to change directory & then invoking that just complicates things. – Thomas W Aug 21 '17 at 23:28
  • @ThomasW: If you want to `cd` to a specified directory on startup, you're likely to want to do so again later. Wrapping the `cd` command in a function (hopefully with a better name than `foo`) makes it easy to do that. – Keith Thompson Aug 22 '17 at 14:36
  • @ThomasW: I've updated the answer to mention using the `cd` command directly in `.bashrc`. – Keith Thompson Aug 25 '17 at 15:51
  • remove the --cd-to-home option in the startup command (Shortcut Properties) – telebog Nov 28 '17 at 12:24
  • I keep getting: `You must type a file name` every time I try creating this `.bashrc` – Yokhen Dec 04 '18 at 15:23
  • @Yokhen: I don't think your problem has anything to do with this question/answer. Can you create other files? You might post a question (with more details) in [su]. – Keith Thompson Dec 04 '18 at 20:53
  • Great; you saved so many thousands of seconds in my life, from now on! – Kamafeather Feb 12 '20 at 05:08
45

Add the line to the .bashrc file in the home directory (create the file if it doesn't exist):

cd ~
touch .bashrc
echo "cd ~/Desktop/repos/" >> .bashrc
Web_Designer
  • 64,966
  • 87
  • 197
  • 254
orip
  • 66,082
  • 20
  • 111
  • 144
  • 5
    I know I'm super late to the party, but thank you for stopping the function nonsense! :) – Felipe Gerard Mar 21 '17 at 17:07
  • 1
    This was perfect. Went to my ```.bashrc.local``` and added ```cd ~/Desktop/repos/;``` to the bottom. Worked like a charm! changing the bottom of ```.bashrc``` would also work. – Michael Dimmitt Jul 23 '17 at 17:58
  • I just created the .bashrc file at ~ and the next time I opened git Bash it complained that I don't have .bash_profile and another file so it created them for me. And after reopening git Bash it worked flowerless ! – Pini Cheyni Nov 01 '17 at 06:29
  • remove the --cd-to-home option in the startup command (Shortcut Properties) – telebog Nov 28 '17 at 12:24
  • I went to the `.bashrc` file location, open it and change the (whatever) path I wanted to open git bash by default. I made the change, for example, to `cd "E:\first-folder\second-one\last-one-with-many-files"` and worked (after following some of the instructions above and screwing it up at some point). – carloswm85 Dec 19 '20 at 21:46
38

I use ConEmu (strongly recommended on Windows) where I have a task for starting Git Bash like

enter image description here

Note the button "Startup dir..." in the bottom. It adds a -new_console:d:<path> to the startup command of the Git Bash. Make it point to wherever you like

Maximus
  • 10,488
  • 8
  • 42
  • 63
Juri
  • 30,146
  • 17
  • 97
  • 132
  • Thanks for sharing the custom task, I couldn't get it to work. How can I run simple windows commands after that, like pinging Google.com for instance? – Iman Mohamadi Oct 14 '14 at 13:48
  • 1
    @ImanMohamadi For things like that I'd rather create scripts with you add to your PATH env variable s.t. u can exec cmds like `p-g` for pinging google etc... – Juri Oct 14 '14 at 20:16
23

This may help you.

image description

  1. Right click on git bash -> properties
  2. In Shorcut tab -> Start in field -> enter your user defined path
  3. Make sure the Target field does not include --go-to-home or it will continue to start in the directory specified in your HOME variable

Thats it.

Martin Nyolt
  • 3,706
  • 2
  • 22
  • 33
Krish
  • 674
  • 7
  • 15
  • I tried that but it does not work when the path is in another drive like d:\projects\test – Ray Jan 04 '17 at 00:49
  • Windows 10 doesn't show that tab for git bash , But you can hack it using this small 3 lines of code , save it in some **.bat file `1. d: 2. cd data 3.git bash` if you double click the bat file then it will open in desired location – Krish Jan 05 '17 at 23:19
  • another solution is, `start in` option as shown in above picture is available in cmd properties, you can change there, when it is invoked directly ran the git bash command. – Krish Jan 05 '17 at 23:24
  • 1
    Look that `Target` field, it do not have parameters. By default contains `-go-to-home` which must erase from there to that works. – Kurapika Jun 09 '17 at 14:42
  • 1
    It works for me Target: "C:\Program Files\Git\git-bash.exe" ( IMPORTANT ) Start in: d:/xampp/htdocs/ – raftaar1191 Sep 13 '17 at 13:47
  • 1
    It works for me too with the same target than @raftaar1191 (I removed the "--cd-to-home" option), and I configured the "start in" line to "C:\wamp\www", very useful ! – nayfun Mar 08 '18 at 12:40
10

I also just changed the "Start in" setting of the shortcut icon to: %HOMEDRIVE%/xampp/htdocs/

Andro Selva
  • 51,960
  • 51
  • 189
  • 237
Francis
  • 101
  • 1
  • 2
9

(Please read warning below)

Really simple way to do this in Windows (works with git bash, possibly others) is to create an environmental variable called HOME that points to your desired home directory.

  1. Right click on my computer, and choose properties
  2. Choose advanced system settings (location varies by Windows version)
  3. Within system properties, choose the advanced tab
  4. On the advanced tab, choose Environmental Variables (bottom button)
  5. Under "system variable" check to see if you already have a variable called HOME. If so, edit that variable by highlighting the variable name and clicking edit. Make the new variable name the desired path.
  6. If HOME does not already exist, click "new" under system variables and create a new variable called HOME whose value is desired path.

Environmental Variable

NOTE: This may change the way other things work. For example, for me it changes where my .ssh config files live. In my case, I wanted my home to be U:\, because that's my main place that I put project work and application settings (i.e. it really is my "home" directory).

EDIT June 23, 2017: This answer continues to get occasional upvotes, and I want to warn people that although this may "work", I agree with @AnthonyRaymond that it's not recommended. This is more of a temporary fix or a fix if you don't care if other things break. Changing your home won't cause active damage (like deleting your hard drive) but it's likely to cause insidious annoyances later. When you do start to have annoying problems down the road, you probably won't remember this change... so you're likely to be scratching your head later on!

geneorama
  • 3,057
  • 4
  • 24
  • 33
  • I don't understand why the edits suggested by @asalle (mainly suggesting that you could also edit the user system variables) were rejected Bamsworld fish_ball and greg-449, the edits seemed reasonable. – geneorama Feb 19 '16 at 20:27
  • This is a way too much destructive way to be a good idea. It can affect a lot of other applications and even windows. It's not recommended – Anthony Raymond Jul 24 '16 at 07:58
  • @AnthonyRaymond As I noted changing "HOME" can have other consequences, however I wouldn't say it's necessarily destructive. In fact, it could be beneficial if you want your HOME to be a specific location or if another application has set HOME to an undesired location. In any event, I think this answer sheds some light on how the default directory is set. – geneorama Jul 26 '16 at 16:01
  • @AnthonyRaymond although I agree, that in the context of this question setting home to a particular project (e.g. `../../../d/work_space_for_my_company/project/code_source`) is a bad idea. Generally speaking though, there are times where HOME doesn't need to be `/c/Users/[current user name]/` – geneorama Jul 26 '16 at 16:06
  • If you want a special folder to point to another location create symlink. https://technet.microsoft.com/en-us/library/cc753194(v=ws.11).aspx We have no way to know which program use the HOME variable, si i feel like it is a bit destructive. – Anthony Raymond Jul 26 '16 at 16:09
9

This will do it assuming you want this to happen each time you open the command line:

echo cd ../../../d/work_space_for_my_company/project/code_source >> ~/.bashrc

Now when you open the shell it will move up three directories from home and change to code_source.

This code simply appends the line "cd ../../../d/work_space_for_my_company/project/code_source" to a file named ".bashrc". The ">>" creates a file if it does not exist and then appends. The .bashrc file is useful for running commands at start-up/log-in time (i.e. loading modules etc.)

Usagi
  • 2,678
  • 2
  • 24
  • 35
7

Right-click the Git Bash application link go to Properties and modify the Start in location to be the location you want it to start from.

Michael Stramel
  • 1,153
  • 1
  • 13
  • 18
5

From a Pinned Start Menu Item in Windows 10

  1. Open the file location of the pinned shortcut
  2. Open the shortcut properties
    1. Remove --cd-to-home arg
    2. Update Start in path
  3. Re-pin to start menu via recently added

open file location screenshot

open shortcut properites screenshot

update shortcut properites screenshot

pin via recently added screnshot


Thanks to all the other answers for how to do this! Wanted to provide Win 10 instructions...

spottedmahn
  • 11,379
  • 7
  • 75
  • 144
4

For windows: Follow these steps-

  1. Go to windows home> Right click on "Git Bash" application.
  2. Properties> Shortcut
  3. Change these two settings: (a) Delete --cd-to-home from target (b) Type folder path you want to start with git in "Start in".

This worked for me:)

Rohit Poudel
  • 1,604
  • 2
  • 16
  • 19
3

If you want to have projects choice list when u open GIT bash:

  • edit ppath in code header to your git projects path, put this code into .bashrc file and copy it into your $HOME dir (in Win Vista / 7 it is usually 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 its probably redundant, since this file is stored on ntfs filesystem)
s1w_
  • 134
  • 6
2

If you type this command: echo cd d:/some/path >> ~/.bashrc

Appends the line cd d:/some/path to .bashrc. The >> creates a file if it doesn’t exist and then appends.

mtpultz
  • 13,197
  • 18
  • 96
  • 180
2

My Git Bash shortcut on Windows complained when I put the cd to my work directory into ~/.bashrc

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.

So git created this .bash_profile:

$ cat ~/.bash_profile
# generated by Git for Windows
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc

Which does the job.

Alternatively, you can just remove the .bashrc again and put the cd command into .bash_profile:

$ rm ~/.bashrc
$ echo "cd Source/Repos" >~/.bash_profile

$ cat ~/.bash_profile
cd Source/Repos

Once this is done you can close the Window and re-open it using your desktop shortcut and the prompt will tell you that your location is now where you wanted it - looks like this is my case:

Administrator@raptor1 MINGW64 ~/Source/Repos
$
dr. rAI
  • 1,455
  • 1
  • 9
  • 14
1

Another solution for Windows users will be to copy the Git Bash.lnk file to the directory you need to start from and launch it from there.

skip405
  • 5,627
  • 2
  • 23
  • 26
1

it must be cd d:/work_space_for_....

without the : it doesn't work for me

Aliaksei Kliuchnikau
  • 13,099
  • 4
  • 52
  • 66
larsbo
  • 121
  • 1
  • 6