Become a Git command line guru

Tags: Git, Powershell, Tools

Git became one of the widely used Source Control System. If you work on a Windows machine you may rely on your IDE or other tools like GitHub Desktop or Atlassian's SourceTree to work with git. But in order to get all the power git offers you need to start using git from the command line. Don't get put off by the fact that git on windows is limited. With the right tools and configurations you'll enjoy it and finally you'll start liking it.
The default command line solution for git is to use Git Bash but we'll use ConEmu with PowerShell Posh Git module. You'll be very pleased how it works.

 

Installation

Git

If you have not installed git yet let’s do it now. Either download it from https://git-scm.com/download or run chocolately:

choco install git.install

You can add –notSilent in the command line in order to have the installation wizard if you want to provide different options.

 

ConEmu

Install ConEmu from https://github.com/Maximus5/ConEmu or run chocolately:

choco install conemu

 

Posh Git

After ConEmu was installed we need to install Posh Git

Run

choco install poshgit

Or go to the web site https://github.com/dahlbyk/posh-git and follow the instructions

Note:

When you install poshgit make sure that you have git installed already and the Environment path for git was set before opening the PowerShell. If for some reasons you don't want to have git path in the Environment variable then add the following to your c:\Users\[UserName]\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 file.

$env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"

Replace ProgramFiles(x86) with ProgramFiles if you have installed git in Program Files.

 

The installation of posh-git will add the following in the Microsoft.PowerShell_profile.ps1

# Load posh-git example profile
. 'C:\tools\poshgit\dahlbyk-posh-git-869d4c5\profile.example.ps1'

 

ConEmu Configuration

Open ConEmu and navigate to a git repository. In my case, I've opened xunit. As you can see from the image the command line has this information: I am on the master branch, 1 file added and two edited.

git-cmd

 

We can add task bar jump list in ConEmu. In this case when I right click on the ConEmu in the task bar it will open a PowerSell console with poshgit at that location.

Click on the icon on the left hand side of the ConEmu and select Settings… and then Startup > Tasks

conemu-menu

By default every item has Taskbar jump lists checked. This means that all the items from the list will appear in Task bar list. We don't need to have all of them, so go though everyone and deselect what you don't need.

The list in the Predefined tasks contains all tabs you can open when creating a new Console. Let’s create one which will open PowerShell with poshgit in C:\Projects:

Click the + button and add the following:

  • Name – let’s name it Projects.
  • Task parameters - a path where you want the console to open, for example: /dir "c:\Projects"
  • Start console: powershell.exe

Click Up to move it to a desired location.

Click Save settings with Shift key pressed because we don’t want to close the dialog yet.

task-configuration

In the next step we'll add our new created Task to the Task bar jump list. Go to the Main > Task bar. At the bottom of the window check Add ConEmu tasks and click Update Now!

Click Save settings and we're done with the ConEmu settings. Now when you right click on Task bar you can open a new ConEmu instance directly into you Git local repository.

 

Git colours

Let's set a few colours in Git. If you use PowerShell colour scheme in ConEmu (I prefer ConEmu scheme) then the text in the default red colour will be difficult to read on the blue background. You fix this by adding the following in the Microsoft.PowerShell_profile.ps1 file.

$global:GitPromptSettings.WorkingForegroundColor    = [ConsoleColor]::DarkGray

 

At this point all the PowerShell configuration is done. But I don't like the default git colours so I will update a few colours for git. This is nothing to do with PowerShell and this changes will apply to Git Bash as well.

git config --global color.status.added "green normal bold"
git config --global color.status.header "cyan normal bold"
git config --global color.status.updated "green normal bold"
git config --global color.status.changed "cyan"
git config --global color.branch.remote "blue normal bold"
git config --global color.branch.current "green normal bold"
git config --global color.branch.local "green dim"

 

git configuration

Store git credentials

An easy way to store git credentials so you don’t have to type them every time is to use Git Credential Manager for Windows, download it from https://github.com/Microsoft/Git-Credential-Manager-for-Windows

 

Compare configuration

Before committing my files I like to compare my local changes with the remote version. I do that using Beyond Compare.

I run the comparison from the command line:

git difftool

And configure git to use Beyond Compare:

git config --global merge.tool bc
git config --global mergetool.bc.path "c:/Program Files/Beyond Compare 4/bcomp.exe"
git config --global difftool.prompt false

And the last step, I don’t want to write difftool every time so let’s create an alias:

git config --global alias.dt difftool

Now it is just enough to type dt and it will run the comparer.

 

Conclusion

As you can see there’s enough work to do in order to have a proper git command line on Windows. But you don’t have to configure everything. The most important thing is ConEmu and poshgit.

Comments

comments powered by Disqus