All articles
2 min read

How to Add 'Publish to GitHub' to Windows Context Menu

Add a right-click 'Publish to GitHub' option to Windows Explorer. When you right-click a folder, a Python script initializes a Git repo and pushes it to a new GitHub repo via SSH.

This guide adds a Publish to GitHub entry to the Windows right-click context menu. Right-clicking a directory runs a Python script that initializes a Git repository in that folder and pushes it to GitHub.

Prerequisites

  • Python installed and accessible from the command line
  • Git installed
  • An SSH key configured for GitHub (see GitHub's SSH key guide)
  • A GitHub account with your GitHub username known

The Python Script

Create publish_to_github.py in a permanent location (e.g., C:\scripts\publish_to_github.py):

import subprocess
import os
import re
import sys
import unicodedata

GITHUB_USERNAME = "YOUR_GITHUB_USERNAME"  # replace this

def slugify(value):
    value = str(value)
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
    value = re.sub(r'[^\w\s-]', '', value)
    return re.sub(r'[-\s]+', '-', value).strip('-_')

def main():
    # Change to the target directory (passed as argument from context menu)
    target = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
    os.chdir(target)

    if os.path.exists('.git'):
        print("ERROR: Git repository already exists in this folder.")
        input("Press Enter to exit...")
        return

    folder_name = slugify(os.path.basename(os.getcwd()))
    repo_url = f"git@github.com:{GITHUB_USERNAME}/{folder_name}.git"

    print(f"Publishing '{folder_name}' to GitHub...")

    try:
        subprocess.run(['git', 'init', '-b', 'main'], check=True)
        subprocess.run(['git', 'add', '.'], check=True)
        subprocess.run(['git', 'commit', '-m', 'Initial commit'], check=True)
        subprocess.run(['git', 'remote', 'add', 'origin', repo_url], check=True)
        subprocess.run(['git', 'push', '-u', 'origin', 'main'], check=True)
        print(f"\nSUCCESS! Published to {repo_url}")
    except subprocess.CalledProcessError as e:
        print(f"\nERROR: {e}")
        print("Make sure the GitHub repository exists and your SSH key is configured.")

    input("Press Enter to exit...")

if __name__ == '__main__':
    main()

Update GITHUB_USERNAME to your GitHub username. The script expects the GitHub repository to already exist — create it first at github.com/new (without README/gitignore, so it's empty).

Registry Files

For a Directory (right-click on folder)

Create context_menu_dir.reg:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Publish to GitHub]
@="Publish to &GitHub"
"Icon"="%SystemRoot%\\System32\\shell32.dll,71"

[HKEY_CLASSES_ROOT\Directory\shell\Publish to GitHub\command]
@="python \"C:\\scripts\\publish_to_github.py\" \"%1\""

For the Directory Background (right-click inside a folder)

Create context_menu_bg.reg:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\Publish to GitHub]
@="Publish to &GitHub"
"Icon"="%SystemRoot%\\System32\\shell32.dll,71"

[HKEY_CLASSES_ROOT\Directory\Background\shell\Publish to GitHub\command]
@="python \"C:\\scripts\\publish_to_github.py\" \"%V\""

Update C:\\scripts\\publish_to_github.py to the actual path where you saved the script. Note the double backslashes.

Installing the Context Menu Entry

Double-click the .reg file and click Yes when Windows asks to confirm the registry change.

To verify it's installed: right-click a folder in Windows Explorer — you should see Publish to GitHub in the context menu.

Removing the Entry

Create remove_context_menu.reg:

Windows Registry Editor Version 5.00

[-HKEY_CLASSES_ROOT\Directory\shell\Publish to GitHub]
[-HKEY_CLASSES_ROOT\Directory\Background\shell\Publish to GitHub]

Double-click to remove the entries.

Alternative: Register via HKEY_CURRENT_USER (No Admin Required)

Replace HKEY_CLASSES_ROOT\Directory with HKEY_CURRENT_USER\Software\Classes\Directory in the .reg files to install without admin privileges (user-only, not system-wide).

Conclusion

This workflow combines a Python script with Windows registry entries to add a one-click publish action. The key requirement is that the GitHub repository must exist before pushing — you can automate repository creation using the GitHub CLI: gh repo create folder-name --private.