Git for Sysadmins – Using POSH-GIT
Apart from the amusing name, POSH-GIT is a PowerShell module for GitHub supplied as part of Git for Windows. In this Ask the Admin, I’ll show you how to use Git source control via this PowerShell module.
If you’ve been following my series on GitHub, you’ll understand the basics of Git, GitHub and how to work with GitHub for Windows, the free GUI tool. Git is an open-source versioning system for code, which when used with a hub – think GitHub – allows developers to collaborate on projects in the cloud. When GitHub for Windows is installed, the PowerShell module for GitHub, Git Shell, is also installed, allowing you to clone repositories, send pull requests, among other things, all from the command line.
For more information on Git and GitHub, see What is GitHub?, Create a GitHub Repository, and GitHub for Windows – Installation, Adding Accounts, Committing Changes, and Syncing Repos on the Petri IT Knowledgebase.
Make sure that GitHub for Windows is installed, and open the Git Shell by double clicking the Git Shell icon on your desktop. You can alternatively use Import-Module Posh-Git to import the module, but you’ll need to change the working directory, or specify the full path to the posh-git.psm1 PowerShell module file, to the GitHub for Windows LocalAppData directory.
Clone a repository
To clone a repo from GitHub, you first need the download URI.
- Open the repo you want to clone in GitHub on the web.
- On the Code tab, click the green Clone or download button.
- In the Clone with HTTPS box, copy the URI.
- Make sure your PowerShell remote script execution policy is set to Unrestricted or RemoteSigned. To check the current policy, run Get-ExecutionPolicy. If you need to change the execution policy, run Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm.
- Now run the command below in the Git Shell, replacing the URI with the one you just copied to the clipboard.
git clone git://github.com/dahlbyk/posh-git.git
- Now change to the posh-git directory by typing cd posh-git.
- Type git status to see the current status of the repo.
Add an SSH key to Git Shell
Some repos can only be cloned using a secure connection. To add that ability to Git Shell, you’ll need to generate an SSH key as follows:
- In the Git Shell, type ssh-keygen.
- You’ll be prompted for a file name. Press ENTER to accept the default filename.
- Enter a passphrase for the file and confirm it.
- The SHA256 thumbprint will then be displayed in the console window.
- Now type add-sshkey. Enter the passphrase for the file you created above to add the identity to Git Shell.
Create a new repository
Let’s create a new repo on the local PC with no connection to GitHub.
- Create a directory for the new repo. For example, mkdir mynewrepo.
- Change to the new directory, using cd mynewrepo.
- Initialize the repo as follows: git init
- You can then check its status by running git status.
Let’s commit changes we’ve made to a repo.
- Make sure you’re in the working directory of your repo.
- Type git add . – note the period after ‘add’. This tells GitHub to add every file to the index. You can omit the period and alternatively specify individual files.
- Type git commit -m “new project files”. The -m parameter is required for the commit message.
Push files to GitHub
Once the files have been committed, we can push the repo to GitHub.
- Make sure you’re in the working directory for your repo and that any changed files that you want to push to GitHub have been committed.
- Type git push to upload changes to the server.
- If you want to generate a pull request, use git request-pull as shown below, replacing the URL for the repo, and a version number.
git request-pull v1.0 https://github.com/user/petri_testing.git
More in Windows Server
Latest Windows Server Updates May Cause Printing and Scanning Issues
Jul 25, 2022 | Rabia Noureen
Latest Windows Server vNext Insider Build Brings Support for LAPS
Jul 7, 2022 | Rabia Noureen
Microsoft to Fix Windows Bug Breaking Wi-Fi hotspots After Installing Latest Patch Tuesday Update
Jun 17, 2022 | Rabia Noureen
Microsoft Confirms Windows Server Backup Issues in This Month's Patch Tuesday Updates
Jun 16, 2022 | Rabia Noureen
Microsoft Acknowledges Office Zero-Day Flaw Affecting Windows Diagnostic Tool
May 31, 2022 | Rabia Noureen
Microsoft Releases Out-Of-Band Patches to Fix Windows AD Authentication Issues
May 20, 2022 | Rabia Noureen
Most popular on petri