morristech
9/13/2018 - 6:41 AM

Getting setup with PowerShell and Git and GitHub

Getting setup with PowerShell and Git and GitHub

My journey with PowerShell, Git, GitHub (CygWin, Strawberry Perl already installed)

PowerShell - already on Win7+
-create profile folder ~/Documents/WindowsPowershell/
-create modules folder ~/Documents/WindowsPowershell/Modules
-Keep this handy: http://technet.microsoft.com/en-us/library/ee692764.aspx
-recommend 
  installing Chocolatey:  http://chocolatey.org/ (you'll need something like CPAN for Perl to keep track of PS stuff and other things)
		to install:
		cmd.exe> @powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin - See more at: http://chocolatey.org/#sthash.5KjLIpD5.dpuf
	IMPORTANT NOTE: You can install apps through Chocolatey as .Install or not (.Install shows up in Windows Add/Remove Pgm; the other is "portable") 
	See the following for more info:  
	https://github.com/chocolatey/chocolatey/wiki/ChocolateyFAQs#what-distinction-does-chocolatey-make-between-an-installable-and-a-portable-application
	http://www.powershellmagazine.com/2012/02/20/chocolatey-and-powershell-revolutionize-windows-part-ii/
	
	installing Git:  PowerShell(PS)>cinst git.install  
	--See: http://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide	
	
	installing PoshGit:  PS>cinst poshgit <== NORMALLY YES; BUT the source code is newer so get from there (first use of Git)
	--See: http://dahlbyk.github.io/posh-git/ {newer then Mark Embling's original post}
	--provides PowerShell/Git integration
	
	installing Notepad++ (if you don't already have it):  PS>cinst notepadplusplus.install
	--a really good editor with language smarts (highlighting...  ;-)  )
	
	installing 7Zip:  PS> cinst 7zip.install
	--a really good zip pgm
	
	installing Mark Embling's scripts to change the prompt; see http://markembling.info/2009/09/my-ideal-powershell-prompt-with-git-integration
	--place all three in profile folder (~/Documents/WindowsPowershell/)

	profile.ps1:  from https://gist.github.com/markembling/180854#file-profile-ps1 
	**I changed the '>' at the very end to '$' - bash'ers will know...
	**save as ~/Documents/WindowsPowershell/Microsoft.PowerShell_profile.ps1

	gitutils.ps1: from https://gist.github.com/markembling/180853#file-gitutils-ps1
	--adds the branch and new/changed/deleted counter in Git repos to command prompt

	ssh-agent-utils.ps1:  from https://gist.github.com/markembling/187305
	--auto starts ssh agent for comms with https sites.
	
	
	installing TabExpansionPlusPlus: 
	PS> iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/lzybkr/TabExpansionPlusPlus/master/Install.ps1')



	**ensure the following lines are in your profile.ps1 (the dot followed by space is important - persists variables after script exit)
		# See http://gist.github.com/180853 for gitutils.ps1.
		. (Resolve-Path ~/Documents/WindowsPowershell/gitutils.ps1)
		# See https://gist.github.com/markembling/187305
		. (Resolve-Path ~/Documents/WindowsPowershell/ssh-agent-utils.ps1)
		# Load posh-git example profile [don't like install location {c:\tools}, but result of cinst auto install]
		# See https://github.com/dahlbyk/posh-git {looks newer then chocolatey}
		# See http://chocolatey.org/packages/poshgit
		# line below replaces what the poshgit inserted
		. (Resolve-Path C:/tools/poshgit/dahlbyk-posh-git-36d072f/profile.example.ps1)

	installing the GitHub GUI (for those so inclined)
	--see https://help.github.com/articles/set-up-git and select the Download GitHub for Windows
	--I did this first, but then did all the CL stuff as I prefer to work from CL.
	
	optional install: Password caching if entering your password is really annoying
	--see https://help.github.com/articles/set-up-git and select the Download GitHub for Windows
	--see also http://www.manpagez.com/man/1/git-credential-cache/
	--see for help http://stackoverflow.com/questions/11693074/git-credential-cache-is-not-a-git-command
	--source code (for the paranoid) is available at http://gitcredentialstore.codeplex.com/SourceControl/latest#README.md
	--ensure git.cmd is in environment path {it is if you can run git}
	--download from http://github-windows.s3.amazonaws.com/GitHubSetup.exe
	--or from http://gitcredentialstore.codeplex.com/downloads/get/640464
	--there is no Chocolatey install for it yet.
	--then copy to "C:\Program Files (x86)\Git\libexec\git-core\git-credential-winstore.exe"
	--run PS> git config --global credential.helper winstore
	--to get rid of it: PS> git config --unset --global credential.helper

