Git for Windows, Changing Directories, part 2

It seems like my first posting on changing directories inside of Git Bash on Windows has some problems.  I'd like to explore this a little more by going through some other choices.

If you change your Windows Home directory as recommended in my first post so that cd ~ will go to  your current working directory, everything will work fine, except this does leave you with a couple of hangovers. First, every time you switch to another project you end up having to switch your Windows Home directory, it would be better to have something like a Windows .bat file to change the directory with a different .bat file for each project.  Nice thought, but Bash is a Linux environment and .bat files are Windows script files. So no dice there.

The second reason that you probably shouldn't change your Windows Home directory is that git stores its config file ~/.gitconfig file in your Home directory.  If your switching home directories back and forth, git could loose track of where your .gitconfig file is, or you will have to duplicate the .gitconfig multiple times for each project.  This is a poor solution as you have to change every .gitconfig file if you change one of them so git will perform the same in every repository.  True, git will run without the .gitconfig file, but you want your mergetool, for example, when you need it.

Those Windows .bat files seem nice, and Linux has what is called shell scripts that are very much like .bat files with one exception, when you run a shell script it runs in its own shell space.  If you say change directory inside this shell, you will change directories inside that shell, but when you exit, you go back to the originating shell, and lo and behold you're right back where you started.  Linux shell scripts will not work in this case.

How about symlinks, or symbolic links?  Symbolic links allow you to assign a name as a reference to another file or directory.  Symlinks are mainly used to manipulate files, not change directories.

Well, what about using an alias. Something like this:  "alias cbc="cd /c/wamp/www/cbc"    Now when you type cbc at the command prompt, you'll go to /c/wamp/www/cbc.   Well, this works fine, and is what we want, except for one problem.  It lives only for your current session of git Bash.  When you exit the window, it blows the alias away.  Aliases need to be loaded every time you open the git Bash window.

It turns out that when git Bash opens,  it looks  for, and loads the .bash_profile file located in your home directory, another reason not to move your home directory around.  When you first install git, you will not find a .bash_profile file.  You have to create it.  Let's do that.

You can not use a Windows editor because of the different line feeds in Linux files versus Windows files.  We'll use the vi editor that comes with git Bash.  Open git Bash.  You should be in your home directory.  It should have the .gitconfig file in it.  Which you can look for with a "ls -al" command.

I'm not sure of the level of reader reading this, so bear with me, and  I'll slow down a touch.  Open the vi editor at the command prompt by typing vi .bash_profile.  When the edited file is saved this will create the .bash_profile file in your home directory.

The vi window opens in command mode, you need to move into insert mode.  Type i and you will enter the editors insert mode.  Now type your alias.  "alias cat="cd /c/wamp/www/cat/rel_0.1.2"   is the syntax for the alias command.  Note the forward slash /, and the c drive is just c not c:  You start with the /.  You can put as many aliases you want, one per line.  If you have a couple of projects, I would make an alias for each project.

When your done typing, you're still in insert mode in the vi editor.  To go back to the command mode to write the file, hit the escape key, and then type :wq that will save the file and quit the editor.  If you make a mistake and want to leave the editor without saving, type :q!, and try again.

There's one gottcha here.  The .bash_profile loads when the git bash command window opens.   After you first complete your .bash_profile file in the vi editor, it's still not loaded.  You have to close down and restart the git bash window before the alias will work.

If you ever forget what your aliases are, just type "alias" at the command prompt, and all your alias will print out.  Try it.

Try your alias out.  If it doesn't take you to the correct directory, go back and edit the .bash_profile file until it does what you want.  To go back to your home directory, type "cd ~".  Type your alias again and go back to your working directory.  Minor problem solved, and more important, your back to one .gitconfig file for your globals.


Git for Windows, Changing Directories, part 2 — 8 Comments

  1. And I wouldn’t use cat” as an alias since it would hide the normal cat command. This is quite useful because it reads files and displays them. try ‘cat .bash_profile’
    And one more thing, the git bash is unix like supporting the tab key for autocompletion.  if you would like to add a file named “Gulli.txt” just type

    git add G
    and it will autocomplete the name or show you the possible choices. This also works for all git commands. Examples:

    git stat
    git com
    git branch         and it will display all possible branches

  2. Just a quick hint, if you click properties on the shortcut to your git bash, there is a “Start in” option that is set to %HOMEDRIVE%%HOMEPATH%. You can change it to your repo directory. If you want to do it for multiple repos, simply create multiple shortcuts

    • Thanks for that tip! I spent 30 minutes following the tutorial and couldn’t get his suggestion to work. 15 Seconds on that screen fixed it!

    • haha, thank you soooo much! Yeah, this tutorial sucks. Splitting it into 2 parts for dramatic effect was a bit overboard

  3. You don’t need to close & restart the terminal window.
    just type
    ‘source .bashprofile’
    while you’re still in the home directory. Then your new aliases will be available.