Mixing Powershell and Git with powerful shortcut commands

M

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:

[code]
git checkout development
git pull

git checkout master
git pull

git merge development
git push
[/code]

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:

[code]
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
}
[/code]

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.

[code]
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
}
[/code]

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

[code]
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
}
[/code]

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

About the author

By Jamie

My Books