Cool things you can do...
run sh (and shell scripts)- once you install Git and Poshgit
run bash - once you install Cygwin
create GitHub Repositories from command line
On GitHub, create OAuth tokens to use instead of UID/password; can do from command line

Fiddling...
-I had to fiddle with the terminal colors for the pinned PS instance in taskbar and called from Run.
-Escaping on the PS CL is not fun!  Had to use double escape for double quotes when those have to pass through to the command
-- " becomes \`"  (backslash backtick double-quote)


Git and GitHub Features (should work from Cygwin and PS and some limited cmd.exe functionality)
First need to configure...
	git config —global user.name "USER"
	git config —global user.email "email.addie.registered.w.GitHub@mailservice.com"
	git config --global push.default simple --otherwise, you get warnings.

	Some helpful sites:
	http://git-scm.com/book/en/Getting-Started-Git-Basics
	http://developer.github.com/guides/getting-started/ (useful for figuring out how curl works)
	http://lifehacker.com/5983680/how-the-heck-do-i-use-github
	
	
To list authorizations:
curl -u 'USER' -G https://api.github.com/authorizations

To create authorizations:
Should work!  maybe works in Linux and Cygwin...  
curl -u 'USER' -d '{"scopes": ["public_repo"], "note": "YOUR NOTE LIKE-CL authorization"}' https://api.github.com/authorizations

Necessary for Powershell...  double escaped quotes backslash and backtick! (can also try --globoff on curl but didn't work for me)
curl -u "USER" -d "{\`"scopes\`": [\`"public_repo\`"], \`"note\`": \`"YOUR NOTE LIKE-CL authorization\`"}" https://api.github.com/authorizations

Adding a repo under Linux/Cygwin REPLACE CAPS
curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
git remote add origin https://github.com/USER/REPO.git
git push origin master

And under PowerShell  
curl -u "USER" https://api.github.com/user/repos -d "{\`"name\`":\`"REPO\`"}"
git remote add origin https://github.com/USER/REPO.git
git push origin master

**If origin already exists (and it's bad), remove with 'git remote remove origin'; this only affects local config - sort of like disconnecting local from the remote (doesn't delete remote repo - you should do that through the GitHub GUI - likely can do it through the v3 API, too.)

Some useful commands:
$Auth = [ -u 'USER' | -H 'Authentication: token OAUTHTOKEN' ]

curl $Auth https://api.github.com/user
-returns information about you

curl $Auth https://api.github.com/user/repos
-returns info about your repos

curl https://api.github.com/users/USER/repos
-another user's repos

curl https://api.github.com/orgs/ORG/repos
-another organization's repos

So, creating repos from CL.  You only need the Name property, but other useful ones are:
description
team_id
see http://developer.github.com/v3/repos/ and look for <>Create for others

Tired of entering your UID/pswd using the HTTPS clone method?  Can switch to the older SSH clone method
--see https://help.github.com/articles/why-is-git-always-asking-for-my-password
--simply use git@github.com:USER/REPO.git, instead of https://github.com/USER/REPO.git
--UID and REPO ARE case sensitive for SSH!
	git remote set-url origin git@github.com:UID/RePO.git --switches to SSH
	git remote set-url origin https://github.com/UID/REPO.git  --switches back to https
**I like SSH...  no more entering that pesky password!
Note:  SSH can be pushed over 443, too.  See https://help.github.com/articles/using-ssh-over-the-https-port

**Final Note:  gist.github.com provides a spot for those quick snippets instead of attempting to create a whole repo of odds n ends.