Git in PowerShell
Get Productive Results (in 2021)
This guide will teach you everything you need to know about Git in PowerShell.
Advanced strategies.
Cool features.
PowerShell Powerline.
Productivity hacks & more with Git in PowerShell.
Let’s get started…
Download
Git in PowerShell
Git in PowerShell is a Long Guide. So, I've created a downloadable pdf.
Let's start ...
Chapter 1
Git in PowerShell Fundamental
What is Git in PowerShell?
Git is robust source control.
The best part?
You can use Git in other environments like PowerShell.
Also, you can use Git if you are running PowerShell core on Linux.
Do you remember cmd.exe?
(windows command line)
The cmd is not capable of a customized Git experience.
And if you are familiar with PowerShell, you are ahead of the game to use Git in PowerShell.
You can also use Git on the Windows Terminal.
Windows terminal is a modern, efficient & productive terminal application.
Git inside Windows Terminal (Git in PowerShell) will boost your productivity.
Here how Git in PowerShell will look like. (more in Chapter 4)
More details with you later in this post.
Keep reading.
What is Windows Terminal
Windows Terminal is open source, a multi-tabbed command-line front-end.
It was for Windows 10 to replace the windows console.
Windows Terminal is an up-to-date terminal application. It is for command-line tools and shell users. It supports a wide range of environments like
- Command Prompt
- PowerShell
- Windows Subsystem for Linux (WSL)
And by default, Windows Terminals open PowerShell tab. You may now have multiple shell environments open in the same window.
The easiest way (and preferred way) to download Windows terminal from the Windows store.
You may also obtain it from GitHub's releases, but I suggest the store because it will always be up to date.
How you can use Git in PowerShell?
You can use Git in PowerShell.
How?
It's super easy!
On Windows 10 it's only one step.
You need Git and PowerShell installed on your machine.
that’s it.
You are done!
Start writing Git commands into PowerShell.
In the screenshot below I am checking the Git Version in PowerShell.
You can use the following command.
git --version
Here you are executing a Git in PowerShell.
Chapter 2
PowerShell vs PowerShell Core
What is PowerShell
To get a better knowledge of PowerShell, let's start by defining what is a shell.
A shell is a computer program that allows you to control your computes with the help of commands.
Windows PowerShell is a command-line shell created by Microsoft for task automation and configuration management.
PowerShell is now an open-source project that you can use on Windows, Mac OS X, and Linux.
PowerShell vs PowerShell Core
There are many differences between PowerShell and PowerShell core.
The vital difference is PowerShell core is cross-platform.
You can run PowerShell core on Windows, Linux, and macOS. Also, it's open-source.
Here is the table that shows the other significant difference.
Features | PowerShell | PowerShell |
---|---|---|
Compatibility | Windows Only | Cross Platform |
Program | powershell.exe | pwsh.exe |
Scripting Environment | Windows PowerShell Integrated Scripting Environment (ISE) | Visual Studio Code |
Install Location | $env:WINDIRSystem32 WindowsPowerShellvl.O | $env:ProgramFiles Pow |
Profile Path |
DocumentsWindowsPowerShell |
DocumentsPowerShell |
Important thing..
You can keep both (PowerShell and PowerShell Core) on your windows.
Windows PowerShell is there by default in Windows 10. And you can install PowerShell core from the Microsoft store.
(Microsoft Store is a centralized app distribution hub.)
You can use Git in the PowerShell Core and in Windows PowerShell.
In next section, You will learn the best way to check the installed version of PowerShell.
Keep reading..
What PowerShell Version do You have?
You can check the PowerShell version with $PSVersionTable
It is the best and easiest way to referencing the PSVersion property on the $PSVersionTable automatic variable.
Why?
This method will always represent the PowerShell engine.
I've both, windows PowerShell and PowerShell core installed on my machine.
Windows PowerShell 5.1
And PowerShell Core 7.1
As you can see, by default, the blue background is for the windows PowerShell, and the black background is PowerShell core.
Chapter 3
PowerShell vs Git Bash
Is Git Bash the same as PowerShell?
Both are different!
Originally PowerShell was windows-native.
And now, PowerShell core is cross-platform and open source.
On the other hand, Git bash is bash.
The name is an abbreviation for the 'Bourne-Again SHell,'
It is the default shell on macOS (but not on Windows, on windows its cmd.exe)
The primary distinctions between these two are in their syntax and data processing.
How does PowerShell differ from Bash?
I can share the one significant difference between PowerShell and Bash.
PowerShell treats outputs as objects. And Bash treats them as strings.
PowerShell can stream objects and data across scripts & apps. Each object has its own set of attributes, allowing PowerShell to handle data in greater detail.
Bash, like PowerShell, may use pipes to transfer data between commands (but in the form of strings).
Which is better: PowerShell or the Git bash?
PowerShell and Bash are scripting languages.
Bash is a CLI (Command Language Interpreter).
And PowerShell is both a command-line interface and a programming language.
With PowerShell you can do more things (especially if you are on Windows OS)
That's why you found PowerShell on the winning side.
PowerShell Integrated Scripting Environment (ISE) demonstrates how you can build scripts fast without compromising direct access to a command line.
And when you use Git in PowerShell with best practices you will get productive results.
Comparison of Bash vs. PowerShell Commands
PowerShell is a shell that is comparable to Bash.
Most bash commands, such as 'rm,' 'ls,' and 'cp,' may also be used in PowerShell.
Both shells provide commands for traversing directories, manipulating files, and starting other applications.
Here is a quick comparison of Bash and PowerShell commands.
Description | Bash | PowerShell |
---|---|---|
List (Files/Dir) | ls | Get-childItem (or Ls) |
Change Dir | cd | Set-childItem |
Current Dir | pwd | Get-Location |
Copy File | cp | Copy-Item |
Delete File | rm | Remove-Item |
Print String | echo "string" | Write-host "string" |
Create File | touch <filename> | New-Item <filename> |
Search Path | $PATH | $env:path |
Create variable | var=1 | $var=1 |
Search string | grep | Select-string |
That's it about PowerShell and Bash.
Now it's time to move on to advanced topics.
It's time to use Git in PowerShell and boost your productivity to the next level.
More in the next chapter.
Keep reading.
Chapter 4
Git Powerline (Productive Hack) Step-by-Step
If you use Git on the PowerShell, it will boost your productivity.
And by using Powerline, productivity will be simply doubled.
Here I will share everything you need to know to install and use Powerline.
What is Powerline?
I said many times, Powerline can boost your overall productivity many times.
But what the heck is Powerline?
Powerline customises the command prompt experience.
So you can see Git status color-coding and prompts with PowerShell powerline.
You can install and use Powerline in PowerShell and Windows Terminal.
Let's see how you can set it up.
Keep reading.
How to Setup Powerline
You can use PowerShell to install the Powerline.
You need few things (with Git and PowerShell) to setup and user Powerline in Windows Terminal.
What is Posh Git (& Why You Need it)?
In simple words, posh-git is a PowerShell module.
(Modules in Windows PowerShell allow you to split, organize code into self-contained, reusable components.)
Git-Posh Installation (Step-By-Step)
Before you install posh-git, there are three prerequisites.
On my Windows PowerShell, the execution policy is Unrestricted.
On Windows Terminal, the default tab is PowerShell.
I've both PowerShell and PowerShell Core installed on my machine.
And as you can see below, both may have different execution policies.
In my machine for PowerShell core, the execution policy is RemoteSigned.
PowerShell's execution policy is a security feature.
It directs how your files and scripts are loaded and executed.
You can check it with
Get-ExecutionPolicy
And set with
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm
Suppose you want to set up Powerline in both
- PowerShell Core and in
- Windows PowerShell.
Then, you have to install all modules separately on both versions.
Use PowerShell to install Posh-Git
There are many ways to install Git-Posh. I will focus on the PowerShell way.
Its my preferred option.
The PowerShell Gallery is a one-stop-shop for all things PowerShell. It is like a repository for PowerShell content.
Microsoft offers a PowerShell Gallery with a variety of free materials.
You must first download and install the PowerShellGet module. If you are using an older version of PowerShell - 3.0 or lower)
Here is the good news.
Windows 10 comes with version 5 of PowerShell. And the PowerShellGet module is available in PowerShell version 5.
Execute the following command on PowerShell
Install-Module posh-git -Scope CurrentUser -Force
Here is how it will look when you install the posh-git.
Configure Posh-Git
Use the following command from PowerShell. And it will import the posh-git module.
Import-Module posh-git
You don't want to have to run the Import-Module command manually every time you open a new PowerShell prompt.
You can posh-git to profile by running the following command.
Add-PoshGitToProfile
This will add the command to the file $profile.CurrentUserCurrentHost.
You can view and confirm if Posh Git added to your profile with following command.
Get-Content $profile.CurrentUserCurrentHost
And that’s it, posh-git is installed and running.
What is Oh-My-Posh (& Why You Need it)?
Oh My Posh is a custom engine.
You can use it for any shell. But the shell should have the ability to adjust the prompt string with a function or variable.
You need Oh my post as it allows you to visualize the prompt in the style you want.
Oh-My-Posh Installation (Step-By-Step)
You can use PowerShell to install oh-my-posh.
Install-Module oh-my-posh -Scope CurrentUser
You might get message like below
The fact that if any repository is untrusted implies that it can't automatically install modules without user involvement. Or with someone using the -Force option.
So in this case you have to press Y to continue with the installation.
With the Set-PSRepository, you may set the repository to be trusted and no longer receive the prompts.
Configure Oh-My-Posh
Use the following command from PowerShell. And it will import the posh-git module.
Import-Module oh-my-posh
Powerline Fonts
You can use Cascadia Code font.
Once installed, Cascadia Code font, you can use inside PowerShell and in Windows Terminal.
Also, this font is suitable for Powerline symbols.
Powerline Fonts Installation (Step-By-Step)
Installation is straightforward.
You can visit the Cascadia Code font release page and download the fonts zip file.
Here is the screenshot.
When you unzip the folder, you see many fonts.
The most crucial font for the Powerline is below (with the word PS in them).
You can right click and install the font.
And once you install the fonts, its time to configure.
Keep reading..
Configure Powerline Font
It's easy to configure the font with PowerShell (or PowerShell Core).
Simply open the settings menu from Windows Terminal.
From the Settings select the powerline compatible font.
You can select font separately for Windows PowerShell and for PowerShell Core.
The second option is to open the JSON file from the UI or directly open from the path
%LOCALAPPDATA%PackagesMicrosoft.WindowsTerminal_8wekyb3d8bbweLocalStatesettings.json
That's it.
Its time to set a theme before you use your powerline.
PSReadLine (Optional - Required for PowerShell Core)?
PSReadLine is only required if you are using PowerShell core.
It will enhance the command-line editing experience.
Here is the PowerShell way to install it
Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck
Set a Theme (Pretty Prompt) in Windows Terminal
Now it's time to convert your prompt to pretty prompt in the windows terminal.
You already have the posh-git & oh-my-posh. It's time to set a theme
Here is the way
Set-PoshPrompt -Theme Paradox
PowerShell profile
The PowerShell Profile, or $PROFILE, is a script that PowerShell loads every time you start the PowerShell.
The $profile is an automatic variable
You can create your $PROFILE or edit the existing one with the following.
# It will create a profile if it does not exist. And then edits it
if (!(Test-Path -Path $PROFILE)){ New-Item -Path $PROFILE -ItemType File } ; ise $PROFILE
This will open the PowerShell ISE, allowing you to edit the profile.
Once you are in the $PROFILE, you can add these three lines to the end.
Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt -Theme fish
The last line is to set a theme. Here the theme name is fish.
For you to use, there are many themes available. You can find it here.
Here is how Git in PowerShell (before Powerline)
Here is screenshot of Git in PowerShell (after Powerline)
In the next section, I will share more about the Git command in PowerShell.
Keep reading...
Chapter 5
Git in PowerShell : Productive Results
This Chapter is all about executing Git commands in the PowerShell environment.
You will see how Git in PowerShell is more friendly and boost your productivity.
Git in PowerShell: Get Productive Results
Now you have EVERYTHING that you need to run Git in PowerShell.
Here you will learn how you can use PowerShell instead of Git Bash.
(Git in PowerShell is more productive)
Let’s dive in.
Here, I am sharing five simple Git commands for beginners.
It is not a replacement for the Git Commands guide. You need to refer to the guide for all the Git commands.
For now, let's start with a cheat sheet for beginners.
1. git clone
The very first step is to clone the repository with git clone.
$ git clone https://github.com/R-B-2020/Demo.git
2. git checkout -b
Its time for you to create a new branch
$ git checkout -b experiment-branch
Because this repository is purely for a demo, I will use PowerShell to write something to the readme file.
"Demo file for https://acompiler.com/" | Out-File ".readme.md"
Did you notice something?
I am using PowerShell and Git side by side. (from PowerShell)
This is the beauty to use Git in PowerShell.
Let's check the status with $ git status
3. git add
Time to stage your changes
$ git add -A
4. git commit
You need to commit your modification. The -m option specifies a commit message.
$ git commit -m "Text added in the readme.md"
5. git push
Once you've committed your change, it's time to push to the remote.
$ git push
You will get message similar to
fatal: The current branch experiment-branch has no upstream branch.
You can set upstream with the following command
$ git push --set-upstream origin experiment-branch
And you can use your PAT to connect with GitHub (This step is different for different remote repos)
Once authorized, you are ready to push your changes.
And you can merge the pull request in GitHub. For more Git commands, you can follow the detailed guide here.
This is all about the combo pack - The Git in PowerShell.
What Did You Think?
Now I’d like to hear from you:
Which technique from this post (Git in PowerShell ) you like the most?
Are you going to use PowerShell or Windows Terminal?
Either way, let me know by leaving a comment below right now.