How to Manage GIT with PowerShell 7 and Posh-GIT

Last Update: Sep 04, 2024 | Published: Mar 05, 2020

SHARE ARTICLE

If you haven’t heard of Posh-GIT, it is a great module for assisting in managing your GIT repositories from within PowerShell. There is so much functionality in GIT that it can be hard, especially in a PowerShell oriented way, to use that functionality effectively. Using Posh-GIT, you are able to interact easily with GIT using the PowerShell shell. Of course, there are a few prerequisites. In this article, it is assumed that you have the GIT client itself installed. You can install the client from here.

Installing Posh-GIT

The preferred way to install Posh-GIT is from the PowerShell Gallery. This method of installation relies on two things which are listed below.

  • Trust installation of packages from the PowerShell Gallery
  • Use a PowerShellGet version greater than or equal to 1.6

To install Posh-GIT, run the following command to install the module from the PowerShell Gallery.

​

​

Getting Started

Posh-GIT integrates GIT and PowerShell by modifying the PowerShell prompt to include GIT status information.

Untitled 61

Additionally, Posh-GIT provides helpful tab completion support for many common GIT commands, branch and remote names, and paths. Using simple commands, it is easy to tab-complete through the available commands which saves time remembering what the exact command may be.

Importing Posh-GIT

After we have installed Posh-GIT, the next steps are to actually import the module. This is as simple as running: Import-Module posh-git. Once the module has imported, provided you are in a GIT repository directory, the PowerShell prompt will change to reflect the current status.

Breaking Down the Prompt

There is a lot that can be displayed in that prompt, so let's break down what the properties are. This is the default configuration, which is configurable. [{HEAD-name} S +A ~B -C !D | +E ~F -G !H W] The breakdown of the above prompt is available from the

GitHub project page

, but highlighted below are a few of the more useful attributes.

HEAD

 This displays the name of the branch and the color indicates the type of branch it is. This is very useful as you will be able to tell, at a glance, what the current status is.
  • Cyan means the branch matches its remote
  • Green means the branch is ahead of its remote (green light to push)
  • Red means the branch is behind its remote
  • Yellow means the branch is both ahead of and behind its remote
ABCD and EFGH These two sets of indicators show both the index and the working directory file statuses. What is most useful here is the ability to know whether changes need to be committed and what has been modified, removed, or added. W The overall status of the working directory, a quick way to know what you should do next in terms of GIT workflows.

Customizing Posh-GIT

Posh-GIT makes it trivially easy to customize your GIT prompt. By modifying the $GitPromptSettings variable, you can customize the prompt to fit your exact needs. As you can see below, there are a large number of settings available. 

Untitled 62

 Remember, to make these settings permanent make sure to put them in your PowerShell profile so that they are set upon startup! Though there are many ways to customize the prompt. A few of the more common ones are below. With all of the GIT and path information shown on a prompt, sometimes it can get quite long. If you pass in a new-line character, as seen in the below code, then you will have the input prompt shown below the PowerShell prompt.
​

​
Untitled 63

Perhaps for readability, it is easier to see the branch and GIT information prior to the path. To do this simply set DefaultPromptWriteStatusFirst to $True.
​

​


Untitled 64



Finally, what if we wanted the entire path to be shown and not abbreviate the path (even if quite long). Simply set DefaultPromptAbbreviateHomeDirectory to $False.
​

​


Untitled 65



Of course, what is all this customization without modifying colors! There are a number of different settings we can use to change up the colors on our prompt. This might be most useful so that we can match the theme that PowerShell is being used in. To change the colors, it is as simple as modifying some of the following settings:
  • DefaultColor
  • BranchColor
  • IndexColor
  • WorkingColor
  • StashColor
  • ErrorColor
Each has a ForegroundColor and BackgroundColor property. By changing these using the available [ConsoleColors], you can make the prompt look however you want.
​

​
To learn more about the available console colors, you can check out the

documentation here

. What if you want to use a custom HTML color though? To make sure it works on both Windows and Linux, make sure to specify the color using a proper HEX code.
​

​
Windows can understand Orange for example but PowerShell on Linux cannot, so it is best to use HEX codes for both.

Extending the GIT Prompt with $GitPromptScriptBlock

Further customization can be done using the $GitPromptScriptBlock variable. You can read up on this customization in-depth

here

. The easiest way to start customizing your prompt is to modify the standard prompt function in your profile. By including the $GitPromptScriptBlock from within, you will be able to decorate around your prompt with whatever additional information helps you work!
​

​

Conclusion

When working with GIT repositories, it helps tremendously to have tools that support easy information gathering. Post-GIT provides a fantastic way to quickly visualize what your GIT status is and makes working with common GIT commands even easier, through tab completion and other enhancements. With an upcoming 1.0 release for Posh-GIT, the module becomes even more closely integrated into PowerShell across platforms.

Related Article:

SHARE ARTICLE