Git for Windows, and Changing Directories, part 1

Recently I switched my version control software from Bazaar to Git. I did this for a couple of reasons, one of which was I was using Tortoise Bazaar and every time I had to find a file in my Windows Explorer or Zend Studio file explorer, it took forever for Tortoise to synchronize so you could even see the folder to open a file. In all fairness to Bazaar, I think it was more a Tortoise problem than a Bazaar problem.

We looked around for a version control system that was distributed when we first set up the version control about a year ago. Git, at the time, did not have a Windows port and Mercurial was not really ready yet either. We had a mixed Linux, Windows environment. That left Bazaar, which easily out featured CVS and SVN, which are not distributed. This is not a review of either Bazaar, Git, or Mercurial, I want to reserve judgment until I spend some time with Git.

I resolved when I first started posting to this blog that if any problem took me some time or I couldn't find a lot of information about it on the web, I would write a post about it. This is one of those.

Git for Windows is available in two distributions, one uses the Linux environment for Windows provided by Cygwin, and the other uses an environment installed with the Git installation using msysgit. All postings seem to recommend the later choice available here
use the Full Installer for official Git.

During the install check that you want the GUI and the BASH and check the bottom box with the red warning and you'll know what I mean when you get to it. There is some good documentation on Git install for Windows except not many of them tell you which boxes to check on the Windows install. I installed twice before I got it correct.  See my article on "Installing Git on Windows" for a complete walk-through.

Git will install an icon on your desktop. When you click the icon, it will bring up a command window, that command window is a Linux environment. Change to the directory where you'd like to start a repository, type git init, and it will set up a repository for your files. If you'd like all your files in the repository, type "git add ." including the period. but not the quotes followed by "git commit -m "first commit".  You've done your first commit.

You will want to know more, and Git has great documentation, but none better that the book written by Scott Chacon available on Amazon here, and online for free here.  Please buy the book, and read it online, while your waiting for your book to come.

What I wanted to write about when I started this post is a small irritating problem that I experienced. When you open the Git Bash Window, it opens in the same start directory that Windows opens when you open a cmd window in Windows. That's a small problem, because your repository is located in another directory and in the Linux Git Bash window you have to do the "cd .." and "pwd" and "ls" until you arrive at your repository directory. To me that is a minor pain since usually the repository is far and many levels from your starting directory. Every time I use the Git Bash window you have to go through this.  There has to be shortcut.

I searched for a quick solution to this on the Internet and it's not well documented. I thought about setting an environmental variable, or writing a short Linux script to run, when actually there's is a quick way to go back and forth from your start directory to your repository using the tilde ~.

For example, if Windows sets your start directory at "C:\Document and Settings\Dale",  and if you want your working repository to be located at "C:\wamp\www\Joanna"   you can designate the later as your HOME directory in Windows.   This is done in Windows by right clicking on Start>My Computer>properties>Advanced>Environmental Variables.  Under system variables, click on HOME and the Edit to the directory where ever your currently working.  In git bash this sets your ~ character to that directory.

Click the Git Bash icon to open the Linux command window and ...

// Your current directory is your start directory
$ pwd
// the output
$ /c/Document and Settings/Dale

// To go to the repository directory
$ cd ~
// or
$ cd $HOME

//Check the directory
$ pwd
// the output
$ /c/wamp/www/joanna

// to go back
$ cd ~-
// check the directory
$ pwd
$ /c/Document and Settings/Dale
// and your back to your start directory

This makes switching directories a lot easier, however it creates a couple of problems, which I'll talk about in part 2 here.

Comments

Git for Windows, and Changing Directories, part 1 — 5 Comments

  1. This is like a B- movie. Why is there 10,000 words to explain a two sentence concept.

  2. If you assign a new drive letter (e.g. by mounting a true crypt container or adding a network drive), you have to restart git bash, otherwise you are not able to switch to the new drive, e.g. cd /f/ for accesing drive f won’t work. At least for me 😉

  3. My git repository is in C:projectsprojONE.git, when I open GIT bASH, it comes with no current folder name… can some one tell me, on how to change directory to my .git folder?

    Thanks!!