How to setup GIT for Windows using Command Prompt and Posh-Git

On my local environment, I am using:

  • Git for Windows
  • Command Prompt (comes with Windows so no additional install required)
  • Posh-Git (for nice visual prompt)

I have had the above setup for quite some time now. After helping out a colleague with the setup today I have decided to put the instructions in a post so I can quickly reference it later.

Install Git

Everything starts from here. You can download Git for Windows from here if you haven’t already got it installed. Make sure you ticked “Use Git from the Windows Command Prompt”.

git_setup

Setup Command Prompt shortcut

The steps below will provide you with a MUCH more friendly command prompt.

1. Pin Command Prompt to the taskbar.

2. Right click on Command Prompt (the one in the taskbar) and click on Properties. Set the properties to:

Target: %windir%\system32\cmd.exe /K “cd C:\Dev & powershell”
/K = Carries out the command specified by stirng but remains
& = Used to separate multiple commands

This command will 1) Switch into C:\Dev (or any other working folders) and 2) Run powershell.

3. Click on the Options tab. Tick QuikEdit mode in Options. This makes copy and paste operations a lot easier.

Bonus Tip: In order to start cmd.exe in admin mode, you can add the following registry key:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\\Windows\\System32\\cmd.exe"="~ RUNASADMIN"

Install PowerGUI (optional)

You can choose any IDE to work with powershell scripts but this is my personal preference. PowerGUI is a free IDE for powershell scripts that can be downloaded from here.

Install Posh-Git

1. Download the Posh-Git zip file from here. You can install Posh-Git using other methods like git clone or PsGet but my experience says manipulation of a zip file is the easiest way for beginners to understand and follow.

2. Unzip (using 7-Zip) to C:\Dev\posh-git-master. If you don’t have 7-Zip and relied on the Windows “Extract All…”, you might encounter issues with powershell’s execution policy. Instead of messing around with the execution policy, just go download and install 7-Zip from here. It will save you lots of headaches.

It doesn’t matter where you extract the files, I leave it in C:\Dev because that’s where I keep things.

3. In a command prompt, go into the posh-git-master folder and run .\install.ps1. You may or may not need to set the powershell’s execution policy but RemoteSigned at the LocalMachine level should be sufficient.

4. Inspect your newly created profile. If you don’t know where your profile is located. Just run $profile in the command prompt. You profile should contain two lines that read:

# Load posh-git example profile
. 'C:\Dev\posh-git-master\profile.example.ps1'

5. By now you should get the nice prompt that’s integrated with git. command_prompt_posh-git

6. Bonus step. I have talked about including the current date time in the command prompt in a previous post. After installing posh-git, the handy timestamp in the comand prompt would be lost. In order to get that back, I had to edit the file C:\Dev\posh-git-master\profile.example.ps1 by adding Write-Host($(Get-Date)) above the default Write-Host line:

...
Write-Host("")
Write-Host($(Get-Date))
Write-Host($pwd.ProviderPath) -nonewline
...

Setup SSH Key

1. Generate a new SSH key. Again, seemingly simple step but expect unpleasant surprises if your environment is not configured properly. Make sure:

  • C:\Program Files (x86)\Git\bin is in your PATH.
  • HOME = C:\Users\<user name> is added to the system environment variables.
  • C:\Users\<user name>\.ssh folder is manually created if it doesn’t exist already.

Once all the above checks passed, run the command below in the command prompt:

C:\Users\<user name>\.ssh> ssh-keygen -t rsa -C "<email address>"

Tip: If ssh-keygen.exe is not in the path already, you’ll need to look for it in one of the bin sub folders under the git folder.

2. Press <Enter> to accept the default location and file name.

3. Press <Enter> and <Enter> again when prompted to enter a passphrase.

4. Open the public key file (C:\Users\<user name>\.ssh\id_rsa.pub) and copy the content.

5. Add the public key to GitHub / BitBucket.

–oo00oo–
For more info, please refer to the links below:
Better Git with PowerShell

Include timestamp in the command prompt

I find myself working from the command prompt quite often, and I always have the need to capture the screen shots for demonstration or auditing purposes, so it is vital to incorporate the timestamp in the command prompt.

Let me show you how it is done. Open up a command window and type:

PROMPT=$D$S$T$H$H$H$_$P$G

Depending on your locale and system settings, you’ll see something like the following:

16/10/2009 Fri 22:05:59
C:\Windows\System32>

To make the timestamp a permanent feature, simply add it to the environment variables.