Mixing Powershell and Git with powerful shortcut commands Mixing Powershell and Git with powerful shortcut commands

I use GitHub everyday. I don't like the UI, it's slow and cumbersome. So instead I use a combination of Powershell and posh-git; it's the only tools I use. In this article I'm going to show you how to create shortcut commands like merge2master and merge2dev that automate the repeatable tasks of maintaining a development and master branch with GitHub.


To get started you need to create a Powershell profile. To see if your profile file already exists go to your ~/My Documents/WindowsPowershell and look for Microsoft.PowerShell_profile.ps1. If it already exists, open it with your favorite text editor; otherwise, create this file now.

The standard merge development to master

Whenever I am ready to deploy new features I need to merge my changes from the development branch to the master branch. I accomplish this as follows:


git checkout development
git pull
git checkout master
git pull
git merge development
git push

My code is now successfully moved from the development branch to the master branch. I often find myself performing this every two weeks. So much tedious tension.

The awesomeness of Microsoft.PowerShell_profile.ps1

Enter Powershell. The following code is a Powershell function called merge2master that contains the exact same commands as above:


Function merge2master {
	[cmdletbinding()]
	Param () 
	# End of Parameters
	Process {
		git checkout development
		git pull
		git checkout master
		git pull
		git merge development
		git push
	} # End of Process
}

Now inside of Powershell I can simply type merge2master and the process of automating the merge to master is completed.

You may have to close and re-open Powershell as you make changes to your profile for it to take affect.

Next let's create a function called merge2dev. I use this function when I have to perform a hotfix on the master branch and want to keep my development branch up-to-date. The function is almost the same with the order reversed.


Function merge2dev {
	[cmdletbinding()]
	Param () 
	# End of Parameters
	Process {
		git checkout master
		git pull
		git checkout development
		git pull
		git merge master
		git push
	} # End of Process
}

I've create a variety of other Git shortcuts as well:


Function pull {
	[cmdletbinding()]
	Param () 
	# End of Parameters
	Process {
		git pull
      } # End of Process
}
Function push {
	[cmdletbinding()]
	Param () 
	# End of Parameters
	Process {
		git push
      } # End of Process
}
Function status {
	[cmdletbinding()]
	Param () 
	# End of Parameters
	Process {
		git status
      } # End of Process
}
Function add {
	[cmdletbinding()]
	Param () 
	# End of Parameters
	Process {
		git add -A
      } # End of Process
}
Function dev {
	[cmdletbinding()]
	Param () 
	# End of Parameters
	Process {
		git checkout development
      } # End of Process
}
Function master {
	[cmdletbinding()]
	Param () 
	# End of Parameters
	Process {
		git checkout master
      } # End of Process
}

All of the above functions basically save me from having to prefix everything with "git". 4 keystrokes saved each time!

Published on Apr 16, 2019

Tags: Uncategorized | powershell | git

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.