Changing the Default Git branch in Github

Updated:

Categories: ,


When working with Git, it’s sometimes necessary to change the default branch — especially if you want pull requests to target a branch other than main or master by default. This can also be important when migrating repositories from Bitbucket to GitHub, as GitHub may incorrectly set the default branch (often choosing the first one alphabetically).

Step 1: ✅ Install the GitHub CLI (gh)

macOS (Homebrew)

brew install gh

Ubuntu/Debian

sudo apt install gh

Windows (via Winget)

winget install --id GitHub.cli

🔗 More install options →


Step 2: 🔐 Authenticate with GitHub

After installing, run:

gh auth login

Follow the prompts to:

  • Select GitHub.com
  • Choose SSH (always use SSH)
  • Open a browser to authenticate
  • Authorize the CLI to access your account

Step 3: 🌿 Set the Default Branch

Once authenticated, run:

gh repo edit your-org/YOUR_GIT_REPO --default-branch main

Replace:

  • your-org with your GitHub username or organization
  • your-repo with the repository name
  • master with your desired default branch (e.g. main, develop, etc.)

✅ If the branch exists, it will now be set as the default branch on GitHub.


Example

Change “main” to the branch you want to set as default.

gh repo edit Kobayashi-Zeitguys/YOUR_GIT_REPO --default-branch main