Skip to content

Instantly share code, notes, and snippets.

@asheroto
Last active October 11, 2024 17:24
Show Gist options
  • Save asheroto/a9a237f1199a9ac7275adf4a12d4b7f1 to your computer and use it in GitHub Desktop.
Save asheroto/a9a237f1199a9ac7275adf4a12d4b7f1 to your computer and use it in GitHub Desktop.
Use PowerShell or CMD to open the GitHub repository for the current folder in your default web browser. Use parameters to open issues, pull requests, and more.

Open GitHub Repository for the Current Folder

This PowerShell script provides a way to open the GitHub repository for the current folder in your default web browser. It extracts the remote URL of the repository, parses the author and project name, and constructs the GitHub URL to open in the browser. Additionally, it supports opening specific pages like issues, pull requests, actions, projects, releases, and branches.

Usage

  1. Copy the Open-GitHubRepo function from Open-GitHubRepo.ps1 into your PowerShell profile and save it.
  2. Close PowerShell and open a new instance of it.
  3. Browse to a folder containing a repository and type Open-GitHubRepo with the desired parameters into PowerShell.
  4. The repository should open in GitHub.

Examples:

  • Open the main repository page:
    Open-GitHubRepo
  • Open the issues page:
    Open-GitHubRepo -Issues
  • Open the pull requests page:
    Open-GitHubRepo -PullRequests
  • Open the actions page:
    Open-GitHubRepo -Actions
  • Open the projects page:
    Open-GitHubRepo -Projects
  • Open the releases page:
    Open-GitHubRepo -Releases
  • Debug mode to see detailed information:
    Open-GitHubRepo -Debug

Parameters

Parameter Description
-Issues Opens the Issues page of the repository
-Branches Opens the Branches page of the repository
-PullRequests Opens the Pull Requests page of the repository
-Actions Opens the Actions page of the repository
-Projects Opens the Projects page of the repository
-Releases Opens the Releases page of the repository
-Debug Outputs detailed information for debugging

Alternative Approach

Alternatively, you can use the git open command from git-open, a tool that allows you to open the GitHub repository in your browser directly from the command line.

Why Use This PowerShell Script?

If you prefer a simple PowerShell-only approach without relying on additional tools, this script provides a simple and effective solution for opening GitHub repositories in your web browser directly from the command line.

function Open-GitHubRepo {
param (
[switch]$Issues,
[switch]$Branches,
[switch]$PullRequests,
[switch]$Actions,
[switch]$Projects,
[switch]$Releases,
[switch]$Debug
)
# Get the remote URL of the repository
$remoteUrl = git remote -v | Select-String -Pattern "\s\(fetch\)" | ForEach-Object { $_.Line.Split()[1] }
if ($Debug) {
Write-Output "Remote URL: $remoteUrl"
}
# Extract the author and project name from the remote URL
if ($remoteUrl -match 'github.com[:/](.+?)/(.+?)(\.git)?$') {
$author = $matches[1]
$project = $matches[2]
if ($Debug) {
Write-Output "Author: $author"
Write-Output "Project: $project"
}
# Base GitHub URL
$githubUrl = "https://github.com/$author/$project"
# Determine the page to open
switch ($true) {
$Issues { $githubUrl += "/issues" }
$Branches { $githubUrl += "/branches" }
$PullRequests { $githubUrl += "/pulls" }
$Actions { $githubUrl += "/actions" }
$Projects { $githubUrl += "/projects" }
$Releases { $githubUrl += "/releases" }
}
if ($Debug) {
Write-Output "GitHub URL: $githubUrl"
}
# Open the URL in the default web browser
Start-Process $githubUrl
} else {
Write-Output "Failed to parse the GitHub URL."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment